Rasbperry Pi Pico W code not running on reboot (does on soft reboot). #13554
Unanswered
Tom-Camp
asked this question in
RP2040 / Pico
Replies: 1 comment
-
import ahtx0
import secrets
import uasyncio
import ujson
import urequests
from machine import Pin, I2C
from lib.lighting import Lighting
from lib.stemma_soil_sensor import StemmaSoilSensor
lights = 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) + 32
def 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_data
def 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() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions