@ -4,6 +4,7 @@ import RPi.GPIO as GPIO
import time
import time
import requests
import requests
import platform
import platform
import datetime
relay_pin = 11
relay_pin = 11
#GPIO.setmode(GPIO.BCM) # GPIO numbers instead of board numbers.
#GPIO.setmode(GPIO.BCM) # GPIO numbers instead of board numbers.
@ -18,7 +19,7 @@ def get_api_url():
return " http://ritherdon.abbether.net/api/readings/latest/1 "
return " http://ritherdon.abbether.net/api/readings/latest/1 "
elif ( device_id == " gallery2 " ) :
elif ( device_id == " gallery2 " ) :
return " http://ritherdon.abbether.net/api/readings/latest/2 "
return " http://ritherdon.abbether.net/api/readings/latest/2 "
# Make sure this is valid.
# Make sure this is valid.
# api_url = "http://ritherdon.abbether.net/api/readings/add/1"
# api_url = "http://ritherdon.abbether.net/api/readings/add/1"
api_url = get_api_url ( ) # Saves having to do the if-check every call.
api_url = get_api_url ( ) # Saves having to do the if-check every call.
@ -27,7 +28,8 @@ def main():
try :
try :
while True :
while True :
# Make sure the U.R.L. is valid.
# Make sure the U.R.L. is valid.
r = requests . get ( api_url )
s = requests . Session ( )
r = s . get ( api_url , timeout = 5 )
# print(r.status_code) # For testing.
# print(r.status_code) # For testing.
data = r . json ( )
data = r . json ( )
# print(data) # For testing.
# print(data) # For testing.
@ -43,6 +45,16 @@ def main():
GPIO . output ( relay_pin , GPIO . LOW )
GPIO . output ( relay_pin , GPIO . LOW )
except KeyboardInterrupt :
except KeyboardInterrupt :
print ( " [INFO] KEYBOARD INTERRUPT: quitting program. " )
print ( " [INFO] KEYBOARD INTERRUPT: quitting program. " )
except requests . exceptions . ConnectionError :
pause = 60
time . sleep ( 60 )
print ( f " [WARNING] MAX. REQUESTS EXCEEDED: Pausing requests for { pause } seconds... " )
pass
except requests . exceptions . Timeout :
t_stamp = datetime . datetime . now ( )
print ( f " [WARNING] TIMEOUT EXCEPTION: Request timed-out at { t_stamp } . " )
time . sleep ( 60 )
pass
except Exception as e :
except Exception as e :
print ( f " [ERROR] GENERAL EXCEPTION: { e } " )
print ( f " [ERROR] GENERAL EXCEPTION: { e } " )
finally :
finally :