From bb32709f5d10f9afe6466de3196d6408209bfcfe Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Sun, 12 Jan 2020 23:19:02 +0000 Subject: [PATCH] remove old code from relay.py. --- relay.py | 68 ++++++++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/relay.py b/relay.py index d6f4a91..ab4713a 100644 --- a/relay.py +++ b/relay.py @@ -1,7 +1,8 @@ -# Need to install python-rpi.gpio +#!/usr/bin/python3 + import RPi.GPIO as GPIO import time -import requests # Used to grab light reading via the REST API. +import requests relay_pin = 11 #GPIO.setmode(GPIO.BCM) # GPIO umbers instead of board numbers. @@ -9,40 +10,29 @@ GPIO.setmode(GPIO.BOARD) GPIO.setup(relay_pin, GPIO.OUT) -try: - while True: -# r = requests.get('http://192.168.1.228:5000/api/readings/latest/1') - r = requests.get('http://35.176.14.135/api/readings/latest/1') - #print(r.status_code) - data = r.json() - #print(data) - the_reading = data.get("reading") -# print(the_reading) - if the_reading > 30: - print(f"[INFO] Light is on -- {the_reading}.") - GPIO.output(relay_pin, GPIO.HIGH) - else: - print(f"[INFO] Light is off -- {the_reading}.") - GPIO.output(relay_pin, GPIO.LOW) - # last = list(data.keys())[-1] - # reading = data.get(last) - # print(reading["active"]) - # if reading["active"] == True: - # print("Setting the relay to hight") - # GPIO.output(relay_pin, GPIO.HIGH) - # else: - # print("Setting the relay to low") - # GPIO.output(relay_pin, GPIO.LOW) -# print(r.light-reading) - # Set the pin to high -# print("Setting the relay to hight") -# GPIO.output(relay_pin, GPIO.HIGH) -# time.sleep(2) - # Set the pin to low -# print("Setting the relay to low") -# GPIO.output(relay_pin, GPIO.LOW) - #print(GPIO) -# time.sleep(2) -except KeyboardInterrupt: - GPIO.cleanup() - print("Cleaned up GPIO and quitting") +def main(): + try: + while True: + # Make sure the U.R.L. is valid. + r = requests.get('http://3.9.19.84/api/readings/latest/1') + # print(r.status_code) # For testing. + data = r.json() + # print(data) # For testing. + the_reading = data.get("reading") + # print(the_reading) # For testing. + if the_reading > 30: + print(f"[INFO] Light is on -- {the_reading}.") + GPIO.output(relay_pin, GPIO.HIGH) + else: + print(f"[INFO] Light is off -- {the_reading}.") + GPIO.output(relay_pin, GPIO.LOW) + except KeyboardInterrupt: + print("Keyboard Interrupt: quitting program.") + except: + print("Error updating light reading...") + finally: + print("Cleaning up GPIO before closing...") + GPIO.cleanup() + +if __name__ == "__main__" : + main()