From 7a3e3df07af1ca878956eced2f4cbc9460751bf8 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Wed, 16 Dec 2020 16:53:19 +0000 Subject: [PATCH 1/2] change URL to ritherdon.abbether.net The URL used here is a domain name and not just an IP address. This should reduce the need to update the IP address in the code if the server, currently hosted on AWS at time of writing, needs to be restarted. The servers on AWS do not keep the same IP address if you stop and start them. The abbether.net domain is a personal one and used as a way to reduce project costs. I did not use this domain when I first wrote the code here because I did not own it. Long story short, the Covid-19 malarkey meant the project was put on hold and I ended up owning abbether.net during the first lock-down. The decision to use the domain is one of convince my end. I did not want to keep updating the code manually if the server on AWS was stopping and starting. --- shutdown.sh | 2 +- startup.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shutdown.sh b/shutdown.sh index 08190c4..ff0ca61 100755 --- a/shutdown.sh +++ b/shutdown.sh @@ -15,7 +15,7 @@ logDate=$(date '+%Y-%m-%dT%TZ') logFile="/home/rtrp/logs/shutdown-logs.txt" -mainURL="http://3.9.19.84/api/status/update" # Make sure this is valid. +mainURL="http://ritherdon.abbether.net/api/status/update" # Make sure this is valid. getApiUrl () { case $HOSTNAME in diff --git a/startup.sh b/startup.sh index e3720cb..0d5d912 100755 --- a/startup.sh +++ b/startup.sh @@ -24,7 +24,7 @@ sleep 60 logDate=$(date '+%Y-%m-%dT%TZ') logFile="/home/rtrp/logs/startup-logs.txt" -mainURL="http://3.9.19.84/api/status/update" # Make sure this is valid. +mainURL="http://ritherdon.abbether.net/api/status/update" # Make sure this is valid. getApiUrl () { case $HOSTNAME in From 6e4e4254cf6bbdfb3d838945d1d65c78d43641a8 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Wed, 16 Dec 2020 17:18:07 +0000 Subject: [PATCH 2/2] extend URL list for adding new light readings. The base URL uses ritherdon.abbether.net now, in the main part of the python code. I have added checks to see which device is running the code and forms a complete URL for the API call. The way the URL is formed for the API call is not the most elegant solution but is was quick to write and the code has a limited life-span. It will not require any further modification when it goes live. So, the speed it took to write it was a good trade-off in my opinion. --- cli_meter.py | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/cli_meter.py b/cli_meter.py index 79b8c5c..9755465 100755 --- a/cli_meter.py +++ b/cli_meter.py @@ -24,26 +24,42 @@ interrupt -- or something like Htop... doesn't matter. import json import RPi.GPIO as GPIO -import time, math +import time +import math import requests from datetime import datetime +import platform # import pdb # For testing. # Using BCM (Broadcom) names when referencing the GPIO pins. GPIO.setmode(GPIO.BCM) GPIO.setwarnings(True) -a_pin = 18 # Charges the capacitor. -b_pin = 23 # Discharges the capacitor. +a_pin = 18 # Charges the capacitor. +b_pin = 23 # Discharges the capacitor. + +device_id = platform.node() # For servers logs. + + +def get_api_url(): + # Yes, I know I could do this better. Stop moaning. + if (device_id == "factory1"): + return "http://ritherdon.abbether.net/api/readings/add/1" + elif (device_id == "factory2"): + return "http://ritherdon.abbether.net/api/readings/add/2" + # Make sure this is valid. -api_url = "http://3.9.19.84/api/readings/add/1" +# api_url = "http://ritherdon.abbether.net/api/readings/add/1" +api_url = get_api_url() + def discharge(): GPIO.setup(a_pin, GPIO.IN) GPIO.setup(b_pin, GPIO.OUT) GPIO.output(b_pin, False) - time.sleep(0.01) # 0.01 -- initial/default value. + time.sleep(0.01) # 0.01 -- initial/default value. + def charge_time(): GPIO.setup(b_pin, GPIO.IN) @@ -55,42 +71,50 @@ def charge_time(): t2 = time.time() return (t2 - t1) * 1000000 + def analog_read(): discharge() return charge_time() + def read_resistance(): n = 20 - total = 0; + total = 0 for i in range(1, n): total = total + analog_read() reading = total / float(n) resistance = reading * 6.05 - 939 return resistance + def light_from_r(R): return math.log(1000000.0/R) * 10.0 + def get_timestamp(): return datetime.now().strftime(("%Y-%m-%d %H:%M:%S")) + def push_reading(lvalue): time = get_timestamp() headers = {"content-type": "application/json"} - payload = { "reading": int(lvalue), "time": time, "token": "QWERTYuiopasdfghjklzxcvbnm_1234567890"} + payload = {"reading": int(lvalue), "time": time, + "token": "QWERTYuiopasdfghjklzxcvbnm_1234567890"} # print(payload) # For testing. res = requests.post(api_url, data=json.dumps(payload), headers=headers) + def update_reading(): light = light_from_r(read_resistance()) reading_str = "{:.0f}".format(light) # print(reading_str) # For testing. push_reading(light) + def main(): try: while True: - #pdb.set_trace() # For testing. + # pdb.set_trace() # For testing. update_reading() except KeyboardInterrupt: print("Keyboard Interrupt: quitting program.") @@ -100,6 +124,7 @@ def main(): print("Cleaning up GPIO before closing...") GPIO.cleanup() + if __name__ == "__main__": # time.sleep(60) # For testing/debugging. main()