Hi, is there a way to read the last variable values? In my case, I tried to read variables before ArduinoCloud.Update() to get the last value from the graph but it didn't work. Is there a way I can get the last values whenever I reupdate the code?
Hello, I am having an issue and I was wondering if someone has already faced a problem like this. What I would like to do is get the last stored value for a variable in cloud when I power Arduino ON again, which means, use the Cloud to store the last value whenever I need to compile a code again or just reset Arduino. I tried to get the values from the variables before ArduinoCloud.Update() but that didn't do. Does anyone has any ideas on how to achieve that?
why not use EEPROM?
I have merged your cross-posts @limacintia.
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
Did you try looking through existing posts under this Category?
A few look relevant; eg,
You might try a search in this Category for "ArduinoCloud.Update" ...
When the board connects to the cloud it always gets the latest cloud value automatically! So you don't generally need to store it locally. This is very convenient.
However, you should know a few things:
- There's a delay between the time your board starts running and the cloud connection is established. This means that before reading/using the variable you need to make sure it was synced from the cloud. You can do it like this:
void setup() {
...
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, onSync);
...
}
void onSync() {
// Connection to the cloud was established and variables were synced!
// Now you can read and use them.
}
- This of course relies on Internet connectivity. If your board reboots and there's no connection to the cloud, your program will need to wait until connection comes back again. In that case, you might want to store your data locally in flash memory or an SD card.
Hi alranel,
If I use the addCallback method in setup(), how do I go about accessing the cloud variable's Last Value?
Suppose I have a Cloud variable "int counter" which is increased based on external sensors connected to the Arduino. If "counter = 100" before the next power cycle or boot by the Arduino, I'd like to set "counter = 100" in the setup so future values are 101, 102,... instead of 1, 2,...
Thank you
Here's the test code I was using to try to pick-up the Last Value. I created a Thing on the Cloud for this device (an Opta) and made a testing variable "int counter". After letting this run for a few minutes, the counter on the Cloud stepped up to 120 or so. Then I manually reset the Opta to see if the "counter" variable would pick up the Last Value of 120. The variable was reset to 0 again.
// blink the LED on the Opta for one second and increment the counter
// then manually reboot and see if the 'counter' variable picks up the Last Value in the callback...
// the Cloud variable "int counter" is defined here
#include "thingProperties.h"
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();
// local declarations
pinMode(LED_D0, OUTPUT);
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Trying the callback method
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, OnSync);
// these don't actually print to the serial monitor
Serial.println("Booting...");
Serial.println(counter);
/*
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();
// Your code here
digitalWrite(LED_D0, HIGH);
counter++;
delay(1000);
digitalWrite(LED_D0, LOW);
delay(1000);
}
void OnSync(){
/* add your custom code here */
Serial.println("Thing Properties synchronized");
Serial.println(counter);
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.