From 9ef10aef1690613d06783d87163c31c03757a114 Mon Sep 17 00:00:00 2001 From: Craig Date: Sun, 5 Jan 2020 19:15:40 +0000 Subject: [PATCH] port 'cli' code from light_meter to cli_meter. --- cli-meter.py | 21 -------- cli_meter.py | 87 ++++++++++++++++++++++++++++++++ light-meter.py => light_meter.py | 0 3 files changed, 87 insertions(+), 21 deletions(-) delete mode 100644 cli-meter.py create mode 100644 cli_meter.py rename light-meter.py => light_meter.py (100%) diff --git a/cli-meter.py b/cli-meter.py deleted file mode 100644 index d7505cc..0000000 --- a/cli-meter.py +++ /dev/null @@ -1,21 +0,0 @@ -""" -CLI Meter -==================================================================== -Takes a reading of the light using the meter attached via to the -intended Raspberry PI. The reading is the sent to a server where the -reading is stored. The intended use for this script is to be run via -the command-line without the need for a G.U.I. This should be the -version used when the "Return to Ritherdon" project is live. -""" -import json -import RPi.GPIO as GPIO -import time, math -import requests -from datetime import datetime - -# Using BCM (Broadcom) names when referencing the GPIO pins. -GPIO.setmode(GPIO.BCM) - -a_pin = 18 # Charges the capacitor. -b_pin = 23 # Discharges the capacitor. - diff --git a/cli_meter.py b/cli_meter.py new file mode 100644 index 0000000..aa185ae --- /dev/null +++ b/cli_meter.py @@ -0,0 +1,87 @@ +""" +CLI Meter +==================================================================== +Takes a reading of the light using the meter attached via to the +intended Raspberry PI. The reading is the sent to a server where the +reading is stored. The intended use for this script is to be run via +the command-line without the need for a G.U.I. This should be the +version used when the "Return to Ritherdon" project is live. +""" + +import json +import RPi.GPIO as GPIO +import time, math +import requests +from datetime import datetime +import pdb + +# 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. + +api_url = "http://35.176.14.135/api/readings/add/1" + +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 value + +def charge_time(): + GPIO.setup(b_pin, GPIO.IN) + GPIO.setup(a_pin, GPIO.OUT) + GPIO.output(a_pin, True) + t1 = time.time() + while not GPIO.input(b_pin): + pass + t2 = time.time() + return (t2 - t1) * 1000000 + +def analog_read(): + discharge() + return charge_time() + +def read_resistance(): + n = 20 + 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} + print(payload) + 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) + push_reading(light) + +def main(): + try: + while True: + #pdb.set_trace() + update_reading() + except: + print("Error updating light reading.") + finally: + print("clean up") + GPIO.cleanup() # cleanup all GPIO + +if __name__ == "__main__": + main() diff --git a/light-meter.py b/light_meter.py similarity index 100% rename from light-meter.py rename to light_meter.py