- Mon Mar 07, 2022 9:09 pm
#93845
The ESP8266 chip has a lot of things it has to do down in the lower levels when not under your control while in either the setup() or loop() methods. If you keep it too long, those other things can't get done. That's why they have the watch dog... you abuse the lower level, it gives you the finger and reboots on you. So anything longer than single digit milliseconds really ought to be asnyc. Happy dogs don't bite.
So for you to get work done you can...
case 1... A BMP280 pressure sensor wants you to initialize it and it returns a time that indicates when the answer will be ready to pull from the sensor. I want to say it was in the 5-10 ms range. For a lazy weather station project, where a reading once a minute is overkill, you can easily do synchronous - call init(), delay(10), read() all in the loop method and call it good.
case 2 - Another project where I needed pressure readings on two sensors at a 100 Hz while doing a lot of other things including running a webserver and multiple websockets. In that case. I called init() returned from the loop... letting the lower level work. When it came back in the loop I did something else and returned. After thousands of these (do something) loops (10 milliseconds had passed) and I could then do the read() on the first sensor.
That is very painful coding... the better way is setting up events.
With anything client/server related you might as well assume you must use async. Even in the same room a good turn around for a message may be a couple of milliseconds, but I've also seen 400ms in the same room. If the client is half way around the world, it will be far more than the watch dog will tolerate. Besides if you are holding the loop method, the lower levels can't send or receive any traffic. You have the CPU.
You asked specifically about an async web server and does it interrupt the loop. Not like a true software interrupt. Not... really... it's just expecting you to return from the loop to let it do its work. It says... I have a new message that came in and I'll fire the even that finally bubbles up to you. So yes, it comes to you between loop calls, but never while you have control in your loop method.
I don't know if that is clear.