In the python code for web server for an LED button, these 2 commands appear in main(): conn, addr = s.accept() and conn.settimeout(3.0). I understand that the accept() is blocking and the code hangs there until a client appears. What is the purpose of the settimeout() and what does it do (presumeably after a client appears) and the program advances to this line?? (it appears in some Web Servers examples found on github for the book, but not others (e.g. the DHT22 one). The code presented in the book doesn’t have these settimeout() lines after the accept()
Hi.
That is because Google Chrome sometimes makes an extra request and lefts the connection open. While the connection is opened, no other client can connect. So, if the connection is not closed after three seconds, it means its probably one of those problematic connections, so we need to close it manually.
Regards,
Sara
Thanks Sara, that’s helpful. … I’m comparing the C-code example in the “Learn ESP32 with Arduino IDE” book and the python-code example in the “Micropython ESP32 and ESP82665” book, for the Webserver/button example. I see that the C-code is more than double in size (… why would I use C if python is so much more compact and readable?), but it looks like, with the C-code’s while(client.connected and “time checking”) statement this does the same thing as the settimeout(3) python statement… correct? ….
Hi.
Yes, that’s correct.
I think it depends on the programming language you like more and the resources available for each language.
For example, there are many more resources for C with the ESP32 than MicroPython (libraries, functions, forums, and tutorials on the internet).
Programming the ESP32 with Arduino IDe using the Arduino core provides much more flexibility and customization in terms of programming and what is possible to do.
On the other side, using MicroPython is way simpler for someone new to programming or someone already used to programming in Python. There are simpler things to program in MicroPython than in C (for example, controlling an addressable RGB LED strip). But, in some scenarios, it is much more limited.
I hope this answers your question.
Regards,
Sara
Thanks Sara … very helpful as I navigate using python, c or javascript in webservers