Hi guys,
is it possible to subscribe to a MQTT topic and collect messages every time the ESP32 wakes up from deep sleep mode?
I'm using Arduino IDE and loop function is not used in deep sleep mode and cannot understand where I should insert the client.loop() call: in the setup?
I'm using this library: https://pubsubclient.knolleary.net/api.html
Thanks,
Max
How to receive MQTT messages after deep sleep?
Re: How to receive MQTT messages after deep sleep?
Hi Max,
The thing to remember is, when using the arduino framework and deep sleeping, your sketch only runs the setup() section and never runs the loop() part.
So, one (pseudocode) option would look something like this
at some point in do_cool_things() you'd have to decide you'd been awake long enough (maybe a counter) , and set readyToSleep=true;
the code will call client.loop() one last time and then go back to sleep.
cheers,
Dan
The thing to remember is, when using the arduino framework and deep sleeping, your sketch only runs the setup() section and never runs the loop() part.
So, one (pseudocode) option would look something like this
Code: Select all
bool readyToSleep=false;
void setup() {
// I'm awake from sleep
esp_sleep_enable_timer_wakeup(100000000);
setup_networking();
subscribe_topics();
while(!readyToSleep){
do_cool_things();
client.loop(); //Ensure we've sent & received everything
}
esp_deep_sleep_start();
}
void loop(){
// Usually won't get here
}
the code will call client.loop() one last time and then go back to sleep.
cheers,
Dan
Re: How to receive MQTT messages after deep sleep?
Hi,
Has the problem been solved? Because I am having the same issues. I am following the pseudocode suggested by @danwat, but it is still not working.
I am using the WebSocket UI inside CloudMQTT to publish some data to a topic. If the message is published when the ESP32 is sleeping, after the ESP32 wakes up, it doesn't pull this published data from the subscribed topic.
Has the problem been solved? Because I am having the same issues. I am following the pseudocode suggested by @danwat, but it is still not working.
I am using the WebSocket UI inside CloudMQTT to publish some data to a topic. If the message is published when the ESP32 is sleeping, after the ESP32 wakes up, it doesn't pull this published data from the subscribed topic.
Code: Select all
#include <WiFi.h>
#include <PubSubClient.h>
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 30 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
const char* ssid = "XXXXXXXX";
const char* password = "XXXXXXXXXXXXX";
const char* mqttServer = "postman.cloudmqtt.com";
const int mqttPort = XXXXX
const char* mqttUser = "XXXXXXXX;
const char* mqttPassword = "XXXXXXXXXX";
String temp_str;
char temp[50];
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.subscribe("esp/test");
for(int i=0;i<10; i++)
{
client.loop(); //Ensure we've sent & received everything
delay(100);
}
Serial.print("Going to Sleep...");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void loop() {
}
Re: How to receive MQTT messages after deep sleep?
I know it has been a while, but this worked for me on an ESP8266.
essential though that you persist the MQTT message so it is still there when your esp wakes up
https://wp.me/p2ffrb-11V
essential though that you persist the MQTT message so it is still there when your esp wakes up
https://wp.me/p2ffrb-11V
Who is online
Users browsing this forum: Baidu [Spider], Google [Bot] and 107 guests