Skip to content

Commit 4d10ebb

Browse files
committed
Update arduino-cloud-callbacks.md
1 parent cb7fbe1 commit 4d10ebb

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
11
---
22
title: 'IoT Cloud Event & Callbacks'
33
description: 'Learn how to subscribe to events and add callback functions.'
4-
tags: [IoT Cloud, Variables]
4+
tags: [IoT Cloud, Events, Callbacks]
55
author: 'Karl Söderby'
66
---
77

88
The [Arduino IoT Cloud](https://create.arduino.cc/iot/) has support for events and callbacks. This can be used to trigger specific functionalities depending on what state your device is in.
99

10-
You can for example trigger a specific block of code whenever the board is in a connecting, synchronized or disconnected state. In this brief 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.
10+
You can for example trigger a specific block of code whenever the board is in a **connecting**, **synchronized** or **disconnected** state. In this brief 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.
11+
12+
## Events
13+
14+
The `ArduinoIoTCloudEvent` enumeration class has three possible events:
15+
- `CONNECT` (0) - Board successfully connects to IoT Cloud.
16+
- `SYNC` (1) - Data is successfully synced between Board and IoT Cloud.
17+
- `DISCONNECT` (2) - Board has lost connection to IoT Cloud.
18+
19+
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.
20+
21+
These events can be subscribed to using the `addCallback()` function, which is documented in the next section.
22+
23+
## Callbacks
24+
25+
Callbacks can be added for each event, and essentially triggers a custom function whenever the event occurs.
26+
27+
Callbacks are added via the `addCallback()` method from the `ArduinoIoTCloud` class, where the **event** and **custom function** are added as parameters.
28+
29+
```arduino
30+
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
31+
```
32+
33+
The above code will trigger the `doThisOnConnect()`
34+
function whenever the `CONNECT` event occurs.
35+
36+
***Please note that callback functions should be added inside the `setup()` of your sketch.***
1137

1238
## Full Example
1339

14-
The example below demonstrates the available callback
40+
The example below demonstrates how to use events & callbacks in the IoT Cloud.
1541

16-
## Summary
42+
<CodeBlock url="https://github.com/arduino-libraries/ArduinoIoTCloud/blob/master/examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino" className="arduino"/>
1743

18-
In this article, we have covered how to use variables in the Arduino IoT Cloud, and what variables are available.
44+
## Summary
1945

46+
This document covers the use of events & callbacks using the [ArduinoIoTCloud](https://github.com/arduino-libraries/ArduinoIoTCloud) library.

0 commit comments

Comments
 (0)