How to receive MQTT messages after deep sleep?

Maxyct
Posts: 1
Joined: Sun May 26, 2019 4:44 pm

How to receive MQTT messages after deep sleep?

Postby Maxyct » Mon Jun 10, 2019 11:28 pm

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

danwat
Posts: 1
Joined: Sat Jun 15, 2019 5:27 am

Re: How to receive MQTT messages after deep sleep?

Postby danwat » Sat Jun 15, 2019 5:57 am

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

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
}
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

kian79
Posts: 28
Joined: Thu Jun 13, 2019 3:27 am

Re: How to receive MQTT messages after deep sleep?

Postby kian79 » Wed Jul 10, 2019 8:39 am

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.

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() {
 
}

ed2000
Posts: 1
Joined: Sun Jan 26, 2020 11:41 am

Re: How to receive MQTT messages after deep sleep?

Postby ed2000 » Sun Jan 26, 2020 11:46 am

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

Who is online

Users browsing this forum: No registered users and 117 guests