I posted this on the Micropython forum as well, but I have some code that is running on a Raspberry Pi Pico which runs an AHT20 temperature and humidity sensor, a seesaw soil sensor, and some LEDs. The code sends a request to an API which records the sensor data and tells it to turn on the lights (or turn them off). It all runs as expected, except... it doesn't start on device reset/restart. If I do soft reboot it does run. I've moved the network connection into the boot.py and then the rest runs (mainly) in main.py.
main.pyboot.py
main.py
Code:
import ahtx0import secretsimport uasyncioimport ujsonimport urequestsfrom machine import Pin, I2Cfrom lib.lighting import Lightingfrom lib.stemma_soil_sensor import StemmaSoilSensorlights = Lighting(count=secrets.PIXEL_COUNT, pin=secrets.LIGHT_PIN)air_i2c = I2C(secrets.I2C_ID, sda=Pin(secrets.SDA_PIN), scl=Pin(secrets.SCL_PIN), freq=20000)soil_i2c = I2C(secrets.I2C_ID, sda=Pin(secrets.SDA_PIN), scl=Pin(secrets.SCL_PIN), freq=20000)def get_profile(colors: list) -> list: return [tuple(color_set) for color_set in colors]def c_to_f(c_temp: float) -> float: return (c_temp * 9 / 5) + 32def read_sensors() -> dict: air_sense = ahtx0.AHT20(air_i2c) soil_sense = StemmaSoilSensor(soil_i2c) sensor_data: dict = { "air_temp": c_to_f(air_sense.temperature), "humidity": air_sense.relative_humidity, "moisture": soil_sense.get_moisture(), "soil_temp": c_to_f(soil_sense.get_temp()), } return sensor_datadef post_data(data: dict) -> int: sensor_post: dict = { "project_id": secrets.PROJECT_ID, "sensor_data": ujson.dumps(data), } try: response = urequests.post( url=f"{secrets.API_URL}/data", headers={"Content-Type": "application/json"}, data=ujson.dumps(sensor_post), ) return response.status_code except Exception as e: print(e)def get_status() -> dict: try: response = urequests.get( url=f"{secrets.API_URL}/projects/{secrets.PROJECT_ID}/status", headers={"Content-Type": "application/json"} ) return response.json() except Exception as e: print(e)async def run_sensors(): while True: sensor_data = read_sensors() post_data(sensor_data) await uasyncio.sleep(60)async def run_lights(): while True: status_data = get_status() print(status_data) if status_data: profile = get_profile(status_data.get("colors", [(255, 0, 0)])) status = status_data.get("status", False) if status and not lights.status: lights.turn_on_all(profile=profile) elif not status and lights.status: lights.turn_off() await uasyncio.sleep(60)event_loop = uasyncio.get_event_loop()event_loop.create_task(run_sensors())event_loop.create_task(run_lights())event_loop.run_forever()
Code:
import networkimport secretsimport timewlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(secrets.SSID, secrets.PASSWORD)max_wait = 10while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 0: break max_wait -= 1 print("Waiting for connection...") time.sleep(1)if wlan.status() != 3: raise RuntimeError("Network connection failed")else: print("Connected") status = wlan.ifconfig() print(f"IP: {status[0]}")
Statistics: Posted by TomCamp — Tue Jan 30, 2024 12:50 am