|
|
|
|
# Need to install python-rpi.gpio
|
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
|
import time
|
|
|
|
|
import requests # Used to grab light reading via the REST API.
|
|
|
|
|
|
|
|
|
|
relay_pin = 11
|
|
|
|
|
#GPIO.setmode(GPIO.BCM) # GPIO umbers instead of board numbers.
|
|
|
|
|
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")
|