Folks,
I have a Pico WH running a weather station, using wi-fi to connect to my home network. The Pico hosts a simple HTML page that we access for weather. On start up the Pico establishes a connection via connect.py, then the serve function uses a while true loop to wait for a request from a webpage.
The typical signal strength on the Pico is ~-70 dMm and so dropouts are common.
My quandary is about what’s best practice for renewing a wi-fi connection. Options as a I see them are:
A) Monitor the connection and if lost, renew.
B) Automatically reboot the Pico on a regular basis.
My issue with both of these is that the while true loop is always waiting for a request.
Do I simply add some “If connected else” code in the first line of the while true loop?
The serve function.
def serve(connection):
print("serve function running")
while True:
client = connection.accept()[0] # continuously waits for incoming client connection.
request = client.recv(1024)
# Define the values.
bme280_sensor()
Ctime = current_time()
temp = tempC
rh = humRH
pres = preshPa
dp = dewpoint()
Pmsg = pressure_msg()
bvolts = batt_volt()
bperc = batt_perc()
cpower = batt_p_status()
bcharge = batt_c_status()
sigs = signal_strength()
HIc = heat_index()
uvi = uv_index()
uvi_risk = risk
# Send to the page.
html=webpage(tempC, preshPa, humRH, dp, Ctime, Pmsg, bvolts, bperc, cpower, bcharge, sigs, HIc, uvi, uvi_risk) # Gets the data from the webpage function.
client.send(html) # sends the generated HTML back to the client.
client.close() # closes the client connection.
# Cycle the Pico LED each refresh.
Picoled.value(1)
sleep_ms(500)
Picoled.value(0)
sleep_ms(2000)
The connect program.
import network
from secrets import secrets.
import time
def do_connect(ssid=secrets['ssid'],psk=secrets['password']): # Gets the info from the secrets.py
wlan = network.WLAN(network.STA_IF) # Initialize the Wi-Fi interface.
wlan.active(True) # Activate the interface.
wlan.connect(ssid, psk)
print(wlan.isconnected()) # Check the connection status.
# Wait for connect or fail
wait = 10
while wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('wifi connection failed')
# raise is an exception event that stops the program.
# RuntimeError is a built-in exception that is raised when an error is detected that doesn't fall into any of the other specific...
# categories of exceptions. It generally indicates that something went wrong during the execution of the program,
# but it doesn't provide details about what exactly failed.
else:
print('connected')
ip=wlan.ifconfig()[0]
print('network config: ', ip)
# Get the current SSID
ssid_name = wlan.config('essid')
print('Connected to:', ssid_name)
# Get the RSSI (signal strength) of the connected network
rssi = wlan.status('rssi')
print(f"Signal strength (RSSI): {rssi} dBm")
return ip # Exit and return the ip address.
# Get the RSSI (signal strength) of the connected network for display on the w/station.
def signal_strength():
wlan = network.WLAN(network.STA_IF) # Initialize the Wi-Fi interface.
rssi = wlan.status('rssi')
return rssi
# Get the IP address of the connected network for display on the w/station.
def ip_add():
wlan = network.WLAN(network.STA_IF) # Initialize the Wi-Fi interface.
ip=wlan.ifconfig()[0]
return ip