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?
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?
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
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.
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.
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.
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:
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."