I'm trying to make a webserver display some data while the pico is collecting it from some pins. Everything works fine for making the webserver running, i'm able to access it with "static" data, but things get tricky having to loop for periodically collect data AND make the webserver running. I have tried to implement this using the _thread library as i've seen on many tutorials/projects, and made it the following to test if it is working (spoiler it's not) :
def serve(connection):
while True:
client = connection.accept()[0]
try:
html = webpage()
client.send(html)
except Exception as e:
print(f"Error serving client: {e}")
client.close()
...followed by all my methods for logic etc...
def GetSensorsValues():
#Here should be all my data inputs stored locally/updated
print('Test')
ip = connect()
connection = open_socket(ip)
_thread.start_new_thread(serve, (connection,))
while True:
GetSensorsValues()
sleep(5)
What i'm not really understanding is that without the call in the _thread the webserver works well, but threaded it is unreachable. I might be missing something on how this thread library works, i would appreciate some help ... Thanks for having considered my request !
Thanks for having taken the time to provide all those informations !
Actually i already went onto that forum were someone said that this library might be buggy, but this post was from 2022, and the microcontroller i'm using was also released that year so i thought that maybe it should me more stable today.
Anyway i just switched to using another library called microdot, and everything works fine.
Here is the github repo in case anyone is interested : https://github.com/spacecreep/Raspberry_pi_pico_w_weather_station
It is simply a basic website displaying realtime retrieved data from the pico using websockets.