ESP32 not updating ArduinoCloud after esp_deep_sleep

Hi,

I am trying to set up an IoT showing the value of the outside temperature (measured with a Dallas temperature sensor) at regular intervals.

That works if I leave the ESP32 on, however battery does not last long, therefore I am trying to make it go in deep sleep mode between measurements & updates, and although the correct temperature measurement is still done (as visible on the Serial Monitor), the dashboard is not updated (and only the very first value is displayed).

Here is my code:

#include "thingProperties.h"
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 14 on the Arduino
#define ONE_WIRE_BUS 14

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  10       /* Time ESP32 will go to sleep (in seconds) */

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
  
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep every " + String(TIME_TO_SLEEP) + "s");
  
}

void loop() {
  
  ArduinoCloud.update();
  
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  celcius = sensors.getTempCByIndex(0);
  celcius = int(celcius*100.+0.5)/100.;

  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(celcius);
  
  esp_deep_sleep_start();
  
}

What am I doing wrong?
Can someone help me sort this out?

Thanks,

AtKhA

Is there nobody here that has any clue?

I tried to move the section about //Connect to Arduino IoT Cloud from the setup to the loop so that it would try to connect every time, which it does, however there is still no update on the dashboard

Is there something incompatible between the arduino iot cloud and ESP32?

Am I the only one having these issues?

First thing you have to make sure that works is the ESP32 actually wakes up after you put it into sleep. Can you verify that the ESP32 wakes up?

Also, a deep sleep in ESP32 terms is almost the same as a RESET. So you might have to reconnect to Arduino Cloud after waking up, before you update the dashboard with new values

Hi @hzrnbgy

Yes, the ESP32 wakes up after deep sleep and does the temperature measurement correctly.
It also reconnects to my local network, so I thought it was connecting to the Arduino cloud also, but that obviously fails.
I have tried to add delays, thinking it had not had sufficient time, but without more success.
What can I do/test to further debug the problem?

Well, you could write code that instead of going to sleep, disconnects and reconnects to the WiFi things. That way you can simulate part of waking up and going to sleep by getting the connect and disconnect working.

You could try properly disconnecting before just shutting down.

You have these in setup()

Maybe, after waking up, you also need to execute the above. After all, your ESP32 has "resetted" after waking up

ESP32 DeepSleep is an almost reset of the ESP32. The preserved bits are any RTC RAM the programer has setup and the ULP processor code, if any. An ESP32 waking from deep sleep causes the setup() to be re-ran.

Hey @atkha

Did you ever find a solution to this?

I'm running into the same issue.. the thing is, I can see that after ESP.deepSleep(10e6) my device wakes up, connects to the network (confirmed) takes a reading from my dht22, then enters deep sleep once more etc.

I can see from the "Devices" tab in IoT Cloud that the device is registered every ~10 seconds as expected, but the values from the temp / humidity never seem to make it into the cloud. :frowning:

my code for ref:

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  pinMode(DHTPin, INPUT);
  dht.begin();
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  while(!ArduinoCloud.connected())
  {
    ArduinoCloud.update();
    delay(1000);
  }
  int tempAvg = 0;
  int humidAvg = 0;
  
  for(int x = 0; x < 10; x++)
  {
    tempAvg += dht.readTemperature();
    humidAvg += dht.readHumidity();
  }
  temp = tempAvg / 10; // temp = Cloud Variable
  humid = humidAvg / 10; // humid = Cloud Variable
  Serial.println(temp);
  Serial.println(humid);
  ArduinoCloud.update();
  delay(1000);
  ESP.deepSleep(10e6);
  delay(1000);
}

I faced the same problem when using esp8266.
After experimenting with the sketch, I came to the conclusion that apparently the ArduinoCloud.update(); requires several cycles. This code worked for me:

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <OneWire.h>
OneWire  ds(0); // pin D3

const char THING_ID[]           = " ";
const char DEVICE_LOGIN_NAME[]  = " ";

const char SSID[]= " ";    // Network SSID (name)
const char PASS[] = " ";    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = " ";    // Secret device password

static int temperature;

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

void setup() {
  Serial.begin(9600);
  while(!Serial);
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addProperty(temperature, READ, 15 * SECONDS, NULL);
  
ArduinoCloud.begin(ArduinoIoTPreferredConnection, false);

setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
}
int count = 0;
void loop() {
  do {Serial.println("temperature");
 ArduinoCloud.update();


  byte i;
  byte data[12];
    ds.reset();
    pinMode(0, INPUT);
    if (digitalRead(0)) { // check return bus
       Serial.println("t1");
      ds.skip();
      ds.write(0x44);
      delay(1000);
      ds.reset();
      ds.skip();
      ds.write(0xBE);
      for ( i = 0; i < 9; i++) {
        data[i] = ds.read();
      }
    }
    int16_t raw = (data[1] << 8) | data[0];
    temperature = (int)raw / 16.0;
    Serial.println(temperature); 
    delay(100);} while(!ArduinoCloud.connected());

count++;
if(count == 8) {
   ESP.deepSleep(15e6);
  }
}

(works stably at 8 passes).

In another topic somehow similar to this one I have found an answer by user rmcd who stated the following:

"I have found, through careful trials, that you need to call the "ArduinoCloud.update()" method at least every 5 seconds. Longer delays between subsequent calls result in no updates on the dashboard even while is there is data accumulating in the .CSV files in the IoT Cloud. That might be bug in the Class method."

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.