Skip to content

New Callbacks example + Added Nano 33 IoT to boards list in examples comment #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- MKR GSM 1400
- MKR NB 1500
- MKR WAN 1300/1310
- Nano 33 IoT
- ESP 8266
*/

Expand Down
1 change: 1 addition & 0 deletions examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- MKR GSM 1400
- MKR NB 1500
- MKR WAN 1300/1310
- Nano 33 IoT
- ESP 8266
*/

Expand Down
80 changes: 80 additions & 0 deletions examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino
Original file line number Diff line number Diff line change
@@ -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");
}
34 changes: 34 additions & 0 deletions examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <Arduino_ConnectionHandler.h>

/* 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
33 changes: 33 additions & 0 deletions examples/ArduinoIoTCloud-Callbacks/thingProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

#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