Skip to content

Commit 4834ed2

Browse files
committed
Update Callbacks & Events
1 parent 4106b0c commit 4834ed2

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

‎content/arduino-cloud/03.cloud-interface/02.variables/variables.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ Inside a variable configuration, we have several options:
4444
- **On Change:** variable synchronizes whenever value changes (threshold is `0` by default).
4545
- **Periodically:** variable synchronizes every `x` seconds.
4646

47-
48-
4947
### Automatic Sketch Generation
5048

5149
Any variables you add will be automatically added to your [thingProperties](/arduino-cloud/cloud-interface/sketches#configuration-header-file) file, which contains any configurations made inside your Thing.
@@ -129,6 +127,22 @@ Whenever one variable updates, any variables added to the synchronisation will a
129127

130128
***For more details on this feature, check out the [Device to Device](https://docs.arduino.cc/arduino-cloud/features/device-to-device) tutorial.***
131129

130+
## Variable Lifecycle
131+
132+
Cloud variables are only synced with the Arduino Cloud during the **Synchronized** status.
133+
When we are in a different state, any change to the variable is local and will be overridden by the Arduino Cloud value as soon as it gets Synchronized.
134+
135+
A variable can be in any of the following stages:
136+
137+
- **Disconnected** - The variable has the local value. Any modification to the value will be overridden once Synchronized.
138+
- **Connecting** - The variable has the local value. Any modification to the value will be overridden once Synchronized.
139+
- **Connected** - The variable has the local value. Any modification to the value will be overridden once Synchronized.
140+
- **Synchronized** - The variable is synced with the Cloud.
141+
- If the value is changed locally, it is populated to the Cloud.
142+
- If the value is changed in the Cloud via a dashboard or variable sync, the local value is updated (only for **Read Write** variables).
143+
144+
***For callbacks & events depending on what status the variable is in, check out the [Events & Callbacks](/arduino-cloud/api/c-library#events--callbacks) section.***
145+
132146
## Cloud Variable List
133147

134148
Cloud variables are divided into three categories: **basic, specialized** and **complex** types. Below you will find all available variables that you can create.

‎content/arduino-cloud/07.api/03.c-library/c-library.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,48 @@ The documentation lives in the [ArduinoIoTCloud](https://github.com/arduino-libr
3838

3939
Most of its functions are already pre-configured in your sketch files, so for most use cases you will not need to explore the API. Functions for connecting to, and syncing data with the Arduino Cloud is handled automatically, and will be generated into your sketch files in the web environment.
4040

41-
## Arduino IDE
41+
## Arduino IDE (Offline)
4242

43-
This library can be used with the offline version of the Arduino IDE (download through the [Arduino Downloads Page](https://www.arduino.cc/en/software/)). You can program your devices offline and monitor them via the cloud, but your Thing configuration is not synchronized if you do so.
43+
This library can be used with the offline version of the Arduino IDE (download through the [Arduino Downloads Page](https://www.arduino.cc/en/software/)). You can program your devices offline and monitor them via the cloud, but your Thing configuration is not synchronized if you do so.
44+
45+
To use it offline, you will manually need to install the following libraries in the Arduino IDE:
46+
- [ArduinoIoTCloud](https://github.com/arduino-libraries/ArduinoIoTCloud)
47+
- [Arduino_ConnectionHandler](https://github.com/arduino-libraries/Arduino_ConnectionHandler)
48+
49+
## Events & Callbacks
50+
51+
Arduino Cloud has support for events and callbacks. This can be used to trigger specific functionalities depending on what state your device is in.
52+
53+
You can for example trigger a specific block of code whenever the board is in a **connecting**, **synchronized** or **disconnected** state. In this document, we will explore how to set it up, using an example from the [ArduinoIoTCloud](https://github.com/arduino-libraries/ArduinoIoTCloud/blob/master/examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino) library.
54+
55+
### Events
56+
57+
The `ArduinoIoTCloudEvent` enumeration class has three possible events:
58+
- `CONNECT` (0) - Board successfully connects to IoT Cloud.
59+
- `SYNC` (1) - Data is successfully synced between Board and IoT Cloud.
60+
- `DISCONNECT` (2) - Board has lost connection to IoT Cloud.
61+
62+
The `CONNECT` and `DISCONNECT` events can occur even though no variable is created inside the Thing. However, `SYNC` requires a variable to be created, as this triggers whenever data is synchronized between the board and cloud.
63+
64+
These events can be subscribed to using the `addCallback()` function, which is documented in the next section.
65+
66+
### Callbacks
67+
68+
Callbacks can be added for each event, and essentially triggers a custom function whenever the event occurs.
69+
70+
Callbacks are added via the `addCallback()` method from the `ArduinoIoTCloud` class, where the **event** and **custom function** are added as parameters.
71+
72+
```arduino
73+
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
74+
```
75+
76+
The above code will trigger the `doThisOnConnect()`
77+
function whenever the `CONNECT` event occurs.
78+
79+
***Please note that callback functions should be added inside the `setup()` of your sketch.***
80+
81+
### Full Example
82+
83+
The example below demonstrates how to use events & callbacks in the IoT Cloud.
84+
85+
<CodeBlock url="https://github.com/arduino-libraries/ArduinoIoTCloud/blob/master/examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino" className="arduino"/>

0 commit comments

Comments
 (0)