async def main(client): # Called once on startup await client.connect() #await ntp() # any ADC pin 32-39 should work adc = machine.ADC(machine.Pin(36)) adc.atten(machine.ADC.ATTN_11DB) # 0V to 3.3V range adc.width(machine.ADC.WIDTH_10BIT) # 0 to 1023 bits read # Turn OFF blue led # ON Full means Wifi is DISCONNECTED # ON Weak means device has crashed blue_led(0) # Main loop that publishes to broker print("Monitoring water level...") while True: await asyncio.sleep(2) timestamp = str(utime.time()) current_raw_adc = adc.read() current_inches = conversion(current_raw_adc) #key_store.set(timestamp, str(current_water_level)) # If WiFi is down the following will pause for the duration. #await client.publish('devices/' + config['client_id'].decode('utf-8') + '/water/timestamp', timestamp, qos = 1) await client.publish('devices/' + config['client_id'].decode('utf-8') + '/water/raw_adc', str(current_raw_adc), qos=1) await client.publish('devices/' + config['client_id'].decode('utf-8') + '/water/inches', str(current_inches), qos=1)
async def main(client): # Called once on startup await client.connect() await ntp() # 2.5V Input Pin (any GPIO pin should work) pin = Pin(37, Pin.IN) # Initialize variables (assume power is currently on) current_power_status = 1 last_power_status = 1 counter = 0 # Turn OFF blue led # ON Full means Wifi is DISCONNECTED # ON Weak means device has crashed blue_led(0) # Main loop that publishes to broker print("Monitoring Power...") while True: await asyncio.sleep_ms(100) current_power_status = pin.value() # Local timestamps when power fails and is restored if current_power_status != last_power_status: timestamp = str(utime.time()) key_store.set(timestamp, str(current_power_status)) last_power_status = current_power_status # MQTT pings every 2 seconds of current power status if counter >= 2000: # elapsed milliseconds # If WiFi is down the following will pause for the duration. await client.publish('devices/' + config['client_id'].decode('utf-8') + '/power/value', str(current_power_status), qos=1) counter = 0 else: counter += 100 # Since we are waiting 100ms in asyncio.sleep_ms(100)
async def wifi_handler(state): blue_led(not state) await asyncio.sleep(1)