1
+ /*
2
+ This sketch demonstrates how to subscribe to IoT Cloud events and perform actions
3
+ The available events are
4
+
5
+ CONNECT : Board successfully connects to IoT Cloud
6
+ SYNC : Data is successfully synced between Board and IoT Cloud
7
+ DISCONNECT : Board has lost connection to IoT Cloud
8
+
9
+ You don't need any specific Properties to be created in order to demonstrate these functionalities.
10
+ Simply create a new Thing and give it 1 arbitrary Property.
11
+ Remember that the Thing ID needs to be configured in thingProperties.h
12
+ These events can be very useful in particular cases, for instance to disable a peripheral
13
+ or a connected sensor/actuator when no data connection is available, as well as to perform
14
+ specific operations on connection or right after properties values are synchronised.
15
+
16
+ To subscribe to an event you can use the `addCallback` method and specify
17
+ which event will trigger which custom function.
18
+ One function per event can be assigned.
19
+
20
+ IMPORTANT:
21
+ This sketch works with WiFi, GSM, NB and Lora enabled boards supported by Arduino IoT Cloud.
22
+ On a LoRa board, if it is configuered as a class A device (default and preferred option), values from Cloud dashboard are received
23
+ only after a value is sent to Cloud.
24
+
25
+ This sketch is compatible with:
26
+ - MKR 1000
27
+ - MKR WIFI 1010
28
+ - MKR GSM 1400
29
+ - MKR NB 1500
30
+ - MKR WAN 1300/1310
31
+ - Nano 33 IoT
32
+ - ESP 8266
33
+ */
34
+
35
+ #include " arduino_secrets.h"
36
+ #include " thingProperties.h"
37
+
38
+ void setup () {
39
+ /* Initialize serial and wait up to 5 seconds for port to open */
40
+ Serial.begin (9600 );
41
+ for (unsigned long const serialBeginTime = millis (); !Serial && (millis () - serialBeginTime > 5000 ); ) { }
42
+
43
+ /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
44
+ initProperties ();
45
+
46
+ /* Initialize Arduino IoT Cloud library */
47
+ ArduinoCloud.begin (ArduinoIoTPreferredConnection);
48
+
49
+ /*
50
+ Invoking `addCallback` on the ArduinoCloud object allows you to subscribe
51
+ to any of the available events and decide which functions to call when they are fired.
52
+
53
+ The functions `doThisOnConnect`, `doThisOnSync`, `doThisOnDisconnect`
54
+ are custom functions and can be named to your likings and for this example
55
+ they are defined/implemented at the bottom of the Sketch
56
+ */
57
+ ArduinoCloud.addCallback (ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
58
+ ArduinoCloud.addCallback (ArduinoIoTCloudEvent::SYNC, doThisOnSync);
59
+ ArduinoCloud.addCallback (ArduinoIoTCloudEvent::CONNECT, doThisOnDisconnect);
60
+
61
+ setDebugMessageLevel (DBG_INFO);
62
+ ArduinoCloud.printDebugInfo ();
63
+ }
64
+
65
+ void loop () {
66
+ ArduinoCloud.update ();
67
+ }
68
+
69
+ void doThisOnConnect (){
70
+ /* add your custom code here */
71
+ Serial.println (" Board successfully connected to Arduino IoT Cloud" );
72
+ }
73
+ void doThisOnSync (){
74
+ /* add your custom code here */
75
+ Serial.println (" Thing Properties synchronised" );
76
+ }
77
+ void doThisOnDisconnect (){
78
+ /* add your custom code here */
79
+ Serial.println (" Board disconnected from Arduino IoT Cloud" );
80
+ }
0 commit comments