#---- required imports --------
import time, gc
from machine import Pin, time_pulse_us, WDT
import machine
#-----custom modules import
import powermanager as pwmr
import ultrasonic as ult
import espnows as esn
wdt = WDT(timeout=30*1000) # given 30 seconds for the wdt timer
def bundler(water_level=0):
# this funciton is resposible to collect the data and make that into a reciver format
# like battery level, water level, device id, time stamp and logs etc.
return str( {"water-level":water_level,"batery-level": pwmr.get_battery_level(),"solar-status": pwmr.get_solar_status()})
exception_count = 5
while True :
try:
print('hello')
# gets the water level
water_level = ult.get_water_level()
gc.collect()
time.sleep(1)
# bundles the reqired data sets
data = bundler(water_level)
print(data)
# sends the data to reciver
is_data_sent = esn.send_data(data)
if is_data_sent == False:
# this is the condition where the data havent sent sucssfully
# restart/ reboot the device in order to re send the data
# machine.reset()
print("ffaile")
gc.collect()
time.sleep(1)
# data has sent succefull and i need to decide wether need to go sleep or should be acitve
pwmr.hibernate(is_data_sent) # is_data_sent holds the status of the pup wether acitve or not
except Exception as e :
print('exception : ',e)
if exception_count == 0:
machine.reset()
pass
exception_count-=1
raise Exception
finally:
gc.collect()
time.sleep(1)
wdt.feed()