I’m working on a WoL script for my Raspberry Pi Pico W. The goal is to be able to wake up my pc from anywhere using a request to the right IP and webpage.
Currently, I’m having an issue with the pico unexpectedly stopping to respond to any tcp request after a while.
Here is what I’ve tried to fix the issue:
- Make sure the network power management plan is set to
WLAN.PM_NONE
- Make sure the time is set properly using
ntptime
- Tried to call
machine.reset()
every 2 hours
Here is a MRE:
import ntptime,network,time
from machine import Pin
import uasyncio as asyncio
SSID = '' # Put your WifI SSID
PASS = '' # Put your WiFi password
html = """<!DOCTYPE html>
<html>
<head> <title>Hello</title></head>
<body>
<button onclick="turnOn()">Turn On</button>
<script>
function turnOn(){
fetch("/wakeup");
}
</script>
</body>
</html>
"""
ROOT="/"
WOL_ROUTE="/wakeup"
onboard = Pin("LED", Pin.OUT, value=0)
def connect_to_network():
wlan = network.WLAN(network.STA_IF)
wlan.config(pm=network.WLAN.PM_NONE)
wlan.active(True)
wlan.connect(SSID, PASS)
while wlan.isconnected() == False:
time.sleep(1)
print(f"Local IP: {wlan.ifconfig()[0]}")
ntptime.settime()
print("Time synced")
async def serve_client(reader, writer):
addr,port = reader.get_extra_info("peername")
print('Got a connection from %s:%s' % (addr,port))
request_line = await reader.readline()
# We are not interested in HTTP request headers, skip them
while await reader.readline() != b"rn":
pass
request = request_line.decode("utf-8")
method,route,protocol = request.strip().split(" ")
print("Method: %s" % method)
print("Route: %s" % route)
print("Protocol: %s" % protocol)
if route == WOL_ROUTE:
print("Sending magic packet")
# [...] WoL is working no need to include it here
writer.write('HTTP/1.0 200 OKrnContent-type: application/jsonrnrn')
writer.write('{"status":"switched on"}')
elif route == ROOT:
print("Sending WebPage")
writer.write('HTTP/1.0 200 OKrnContent-type: text/htmlrnrn')
writer.write(html)
await writer.drain()
await writer.wait_closed()
print("Client disconnected")
async def main():
connect_to_network()
asyncio.create_task(asyncio.start_server(serve_client, "0.0.0.0", 80))
while True:
onboard.on()
await asyncio.sleep(.25)
onboard.off()
await asyncio.sleep(4.75)
try:
asyncio.run(main())
finally:
asyncio.new_event_loop()