I´m looking for a mqtt button script for Nodemcu esp32, there connects to mqtt broker and post the topic and the payload and after goes to deep sleep mode, and first wakes up then the button got push.
Can you please help me to create a script like that.
I have here a link for a script for esp8266 there works like I want to have it but without deepsleep mode.
How can I convert it for esp32 and add deepsleep mode for the script.
Link to script: https://github.com/davolesh/mqtt_pushbutton/blob/master/mqtt_button_basement.ino?fbclid=IwAR3B8iLZdNf16XgTiUE6NKHuppoTlJSq4NfY747nNRQofyYWjomnEpMlOF4
That example uses the PubSubClient library that should be compatible with the ESP32. So, I think you don’t need to change anything about that.
I recommend you test that script with the ESP32 and see if it works.
Then, because you want to put the ESP32 in deep sleep and wake it up with a button, you need to add an external wake-up.
Move all the code to the setup() function.
Then, enable external wake up with the following:
This function accepts as first argument the pin you want to use, in this format GPIO_NUM_X, in which X represents the GPIO number of that pin.
The second argument, level, can be either 1 or 0. This represents the state of the GPIO that will trigger wake-up.
Note: you can only use pins that are RTC GPIOs with this wake-up source.
Finally, call the following function to start deep sleep:
All of this is explained in this unit: https://rntlab.com/module-3/deep-sleep-external-wake-up/