How to receive message from the cloud when using deep sleep

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

How to receive message from the cloud when using deep sleep

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

Hi all,

I am quite new to ESP32 and would need some advise. My project requirements are as follows:

I have an ESP32 that goes to deep sleep and wakes up every one minute to check if there is any new data to receive from the cloud. On the other end, I have a mobile app that sends a message to the cloud. This message can be sent randomly at any time.

Previously, I was implementing this using MQTT. The mobile app publishes to a topic in CloudMQTT while the ESP32 is permanently connected to the MQTT broker and subscribes to this same topic. However this method only works it if I am constantly connected to the broker.

What is a better solution if I want to be able to sleep the ESP32 and wake up to check for new data inside the cloud? Thanks!

*Edited: Sorry, just realised that my question is similar to this:
https://www.esp32.com/viewtopic.php?f=2&t=10944

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: How to receive message from the cloud when using deep sleep

Postby WiFive » Wed Jul 10, 2019 2:28 pm

You have to use the correct qos and clean session settings

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

Re: How to receive message from the cloud when using deep sleep

Postby kian79 » Wed Jul 10, 2019 11:44 pm

Thanks @WiFive !

I google and found more information. I will try it out soon.

https://github.com/knolleary/pubsubclient/issues/86

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

Re: How to receive message from the cloud when using deep sleep

Postby kian79 » Thu Jul 11, 2019 11:10 am

Hi all,

I tried to allow persistent connections and also enable QOS level 1 but its not working.

I am connected to the broker (on CloudMQTT) using this line:

client.connect("ESP32Client", mqttUser, mqttPassword,"lastwill",1,0,"Disconnect",0 )

And I am subscribing to a topic with QOS 1:

client.subscribe("mytopic",1);

I am using an ESP32 with Arduino IDE and my ESP32 goes to deep sleep every 30 seconds, wakes up and stays awake for 10 seconds. From the Websocket UI on CloudMQTT, I can publish to a topic. If I am publishing during the 10 seconds when the ESP32 is awake, I can receive data from the topic i subscribed to. However if I publish during the time when the ESP32 is in deep sleep, when it wakes up, it is not receiving any data.

Do you know what can be wrong? I am attaching my codes for reference:

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) */

const char* ssid = "XXXXXXX";
const char* password = "XXXXXXXXX";
const char* mqttServer = "postman.cloudmqtt.com";
const int mqttPort = XXXXXXX;
const char* mqttUser = "XXXXXXXX";
const char* mqttPassword = "XXXXXXXXXXX";

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,"lastwill",1,0,"Disconnect",0 )) 
{
   Serial.println("connected");  
 } else {

  Serial.print("failed with state ");
  Serial.print(client.state());
  delay(2000);
 }
}

client.subscribe("mytopic",1);

for(int i=0;i<100; 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() {
}

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: How to receive message from the cloud when using deep sleep

Postby WiFive » Thu Jul 11, 2019 6:29 pm

Are you also publishing with qos 1

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

Re: How to receive message from the cloud when using deep sleep

Postby kian79 » Thu Jul 18, 2019 2:35 am

Hi WiFive,

Thank you for the reply. Indeed that was the problem. The WebSocket UI on CloudMQTT does not allow publishing with QOS 1. I have used another program to connect to CloudMQTT and I can now publish with QOS 1 and the ESP32 setup can now receive messages when it wakes up from deep sleep and connects to the MQTT broker to check for any missed messages.

I have one slight problem though. The last sent message is still there in the topic even though I have read it. It doesn't go away. So everything I wake up the ESP32 from deep sleep it is still receiving the last posted messaged to the topic. Is there anyway it can be removed once I have successfully received and read it?

Thanks in advance.

Who is online

Users browsing this forum: Bryght-Richard and 127 guests