Event & Callbacks
Learn how to subscribe to events and add callback functions.
The Arduino Cloud has support for events and callbacks. This can be used to trigger specific functionalities depending on what state your device is in.
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 library.
Events
The
ArduinoIoTCloudEvent
enumeration class has three possible events:
(0) - Board successfully connects to Arduino Cloud.CONNECT
(1) - Data is successfully synced between Board and Arduino Cloud.SYNC
(2) - Board has lost connection to Arduino Cloud.DISCONNECT
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.These events can be subscribed to using the
addCallback()
function, which is documented in the next section.Callbacks
Callbacks can be added for each event, and essentially triggers a custom function whenever the event occurs.
Callbacks are added via the
addCallback()
method from the ArduinoIoTCloud
class, where the event and custom function are added as parameters. 1ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
The above code will trigger the
doThisOnConnect()
function whenever the CONNECT
event occurs.Please note that callback functions should be added inside the
of your sketch.setup()
Full Example
The example below demonstrates how to use events & callbacks in the Arduino Cloud.
1
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.