0

I am using Pico W as web server to control relays to switch on and off switches. I am able to connect pico w to wifi router. Web server works fine. After some idle time like 2-3 hrs, Pico stops responding.

I am suspecting, it is getting stuck at socket.accept as it is waiting for connection or there is some stack problem.

This idle time is not same everytime. Sometimes it stuck at 2-3hr and sometime it goes working for 7-8 hours

Restarting Pico W solves problem. micropython version used: 1.20

Can someone please help me to figure out the problem.

bedroomcontrol.py:

import socket
import machine
from style import webpage
from picozero import pico_led

def initRelays():
    global relay_light
    global relay_fan
    relay_light = machine.Pin(0,mode=machine.Pin.OUT)
    relay_fan = machine.Pin(1,mode=machine.Pin.OUT)
    relay_light.value(0)
    relay_fan.value(0)
    
def open_socket(ip):
    # Open a socket
    address = (ip, 80)
    connection = socket.socket()
    connection.bind(address)
    connection.listen(1)
    return connection

def serve(connection,wifi):
    #Start a web server
    global light
    global fan
    global color_light
    global color_fan
    global color_reset
    light = 0
    fan = 0
    color_light = "red"
    color_fan = "red"
    color_reset = "red"
    temperature = 0
    while True:
        client = connection.accept()[0]
        print(client)
        try:
            request = client.recv(1024)
        except:
            machine.reset()
        request = str(request)
        try:
            request = request.split()[1]
        except IndexError:
            pass
        print(request)
        requestHandler(request)
        html = webpage(temperature, color_light, color_fan, color_reset)
        client.send(html)
        client.close()
        
def requestHandler(request):
    global light
    global fan
    global color_light
    global color_fan
    global relay_light
    global relay_fan
    if request == '/light?':
        if light == 1:
            color_light = "red"
            relay_light.value(0)
            light = 0
        else:
            color_light = "green"
            relay_light.value(1)
            light = 1
    elif request == '/fan?':
        if fan == 1:
            color_fan = "red"
            relay_fan.value(0)
            fan = 0
        else:
            color_fan = "green"
            relay_fan.value(1)
            fan = 1
    elif request == '/off?':
        color_fan = "red"
        relay_fan.value(0)
        fan = 0
        color_light = "red"
        relay_light.value(0)
        light = 0
    elif request == "/reset?":
        machine.reset()

Connection.py

import network,machine
from time import sleep
from bedroomcontrol import open_socket, serve, initRelays
from picozero import pico_led

#Wifi Credentials
SSID = "********************"
SSID_PASSWORD  = "***************"
pico_led.off()


def do_connect():
    wifi = network.WLAN(network.STA_IF)
    if not wifi.isconnected():
        print('connecting to network...')
        wifi.active(True)
        wifi.connect(SSID, SSID_PASSWORD)
        while not wifi.isconnected():
            print("Attempting to connect....")
            sleep(3)
    ip = wifi.ifconfig()[0]
    pico_led.on()
    return ip,wifi
    
try:
    ip,wifi = do_connect()
    print(ip)
    initRelays()
    connection = open_socket(ip)
    serve(connection,wifi)
except KeyboardInterrupt:
    machine.reset()
  • When your Pico stops responding, is it still connected to your router? You might want to try disabling the WiFi power-saving mode on the Pico by putting `wlan.config(pm = 0xa11140) # Disable power-saving for WiFi` between the call to `wifi.active()` and `wifi.connect(SSID, SSID_PASSWORD)`. – Andrew Aug 28 '23 at 19:03
  • Hello @Andrew, Thanks a lot for your suggestion. I will surely give it a try and let you know if it worked. – Rohan Arora Aug 30 '23 at 07:11

0 Answers0