1
0
Fork 0
Browse Source

remove old code from relay.py.

unstable
Craig Oates 4 years ago
parent
commit
bb32709f5d
  1. 68
      relay.py

68
relay.py

@ -1,7 +1,8 @@
# Need to install python-rpi.gpio #!/usr/bin/python3
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time import time
import requests # Used to grab light reading via the REST API. import requests
relay_pin = 11 relay_pin = 11
#GPIO.setmode(GPIO.BCM) # GPIO umbers instead of board numbers. #GPIO.setmode(GPIO.BCM) # GPIO umbers instead of board numbers.
@ -9,40 +10,29 @@ GPIO.setmode(GPIO.BOARD)
GPIO.setup(relay_pin, GPIO.OUT) GPIO.setup(relay_pin, GPIO.OUT)
try: def main():
while True: try:
# r = requests.get('http://192.168.1.228:5000/api/readings/latest/1') while True:
r = requests.get('http://35.176.14.135/api/readings/latest/1') # Make sure the U.R.L. is valid.
#print(r.status_code) r = requests.get('http://3.9.19.84/api/readings/latest/1')
data = r.json() # print(r.status_code) # For testing.
#print(data) data = r.json()
the_reading = data.get("reading") # print(data) # For testing.
# print(the_reading) the_reading = data.get("reading")
if the_reading > 30: # print(the_reading) # For testing.
print(f"[INFO] Light is on -- {the_reading}.") if the_reading > 30:
GPIO.output(relay_pin, GPIO.HIGH) print(f"[INFO] Light is on -- {the_reading}.")
else: GPIO.output(relay_pin, GPIO.HIGH)
print(f"[INFO] Light is off -- {the_reading}.") else:
GPIO.output(relay_pin, GPIO.LOW) print(f"[INFO] Light is off -- {the_reading}.")
# last = list(data.keys())[-1] GPIO.output(relay_pin, GPIO.LOW)
# reading = data.get(last) except KeyboardInterrupt:
# print(reading["active"]) print("Keyboard Interrupt: quitting program.")
# if reading["active"] == True: except:
# print("Setting the relay to hight") print("Error updating light reading...")
# GPIO.output(relay_pin, GPIO.HIGH) finally:
# else: print("Cleaning up GPIO before closing...")
# print("Setting the relay to low") GPIO.cleanup()
# GPIO.output(relay_pin, GPIO.LOW)
# print(r.light-reading) if __name__ == "__main__" :
# Set the pin to high main()
# 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")