diff --git a/examples/ArduinoIoTCloud-Advanced/ArduinoIoTCloud-Advanced.ino b/examples/ArduinoIoTCloud-Advanced/ArduinoIoTCloud-Advanced.ino index 181205776..aa19f05ab 100644 --- a/examples/ArduinoIoTCloud-Advanced/ArduinoIoTCloud-Advanced.ino +++ b/examples/ArduinoIoTCloud-Advanced/ArduinoIoTCloud-Advanced.ino @@ -7,6 +7,7 @@ - MKR GSM 1400 - MKR NB 1500 - MKR WAN 1300/1310 + - Nano 33 IoT - ESP 8266 */ diff --git a/examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino b/examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino index e5a8e4c86..da432cc6f 100644 --- a/examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino +++ b/examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino @@ -16,6 +16,7 @@ - MKR GSM 1400 - MKR NB 1500 - MKR WAN 1300/1310 + - Nano 33 IoT - ESP 8266 */ diff --git a/examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino b/examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino new file mode 100644 index 000000000..a226a1926 --- /dev/null +++ b/examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino @@ -0,0 +1,80 @@ +/* + This sketch demonstrates how to subscribe to IoT Cloud events and perform actions + The available events are + + CONNECT : Board successfully connects to IoT Cloud + SYNC : Data is successfully synced between Board and IoT Cloud + DISCONNECT : Board has lost connection to IoT Cloud + + You don't need any specific Properties to be created in order to demonstrate these functionalities. + Simply create a new Thing and give it 1 arbitrary Property. + Remember that the Thing ID needs to be configured in thingProperties.h + These events can be very useful in particular cases, for instance to disable a peripheral + or a connected sensor/actuator when no data connection is available, as well as to perform + specific operations on connection or right after properties values are synchronised. + + To subscribe to an event you can use the `addCallback` method and specify + which event will trigger which custom function. + One function per event can be assigned. + + IMPORTANT: + This sketch works with WiFi, GSM, NB and Lora enabled boards supported by Arduino IoT Cloud. + On a LoRa board, if it is configuered as a class A device (default and preferred option), values from Cloud dashboard are received + only after a value is sent to Cloud. + + This sketch is compatible with: + - MKR 1000 + - MKR WIFI 1010 + - MKR GSM 1400 + - MKR NB 1500 + - MKR WAN 1300/1310 + - Nano 33 IoT + - ESP 8266 +*/ + +#include "arduino_secrets.h" +#include "thingProperties.h" + +void setup() { + /* Initialize serial and wait up to 5 seconds for port to open */ + Serial.begin(9600); + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000); ) { } + + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ + initProperties(); + + /* Initialize Arduino IoT Cloud library */ + ArduinoCloud.begin(ArduinoIoTPreferredConnection); + + /* + Invoking `addCallback` on the ArduinoCloud object allows you to subscribe + to any of the available events and decide which functions to call when they are fired. + + The functions `doThisOnConnect`, `doThisOnSync`, `doThisOnDisconnect` + are custom functions and can be named to your likings and for this example + they are defined/implemented at the bottom of the Sketch + */ + ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect); + ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync); + ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnDisconnect); + + setDebugMessageLevel(DBG_INFO); + ArduinoCloud.printDebugInfo(); +} + +void loop() { + ArduinoCloud.update(); +} + +void doThisOnConnect(){ + /* add your custom code here */ + Serial.println("Board successfully connected to Arduino IoT Cloud"); +} +void doThisOnSync(){ + /* add your custom code here */ + Serial.println("Thing Properties synchronised"); +} +void doThisOnDisconnect(){ + /* add your custom code here */ + Serial.println("Board disconnected from Arduino IoT Cloud"); +} \ No newline at end of file diff --git a/examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h b/examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h new file mode 100644 index 000000000..fc0b0661e --- /dev/null +++ b/examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h @@ -0,0 +1,34 @@ +#include + +/* MKR1000, MKR WiFi 1010 */ +#if defined(BOARD_HAS_WIFI) + #define SECRET_SSID "YOUR_WIFI_NETWORK_NAME" + #define SECRET_PASS "YOUR_WIFI_PASSWORD" +#endif + +/* ESP8266 */ +#if defined(BOARD_ESP8266) + #define SECRET_DEVICE_KEY "my-device-password" +#endif + +/* MKR GSM 1400 */ +#if defined(BOARD_HAS_GSM) + #define SECRET_PIN "" + #define SECRET_APN "" + #define SECRET_LOGIN "" + #define SECRET_PASS "" +#endif + +/* MKR WAN 1300/1310 */ +#if defined(BOARD_HAS_LORA) + #define SECRET_APP_EUI "" + #define SECRET_APP_KEY "" +#endif + +/* MKR NB 1500 */ +#if defined(BOARD_HAS_NB) + #define SECRET_PIN "" + #define SECRET_APN "" + #define SECRET_LOGIN "" + #define SECRET_PASS "" +#endif diff --git a/examples/ArduinoIoTCloud-Callbacks/thingProperties.h b/examples/ArduinoIoTCloud-Callbacks/thingProperties.h new file mode 100644 index 000000000..20ee3c9e6 --- /dev/null +++ b/examples/ArduinoIoTCloud-Callbacks/thingProperties.h @@ -0,0 +1,33 @@ +#include +#include + +#if defined(BOARD_HAS_WIFI) +#elif defined(BOARD_HAS_GSM) +#elif defined(BOARD_HAS_LORA) +#elif defined(BOARD_HAS_NB) +#else + #error "Arduino IoT Cloud currently only supports MKR1000, MKR WiFi 1010, MKR WAN 1300/1310, MKR NB 1500 and MKR GSM 1400" +#endif + +#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + +/* BOARD_ID is only required if you are using an ESP8266 */ +#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + +void initProperties() { +#if defined(BOARD_ESP8266) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif + ArduinoCloud.setThingId(THING_ID); +} + +#if defined(BOARD_HAS_WIFI) + WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS); +#elif defined(BOARD_HAS_GSM) + GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); +#elif defined(BOARD_HAS_LORA) + LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, _lora_class::CLASS_A); +#elif defined(BOARD_HAS_NB) + NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); +#endif