@ -39,7 +39,7 @@ a_pin = 18 # Charges the capacitor.
b_pin = 23 # Discharges the capacitor.
b_pin = 23 # Discharges the capacitor.
device_id = platform . node ( ) # For servers logs.
device_id = platform . node ( ) # For servers logs.
session = requests . Session ( ) # Persist connection for REST calls.
def get_api_url ( ) :
def get_api_url ( ) :
# Yes, I know I could do this better. Stop moaning.
# Yes, I know I could do this better. Stop moaning.
@ -56,7 +56,6 @@ def discharge():
GPIO . output ( b_pin , False )
GPIO . output ( b_pin , False )
time . sleep ( 0.01 ) # 0.01 -- initial/default value.
time . sleep ( 0.01 ) # 0.01 -- initial/default value.
def charge_time ( ) :
def charge_time ( ) :
GPIO . setup ( b_pin , GPIO . IN )
GPIO . setup ( b_pin , GPIO . IN )
GPIO . setup ( a_pin , GPIO . OUT )
GPIO . setup ( a_pin , GPIO . OUT )
@ -67,12 +66,10 @@ def charge_time():
t2 = time . time ( )
t2 = time . time ( )
return ( t2 - t1 ) * 1000000
return ( t2 - t1 ) * 1000000
def analog_read ( ) :
def analog_read ( ) :
discharge ( )
discharge ( )
return charge_time ( )
return charge_time ( )
def read_resistance ( ) :
def read_resistance ( ) :
n = 20
n = 20
total = 0
total = 0
@ -82,23 +79,20 @@ def read_resistance():
resistance = reading * 6.05 - 939
resistance = reading * 6.05 - 939
return resistance
return resistance
def light_from_r ( R ) :
def light_from_r ( R ) :
return math . log ( 1000000.0 / R ) * 10.0
return math . log ( 1000000.0 / R ) * 10.0
def get_timestamp ( ) :
def get_timestamp ( ) :
return datetime . now ( ) . strftime ( ( " % Y- % m- %d % H: % M: % S " ) )
return datetime . now ( ) . strftime ( ( " % Y- % m- %d % H: % M: % S " ) )
def push_reading ( lvalue ) :
def push_reading ( lvalue ) :
time = get_timestamp ( )
time = get_timestamp ( )
headers = { " content-type " : " application/json " }
headers = { " content-type " : " application/json " }
payload = { " reading " : int ( lvalue ) , " time " : time ,
payload = { " reading " : int ( lvalue ) , " time " : time ,
" token " : " QWERTYuiopasdfghjklzxcvbnm_1234567890 " }
" token " : " QWERTYuiopasdfghjklzxcvbnm_1234567890 " }
# print(payload) # For testing.
print ( payload ) # For testing.
res = requests . post ( api_url , data = json . dumps ( payload ) , headers = headers )
# res = requests.post(api_url, data=json.dumps(payload), headers=headers )
res = session . post ( api_url , data = json . dumps ( payload ) , headers = headers )
def update_reading ( ) :
def update_reading ( ) :
light = light_from_r ( read_resistance ( ) )
light = light_from_r ( read_resistance ( ) )
@ -113,11 +107,22 @@ def main():
# pdb.set_trace() # For testing.
# pdb.set_trace() # For testing.
update_reading ( )
update_reading ( )
except KeyboardInterrupt :
except KeyboardInterrupt :
print ( " Keyboard Interrupt: quitting program. " )
print ( " [INFO] KEYBOARD INTERRUPT: quitting program. " )
except :
except requests . exceptions . ConnectionError :
print ( " Error updating light reading... " )
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 :
print ( f " [ERROR] GENERAL EXCEPTION: { e } " )
finally :
finally :
print ( " Cleaning up GPIO before closing... " )
print ( " [INFO] Terminating relay.py... " )
print ( " [INFO] Cleaning up GPIO before closing... " )
GPIO . cleanup ( )
GPIO . cleanup ( )