I came to test the Anvil.Works solution first when I bought a Pico W a few weeks ago. Anvil offers a great way to create a simple cloud connected IOT device out of the Pico W.
Unfortunately the Uplink runs for several hours but then breaks. When it does, it seems to reconnect, but the running task does not recover. This is clear from the state of the blinking led and an end to any further recorded activity.
My current Pico W side code is pasted below:
import anvil.pico
import utime, time
import onewire, ds18x20
import uasyncio as asyncio
from machine import Pin
from machine import ADC
# the onewire device is on GPIO22
dat = machine.Pin(22)
# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))
#scan bus to find sensors
roms = ds.scan()
# This is an example Anvil Uplink script for the Pico W.
# See https://anvil.works/pico for more information
SERVER_UPLINK_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# We use the LED to indicate server calls and responses.
led = Pin("LED", Pin.OUT, value=1)
async def pico_log():
while True:
created = utime.localtime()
seconds = created[5]
led.toggle()
await asyncio.sleep(0.5)
#drop through on minute boundary
if ((seconds % 60) == 0):
# read onboard coretemp sensor
sensor_temp = machine.ADC(ADC.CORE_TEMP)
conversion_factor = 3.3 / (65535)
reading = sensor_temp.read_u16() * conversion_factor
coretemp = 27 - (reading - 0.706)/0.001721
#onewire read roomtemp based on initial bus scan
ds.convert_temp()
await asyncio.sleep(0.75)
roomtemp = ds.read_temp(roms[0])
#server call to log data in database
log = await anvil.pico.call('add_record', coretemp, roomtemp, created)
print(f"Roomtemp: {roomtemp} @ {created} - {log}")
#pause to ensure we do not retrigger during same-second
await asyncio.sleep(0.5)
# Connect the Anvil Uplink. In MicroPython, this call will block forever.
anvil.pico.connect(SERVER_UPLINK_KEY, on_first_connect=pico_log())
# There's lots more you can do with Anvil on your Pico W.
I cannot find much advice on diagnosing this problem and wonder if others have found a way around it? I have the 0.1.2 FW running on the Pico W.
Steve