Hi.
I am trying to modify your tutorial ”
ESP8266 NodeMCU MQTT – Publish BME280 Sensor Readings (Arduino IDE)
By adding Deep Sleep. I implemented the tutorial as written on a esp8266 D1 mini board and it worked just fine. I then removed the millisecond timer code that repeated the loop and put all the loop code in Setup then added the deep sleep command at the end of setup. The program compiles and runs but will not connect to MQTT broker. It just reads the sensor and attempts to publish every time it wakes up.
I don’t see what is different now that causes it to not connect. Any ideas would be welcome. Thanks
Rick
Hi Rick.
Can you share your code?
Maybe I can find something just by taking a look at it.
To share your code, share a link to google drive, dropbox, GitHub, Pastebin, or another service that is suitable for you.
Regards,
Sara
Hi Rick.
Basically, you need to add a delay() after calling the function to connect to Wi-Fi and a delay() before going to deep sleep. Otherwise, it doesn’t have time to execute the functions.
Here is the working code: https://gist.github.com/sarasantos/fa4cdd5012c296fbf41141a2f45c5765
I’ve just tested it, and it works.
Don’t forget to connect the RST pin to D0 after uploading the code so that the ESP8266 can wake up from sleep.
Let me know if this works for you.
Regards,
Sara
Hi Sara;
I added the delays and now it is working. Thank you for looking it over; I guess am still not understanding what is happening here. I have been under the impression that delay is a blocking function and prevents anything else from happening until the delay completes. Is this not true? If the delay is needed to wait while something else is finishing then it is not truly blocking. Are the callback functions using interrupts?
Rick
Hi Rick.
I’m also not sure what is happening.
delay() is a blocking function. But that doesn’t mean that you shouldn’t use it in some cases. As you’ve seen, sometimes, it is needed. However, you can always replace those delays with timers.
The MQTT library we’re using is asynchronous. So, I guess it keeps doing its tasks while we’re doing other things on the code.
However, I’m not sure If my explanation is correct.
Regards,
Sara