ESP8266 how to scatter out the delays() properly
Posted: Sat Mar 27, 2021 8:12 am
According to an article:
I have a HTTP server code running on an ESP8266 with a huge delay(1000) in the middle of the main loop. Now I understand why is it required however I still believe that this is slowing down my code and also the TCP/IP stack does not operate efficiently.
For example here is an average ping from the ESP being right next to the access point:
As it shows there are huge lag spikes and packet loss.
I have also tried to put in yields() in different parts of the code, it does not change much. As I understand this is essentially a delay(0).
Isn't there another proper way to do this? Like call whatever function to take care of the ESPs networking needs whether that takes 10mS 20mS or 500mS then return to the main loop to work on the main code?
Thanks!
Code: Select all
Timing and delays
millis() and micros() return the number of milliseconds and microseconds elapsed after reset, respectively.
delay(ms) pauses the sketch for a given number of milliseconds and allows WiFi and TCP/IP tasks to run. delayMicroseconds(us) pauses for a given number of microseconds.
Remember that there is a lot of code that needs to run on the chip besides the sketch to keep an existing WiFi connection alive. WiFi and TCP/IP libraries get a chance to handle any pending events each time the loop() function completes, OR when delay() is called. If you have a loop somewhere in your sketch that takes a lot of time (>50ms) without calling delay(), you might consider adding a call to delay function to keep the WiFi stack running smoothly.
There is also a yield() function which is equivalent to delay(0). The delayMicroseconds function, on the other hand, does not yield to other tasks, so using it for delays more than 20 milliseconds is not recommended.
I have a HTTP server code running on an ESP8266 with a huge delay(1000) in the middle of the main loop. Now I understand why is it required however I still believe that this is slowing down my code and also the TCP/IP stack does not operate efficiently.
For example here is an average ping from the ESP being right next to the access point:
Code: Select all
64 bytes from 192.168.1.104: seq=2568 ttl=255 time=104.947 ms
64 bytes from 192.168.1.104: seq=2570 ttl=255 time=51.506 ms
64 bytes from 192.168.1.104: seq=2572 ttl=255 time=107.570 ms
64 bytes from 192.168.1.104: seq=2574 ttl=255 time=44.578 ms
64 bytes from 192.168.1.104: seq=2576 ttl=255 time=94.083 ms
64 bytes from 192.168.1.104: seq=2578 ttl=255 time=37.497 ms
64 bytes from 192.168.1.104: seq=2580 ttl=255 time=87.996 ms
64 bytes from 192.168.1.104: seq=2582 ttl=255 time=36.203 ms
64 bytes from 192.168.1.104: seq=2584 ttl=255 time=80.566 ms
64 bytes from 192.168.1.104: seq=2586 ttl=255 time=14.936 ms
64 bytes from 192.168.1.104: seq=2588 ttl=255 time=66.418 ms
64 bytes from 192.168.1.104: seq=2590 ttl=255 time=14.558 ms
64 bytes from 192.168.1.104: seq=2592 ttl=255 time=60.157 ms
64 bytes from 192.168.1.104: seq=2594 ttl=255 time=110.632 ms
64 bytes from 192.168.1.104: seq=2596 ttl=255 time=58.076 ms
64 bytes from 192.168.1.104: seq=2598 ttl=255 time=110.749 ms
64 bytes from 192.168.1.104: seq=2600 ttl=255 time=61.434 ms
64 bytes from 192.168.1.104: seq=2604 ttl=255 time=46.181 ms
64 bytes from 192.168.1.104: seq=2606 ttl=255 time=107.223 ms
64 bytes from 192.168.1.104: seq=2608 ttl=255 time=6.172 ms
64 bytes from 192.168.1.104: seq=2610 ttl=255 time=79.259 ms
64 bytes from 192.168.1.104: seq=2612 ttl=255 time=31.355 ms
64 bytes from 192.168.1.104: seq=2614 ttl=255 time=74.469 ms
64 bytes from 192.168.1.104: seq=2616 ttl=255 time=42.742 ms
64 bytes from 192.168.1.104: seq=2618 ttl=255 time=81.456 ms
64 bytes from 192.168.1.104: seq=2620 ttl=255 time=15.986 ms
64 bytes from 192.168.1.104: seq=2622 ttl=255 time=67.207 ms
64 bytes from 192.168.1.104: seq=2624 ttl=255 time=14.543 ms
64 bytes from 192.168.1.104: seq=2626 ttl=255 time=58.151 ms
64 bytes from 192.168.1.104: seq=2627 ttl=255 time=1116.019 ms
64 bytes from 192.168.1.104: seq=2628 ttl=255 time=122.666 ms
--- 192.168.1.104 ping statistics ---
2629 packets transmitted, 1248 packets received, 52% packet loss
round-trip min/avg/max = 1.714/134.133/2966.534 ms
As it shows there are huge lag spikes and packet loss.
I have also tried to put in yields() in different parts of the code, it does not change much. As I understand this is essentially a delay(0).
Isn't there another proper way to do this? Like call whatever function to take care of the ESPs networking needs whether that takes 10mS 20mS or 500mS then return to the main loop to work on the main code?
Thanks!