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
 (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
CONNECTDISCONNECTSYNCThese events can be subscribed to using the
addCallback()Callbacks
Callbacks can be added for each event, and essentially triggers a custom function whenever the event occurs.
Callbacks are added via the
addCallback()ArduinoIoTCloud1ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);The above code will trigger the
doThisOnConnect()CONNECTPlease 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.