Programming a ESP32 I need to sync my variables before putting it to a timed deep sleep.
How can I ensure that my updated variables are pushed to the cloud before sleeping.
Simple way a delay().
Not the best way though.
I tried
delay(1500);
ArduinoCloud.update();
delay(1500);
before sleeping.
No luck
Maybe make it longer?
15 seconds?
@ptillisch
@dbeamonte_arduino
Is there a flush function for Iot Cloud?
Longer delays seems to loose connection to the cloud.
Maybe you could try something more elaborate like:
...
bool go_to_sleep = false;
int sleep_timestamp = 0;
...
void loop()
{
...
ArduinoCloud.update();
if (go_to_sleep) {
go_to_sleep = false;
sleep_timestamp = millis() + 1500;
}
if (millis() > sleep_timestamp) {
<go_to_deep_sleep> // Your code here to go to deep sleep
}
...
}
And you set go_to_sleep
to true
when you want.
This way, you don't use delay()
before sleeping but let the program run freely and call several times ArduinoCloud.update()
before going to sleep.
Let us know if this works.
I think it is a really interesting use case.
Can you share the full code. I'm just curious about how you are going to handle the deep sleep.
And, just out of curiosity, can you give us more details about your project? It looks really interesting and it is always great to learn about real use cases.
Good luck!
Thank you - now it works.
It might have worked even using the delay() function.
I found out that I didn't link the right variable to my dashboard (has been corrected now ).
Btw - I had to change the sleep_timestamp to get it to work.
Your code would go directly to sleep as millis() always would be > 0
My project is to make a plant watering application powered by battery or a solar panel, which is the reason that I want to conserve energy.
Furthermore my plants would'nt be better served checkking them with millisec's interval - I can just check a few times a day - or every hour.
Here is my code (without the watering/irrigation part):
/*
Sketch generated by the Arduino IoT Cloud Thing "Test_sleep"
https://create.arduino.cc/cloud/things/0f4cc19d-a6d1-4e3b-86f3-ff04100f8dfc
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String status;
int sleeptime;
CloudTime tid;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
bool go_to_sleep = false;
unsigned long sleep_timestamp = 99999999999999;
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);
Serial.println(ArduinoCloud.connected());
// Initialize cloud variables
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, onIoTSync);
delay(1500);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
/* Put your own code here - but remember that we are going to sleep when connected to the Cloud */
if (go_to_sleep) {
go_to_sleep = false;
sleep_timestamp = millis() + 1500;
}
if (millis() > sleep_timestamp) {
Serial.println("Going to sleep");
esp_sleep_enable_timer_wakeup(sleeptime * 60 * 1000000);
esp_deep_sleep_start();
}
}
/*
Since Sleeptime is READ_WRITE variable, onSleeptimeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSleeptimeChange() {
// Add your code here to act upon Sleeptime change
}
void onIoTSync() {
// Add your code here to act upon Sleeptime change
tid = ArduinoCloud.getLocalTime();
Serial.println("onIoTSync");
Serial.println(sleeptime);
Serial.println(tid);
go_to_sleep = true;
}
It sounds a really interesting use case. Thanks for sharing.
To schedule your events, you might be interested in taking a look at this:
Thank you for sharing the schedule options which seems very useful.
The downside in my use case is, that it would consume quite much energy if run on battery power.
I will definitely find use for it in future projects.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.