Skip to content

Commit 457d932

Browse files
authored
Merge pull request #224 from ubidefeo/master
New Callbacks example + Added Nano 33 IoT to boards list in examples comment
2 parents a02123e + 0a9b78f commit 457d932

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

examples/ArduinoIoTCloud-Advanced/ArduinoIoTCloud-Advanced.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- MKR GSM 1400
88
- MKR NB 1500
99
- MKR WAN 1300/1310
10+
- Nano 33 IoT
1011
- ESP 8266
1112
*/
1213

examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- MKR GSM 1400
1717
- MKR NB 1500
1818
- MKR WAN 1300/1310
19+
- Nano 33 IoT
1920
- ESP 8266
2021
*/
2122

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <Arduino_ConnectionHandler.h>
2+
3+
/* MKR1000, MKR WiFi 1010 */
4+
#if defined(BOARD_HAS_WIFI)
5+
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
6+
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
7+
#endif
8+
9+
/* ESP8266 */
10+
#if defined(BOARD_ESP8266)
11+
#define SECRET_DEVICE_KEY "my-device-password"
12+
#endif
13+
14+
/* MKR GSM 1400 */
15+
#if defined(BOARD_HAS_GSM)
16+
#define SECRET_PIN ""
17+
#define SECRET_APN ""
18+
#define SECRET_LOGIN ""
19+
#define SECRET_PASS ""
20+
#endif
21+
22+
/* MKR WAN 1300/1310 */
23+
#if defined(BOARD_HAS_LORA)
24+
#define SECRET_APP_EUI ""
25+
#define SECRET_APP_KEY ""
26+
#endif
27+
28+
/* MKR NB 1500 */
29+
#if defined(BOARD_HAS_NB)
30+
#define SECRET_PIN ""
31+
#define SECRET_APN ""
32+
#define SECRET_LOGIN ""
33+
#define SECRET_PASS ""
34+
#endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
4+
#if defined(BOARD_HAS_WIFI)
5+
#elif defined(BOARD_HAS_GSM)
6+
#elif defined(BOARD_HAS_LORA)
7+
#elif defined(BOARD_HAS_NB)
8+
#else
9+
#error "Arduino IoT Cloud currently only supports MKR1000, MKR WiFi 1010, MKR WAN 1300/1310, MKR NB 1500 and MKR GSM 1400"
10+
#endif
11+
12+
#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
13+
14+
/* BOARD_ID is only required if you are using an ESP8266 */
15+
#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
16+
17+
void initProperties() {
18+
#if defined(BOARD_ESP8266)
19+
ArduinoCloud.setBoardId(BOARD_ID);
20+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
21+
#endif
22+
ArduinoCloud.setThingId(THING_ID);
23+
}
24+
25+
#if defined(BOARD_HAS_WIFI)
26+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
27+
#elif defined(BOARD_HAS_GSM)
28+
GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
29+
#elif defined(BOARD_HAS_LORA)
30+
LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, _lora_class::CLASS_A);
31+
#elif defined(BOARD_HAS_NB)
32+
NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
33+
#endif

0 commit comments

Comments
 (0)