I've just received my Pico W this week and want to setup what I think is probably a very common use case scenario for this board but I'm really struggling with HTTP POST requests.
I'm trying to periodically send a POST request to an API which I have made to receive and log the data so that I can remotely monitor various captured data. At this stage I'm literally just trying to make it regularly ping the server.
The Pico W successfully pings the endpoint and logs that it has done so but the HTTP statuscode returned is 426 which is something to do with "Needs Upgrade" - which I do not understand.
When I test the endpoint (A simple FastAPI test server at this stage) from curl or any other HTTP request generator it works fine and increments the counter on the API (which I can monitor by visiting https://results.up.railway.app/pico in browser.
I've also tested other API POST endpoints and they all return some flavour of 4xx error.
What is wrong with the requests generated by the PicoW with urequests? I'm struggling to work it out.
Code: Select all
import time, network, machine
import urequests as request
# Boot sequence to show it is on
led = machine.Pin("LED", machine.Pin.OUT)
led.on()
time.sleep(3) # LED on for 3-secs on boot
led.off()
# Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('xxxx', 'xxxx')
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
led.toggle()
time.sleep(0.1)
led.toggle()
time.sleep(0.1)
led.toggle()
time.sleep(0.1)
led.off()
# Callback function to ping server
def ping_fastapi(timer):
timestamp = machine.RTC().datetime()
timestring="%04d-%02d-%02d %02d:%02d:%02d" %(timestamp[0:3] + timestamp[4:7])
r = request.post("http://results.up.railway.app/pico/")
print(timestring + " Status Code:" + str(r.status_code) )
led.on()
time.sleep(0.2)
led.off()
r.close()
pass
tim = machine.Timer()
tim.init(period=10000, mode=machine.Timer.PERIODIC, callback=ping_fastapi)