You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
#!/usr/bin/python3 |
|
|
|
import RPi.GPIO as GPIO |
|
import time |
|
import requests |
|
|
|
relay_pin = 11 |
|
#GPIO.setmode(GPIO.BCM) # GPIO umbers instead of board numbers. |
|
GPIO.setmode(GPIO.BOARD) |
|
GPIO.setup(relay_pin, GPIO.OUT) |
|
|
|
|
|
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()
|
|
|