I have a campervan that runs a one Pi control screen for switching on the lights etc via a web interface. This works fine. I also had a Pi Zero running an a similar system which allowed control of the Vent fans from the front of the van mounted in the dash. Then I started to play with Picos. Learnt that they can run webservers and though, the system could do with an upgrade and really a Pico is ideal for the job where the Pi’s are a little overboard for some of the smaller systems. So rewrote one of the smaller systems for Pico and it works fine, UNTIL, you aren’t connected to the page anymore then it disconnects from the wifi after a short period and won’t reconnect when you go to access the page agian. The only way to make it active again is to restart the Pico.
As my van control system has multiple pages to use from various controllers, (for general switching, ventilation fans, tank level monitors that can be accessed from the one screen via fixed ip addresses), the pico is the only one that wont remain active while not being accessed.
I have a test network at home that only has the controller and programming computer on it so connected the Pico to it and the same thing happens. I grabbed another Pico, created a basic webserver code from an instructable site and, yes, when you stop accessing the site, the pico disconnects.
Has anyone else had the same problem. I have looked around google and apparently it is a common problem>
If this issue is common with Pico, I may switch over to an ESP32 for these simpler “none thinking” tasks
Thanks
john
import network
import socket
from time import sleep
from machine import Pin
import stag
import time
import ubinascii
led = machine. Pin('LED', machine. Pin. OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(stag.SSID, stag.PASSWORD)
ssid, password = (stag.SSID, stag.PASSWORD)
wlan.ifconfig(('192.168.1.221', '255.255.255.0', '192.168.1.1', '8.8.8.8'))
led_one = Pin(13, Pin.OUT) #led
led_two = Pin(11, Pin.OUT) #reverse
led_three = Pin(9, Pin.OUT) #slow
led_four = Pin(8, Pin.OUT) #fast
led_five = Pin(28, Pin.OUT) #close
led_six = Pin(27, Pin.OUT) #open
led_seven = Pin(26, Pin.OUT) #on/off
led.value(0)
def Website():
value_one = led_one.value()
value_two = led_two.value()
value_three = led_three.value()
value_four = led_four.value()
value_five = led_five.value()
value_six = led_six.value()
value_seven = led_seven.value()
website = """<!DOCTYPE html>
<html>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head> <title>Fan Control</title> </head>
<style>
*{background-color: black;
color:yellow;
font-family: Arial, Helvetica, sans-serif;
}
.button {
border: 2px;
border-style: solid;
border-color: yellow;
color: Yellow;
padding: 0px 20px;
text-align: center;
text-decoration: bold;
display: inline-block;
font-size: 28px;
margin-top:10;
margin: 14px 35px;
cursor: pointer;
border-radius: 6px;
width: 170px;
height: 90px;
background-color: blue;
}
.button:active {
color:red;
background-color: yellow;
transform: translateY(4px);
}
</style>
<body>
<table style="width:750px" class="center">
<tr>
<td></td>
<td><center> <h1><center>Fan Control </center></h1></center></td>
</tr>
<td><center><button class = "button" value='toggle' input type='button' onpointerdown='toggleLed("seven")' onpointerup='toggleLed("seven")' />ON/OFF</center></td>
<td><center><button class = "button" input type='hidden' onpointerdown='toggleLed("six")' onpointerup='toggleLed("six")' />OPEN</center></td>
<td><center><button class = "button" input type='hidden' onpointerdown='toggleLed("five")' onpointerup='toggleLed("five")' />CLOSE</center></td>
</tr>
<tr>
<td><center><button class = "button" input type='hidden' onpointerdown='toggleLed("four")' onpointerup='toggleLed("four")' />FASTER</center></td>
<td><center><button class = "button" input type='hidden' onpointerdown='toggleLed("three")' onpointerup='toggleLed("three")' />SLOWER</center></td>
<td><center><button class = "button" input type='hidden' onpointerdown='toggleLed("two")' onpointerup='toggleLed("two")' />REVERSE</center></td>
</tr>
<tr>
<td><center><button class = "button" input type='hidden' onpointerdown='toggleLed("one")' onpointerup='toggleLed("one")' />LED</center></td>
<td><center><button class = "button" input type='hidden' onpointerdown="window.location.href='http://10.10.10.10';" />MAIN SCREEN</center></td>
<td</td>
<tr>
</table>
<script>
function toggleLed(led){
var xhttp = new XMLHttpRequest();
xhttp.open('GET', '/led/'+led, true);
xhttp.send();
}
function update(){
location.reload(true);
}
</script>
</body>
</html>
"""
return website
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(stag.SSID, stag.PASSWORD)
max_wait = 10
print('Waiting for connection')
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
sleep(1)
status = None
if wlan.status() != 3:
raise RuntimeError('Connections failed')
else:
status = wlan.ifconfig()
print('connection to', ssid,'succesfull established!', sep=' ')
print('IP-adress: ' + status[0])
led.value(1)
ipAddress = status[0]
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
while True:
try:
cl, addr = s.accept()
print('Connection from ', addr, "accepted!")
request = cl.recv(1024)
request = str(request)
if request.find('/led/one') == 6:
led_one.toggle()
if request.find('/led/two') == 6:
led_two.toggle()
if request.find('/led/three') == 6:
led_three.toggle()
if request.find('/led/four') == 6:
led_four.toggle()
if request.find('/led/five') == 6:
led_five.toggle()
if request.find('/led/six') == 6:
led_six.toggle()
if request.find('/led/seven') == 6:
led_seven.toggle()
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(Website())
cl.close()
except OSError as e:
cl.close()
led.value(0)
print('connection closed')
led.value(0)
except KeyboardInterrupt:
machine.reset()