From 32a74c24be7591000133f2e233b60a14eaf55a01 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 6 Feb 2019 12:34:01 -0500 Subject: [PATCH] Add Azure IoT Hub GSM, NB, and WiFi examples --- .../Azure_IoT_Hub_GSM/Azure_IoT_Hub_GSM.ino | 171 ++++++++++++++++++ .../Azure_IoT_Hub_GSM/arduino_secrets.h | 11 ++ .../Azure_IoT_Hub_NB/Azure_IoT_Hub_NB.ino | 168 +++++++++++++++++ .../Azure_IoT_Hub_NB/arduino_secrets.h | 8 + .../Azure_IoT_Hub_WiFi/Azure_IoT_Hub_WiFi.ino | 165 +++++++++++++++++ .../Azure_IoT_Hub_WiFi/arduino_secrets.h | 9 + 6 files changed, 532 insertions(+) create mode 100644 examples/Azure IoT Hub/Azure_IoT_Hub_GSM/Azure_IoT_Hub_GSM.ino create mode 100644 examples/Azure IoT Hub/Azure_IoT_Hub_GSM/arduino_secrets.h create mode 100644 examples/Azure IoT Hub/Azure_IoT_Hub_NB/Azure_IoT_Hub_NB.ino create mode 100644 examples/Azure IoT Hub/Azure_IoT_Hub_NB/arduino_secrets.h create mode 100644 examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/Azure_IoT_Hub_WiFi.ino create mode 100644 examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/arduino_secrets.h diff --git a/examples/Azure IoT Hub/Azure_IoT_Hub_GSM/Azure_IoT_Hub_GSM.ino b/examples/Azure IoT Hub/Azure_IoT_Hub_GSM/Azure_IoT_Hub_GSM.ino new file mode 100644 index 0000000..05387f3 --- /dev/null +++ b/examples/Azure IoT Hub/Azure_IoT_Hub_GSM/Azure_IoT_Hub_GSM.ino @@ -0,0 +1,171 @@ +/* + Azure IoT Hub GSM + + This sketch securely connects to an Azure IoT Hub using MQTT over GSM/3G. + It uses a private key stored in the ATECC508A and a self signed public + certificate for SSL/TLS authetication. + + It publishes a message every 5 seconds to "devices/{deviceId}/messages/events/" topic + and subscribes to messages on the "devices/{deviceId}/messages/devicebound/#" + topic. + + The circuit: + - MKR GSM 1400 board + - Antenna + - SIM card with a data plan + - LiPo battery + + This example code is in the public domain. +*/ + +#include +#include +#include +#include +#include + +#include "arduino_secrets.h" + +/////// Enter your sensitive data in arduino_secrets.h +const char pinnumber[] = SECRET_PINNUMBER; +const char gprs_apn[] = SECRET_GPRS_APN; +const char gprs_login[] = SECRET_GPRS_LOGIN; +const char gprs_password[] = SECRET_GPRS_PASSWORD; +const char broker[] = SECRET_BROKER; +String deviceId = SECRET_DEVICE_ID; + +GSM gsmAccess; +GPRS gprs; + +GSMClient gsmClient; // Used for the TCP socket connection +BearSSLClient sslClient(gsmClient); // Used for SSL/TLS connection, integrates with ECC508 +MqttClient mqttClient(sslClient); + +unsigned long lastMillis = 0; + +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!ECCX08.begin()) { + Serial.println("No ECCX08 present!"); + while (1); + } + + // reconstruct the self signed cert + ECCX08SelfSignedCert.beginReconstruction(0, 8); + ECCX08SelfSignedCert.setCommonName(ECCX08.serialNumber()); + ECCX08SelfSignedCert.endReconstruction(); + + // Set a callback to get the current time + // used to validate the servers certificate + ArduinoBearSSL.onGetTime(getTime); + + // Set the ECCX08 slot to use for the private key + // and the accompanying public certificate for it + sslClient.setEccSlot(0, ECCX08SelfSignedCert.bytes(), ECCX08SelfSignedCert.length()); + + // Set the client id used for MQTT as the device id + mqttClient.setId(deviceId); + + // Set the username to "//api-version=2018-06-30" and empty password + String username; + + username += broker; + username += "/"; + username += deviceId; + username += "/api-version=2018-06-30"; + + mqttClient.setUsernamePassword(username, ""); + + // Set the message callback, this function is + // called when the MQTTClient receives a message + mqttClient.onMessage(onMessageReceived); +} + +void loop() { + if (gsmAccess.status() != GSM_READY || gprs.status() != GPRS_READY) { + connectGSM(); + } + + if (!mqttClient.connected()) { + // MQTT client is disconnected, connect + connectMQTT(); + } + + // poll for new MQTT messages and send keep alives + mqttClient.poll(); + + // publish a message roughly every 5 seconds. + if (millis() - lastMillis > 5000) { + lastMillis = millis(); + + publishMessage(); + } +} + +unsigned long getTime() { + // get the current time from the cellular module + return gsmAccess.getTime(); +} + +void connectGSM() { + Serial.println("Attempting to connect to the cellular network"); + + while ((gsmAccess.begin(pinnumber) != GSM_READY) || + (gprs.attachGPRS(gprs_apn, gprs_login, gprs_password) != GPRS_READY)) { + // failed, retry + Serial.print("."); + delay(1000); + } + + Serial.println("You're connected to the cellular network"); + Serial.println(); +} + +void connectMQTT() { + Serial.print("Attempting to MQTT broker: "); + Serial.print(broker); + Serial.println(" "); + + while (!mqttClient.connect(broker, 8883)) { + // failed, retry + Serial.print("."); + Serial.println(mqttClient.connectError()); + delay(5000); + } + Serial.println(); + + Serial.println("You're connected to the MQTT broker"); + Serial.println(); + + // subscribe to a topic + mqttClient.subscribe("devices/" + deviceId + "/messages/devicebound/#"); +} + +void publishMessage() { + Serial.println("Publishing message"); + + // send message, the Print interface can be used to set the message contents + mqttClient.beginMessage("devices/" + deviceId + "/messages/events/"); + mqttClient.print("hello "); + mqttClient.print(millis()); + mqttClient.endMessage(); +} + +void onMessageReceived(int messageSize) { + // we received a message, print out the topic and contents + Serial.print("Received a message with topic '"); + Serial.print(mqttClient.messageTopic()); + Serial.print("', length "); + Serial.print(messageSize); + Serial.println(" bytes:"); + + // use the Stream interface to print the contents + while (mqttClient.available()) { + Serial.print((char)mqttClient.read()); + } + Serial.println(); + + Serial.println(); +} diff --git a/examples/Azure IoT Hub/Azure_IoT_Hub_GSM/arduino_secrets.h b/examples/Azure IoT Hub/Azure_IoT_Hub_GSM/arduino_secrets.h new file mode 100644 index 0000000..753e2dc --- /dev/null +++ b/examples/Azure IoT Hub/Azure_IoT_Hub_GSM/arduino_secrets.h @@ -0,0 +1,11 @@ +// GSM settings +#define SECRET_PINNUMBER "" +#define SECRET_GPRS_APN "GPRS_APN" // replace your GPRS APN +#define SECRET_GPRS_LOGIN "login" // replace with your GPRS login +#define SECRET_GPRS_PASSWORD "password" // replace with your GPRS password + +// Fill in the hostname of your Azure IoT Hub broker +#define SECRET_BROKER ".azure-devices.net" + +// Fill in the device id +#define SECRET_DEVICE_ID "" diff --git a/examples/Azure IoT Hub/Azure_IoT_Hub_NB/Azure_IoT_Hub_NB.ino b/examples/Azure IoT Hub/Azure_IoT_Hub_NB/Azure_IoT_Hub_NB.ino new file mode 100644 index 0000000..f4a1833 --- /dev/null +++ b/examples/Azure IoT Hub/Azure_IoT_Hub_NB/Azure_IoT_Hub_NB.ino @@ -0,0 +1,168 @@ +/* + Azure IoT Hub NB + + This sketch securely connects to an Azure IoT Hub using MQTT over NB IoT/LTE Cat M1. + It uses a private key stored in the ATECC508A and a self signed public + certificate for SSL/TLS authetication. + + It publishes a message every 5 seconds to "devices/{deviceId}/messages/events/" topic + and subscribes to messages on the "devices/{deviceId}/messages/devicebound/#" + topic. + + The circuit: + - MKR NB 1500 board + - Antenna + - SIM card with a data plan + - LiPo battery + + This example code is in the public domain. +*/ + +#include +#include +#include +#include +#include + +#include "arduino_secrets.h" + +/////// Enter your sensitive data in arduino_secrets.h +const char pinnumber[] = SECRET_PINNUMBER; +const char broker[] = SECRET_BROKER; +String deviceId = SECRET_DEVICE_ID; + +NB nbAccess; +GPRS gprs; + +NBClient nbClient; // Used for the TCP socket connection +BearSSLClient sslClient(nbClient); // Used for SSL/TLS connection, integrates with ECC508 +MqttClient mqttClient(sslClient); + +unsigned long lastMillis = 0; + +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!ECCX08.begin()) { + Serial.println("No ECCX08 present!"); + while (1); + } + + // reconstruct the self signed cert + ECCX08SelfSignedCert.beginReconstruction(0, 8); + ECCX08SelfSignedCert.setCommonName(ECCX08.serialNumber()); + ECCX08SelfSignedCert.endReconstruction(); + + // Set a callback to get the current time + // used to validate the servers certificate + ArduinoBearSSL.onGetTime(getTime); + + // Set the ECCX08 slot to use for the private key + // and the accompanying public certificate for it + sslClient.setEccSlot(0, ECCX08SelfSignedCert.bytes(), ECCX08SelfSignedCert.length()); + + // Set the client id used for MQTT as the device id + mqttClient.setId(deviceId); + + // Set the username to "//api-version=2018-06-30" and empty password + String username; + + username += broker; + username += "/"; + username += deviceId; + username += "/api-version=2018-06-30"; + + mqttClient.setUsernamePassword(username, ""); + + // Set the message callback, this function is + // called when the MQTTClient receives a message + mqttClient.onMessage(onMessageReceived); +} + +void loop() { + if (nbAccess.status() != NB_READY || gprs.status() != GPRS_READY) { + connectNB(); + } + + if (!mqttClient.connected()) { + // MQTT client is disconnected, connect + connectMQTT(); + } + + // poll for new MQTT messages and send keep alives + mqttClient.poll(); + + // publish a message roughly every 5 seconds. + if (millis() - lastMillis > 5000) { + lastMillis = millis(); + + publishMessage(); + } +} + +unsigned long getTime() { + // get the current time from the cellular module + return nbAccess.getTime(); +} + +void connectNB() { + Serial.println("Attempting to connect to the cellular network"); + + while ((nbAccess.begin(pinnumber) != NB_READY) || + (gprs.attachGPRS() != GPRS_READY)) { + // failed, retry + Serial.print("."); + delay(1000); + } + + Serial.println("You're connected to the cellular network"); + Serial.println(); +} + +void connectMQTT() { + Serial.print("Attempting to MQTT broker: "); + Serial.print(broker); + Serial.println(" "); + + while (!mqttClient.connect(broker, 8883)) { + // failed, retry + Serial.print("."); + Serial.println(mqttClient.connectError()); + delay(5000); + } + Serial.println(); + + Serial.println("You're connected to the MQTT broker"); + Serial.println(); + + // subscribe to a topic + mqttClient.subscribe("devices/" + deviceId + "/messages/devicebound/#"); +} + +void publishMessage() { + Serial.println("Publishing message"); + + // send message, the Print interface can be used to set the message contents + mqttClient.beginMessage("devices/" + deviceId + "/messages/events/"); + mqttClient.print("hello "); + mqttClient.print(millis()); + mqttClient.endMessage(); +} + +void onMessageReceived(int messageSize) { + // we received a message, print out the topic and contents + Serial.print("Received a message with topic '"); + Serial.print(mqttClient.messageTopic()); + Serial.print("', length "); + Serial.print(messageSize); + Serial.println(" bytes:"); + + // use the Stream interface to print the contents + while (mqttClient.available()) { + Serial.print((char)mqttClient.read()); + } + Serial.println(); + + Serial.println(); +} diff --git a/examples/Azure IoT Hub/Azure_IoT_Hub_NB/arduino_secrets.h b/examples/Azure IoT Hub/Azure_IoT_Hub_NB/arduino_secrets.h new file mode 100644 index 0000000..ff0f78a --- /dev/null +++ b/examples/Azure IoT Hub/Azure_IoT_Hub_NB/arduino_secrets.h @@ -0,0 +1,8 @@ +// NB settings +#define SECRET_PINNUMBER "" + +// Fill in the hostname of your Azure IoT Hub broker +#define SECRET_BROKER ".azure-devices.net" + +// Fill in the device id +#define SECRET_DEVICE_ID "" diff --git a/examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/Azure_IoT_Hub_WiFi.ino b/examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/Azure_IoT_Hub_WiFi.ino new file mode 100644 index 0000000..7dc9c35 --- /dev/null +++ b/examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/Azure_IoT_Hub_WiFi.ino @@ -0,0 +1,165 @@ +/* + Azure IoT Hub WiFi + + This sketch securely connects to an Azure IoT Hub using MQTT over WiFi. + It uses a private key stored in the ATECC508A and a self signed public + certificate for SSL/TLS authetication. + + It publishes a message every 5 seconds to "devices/{deviceId}/messages/events/" topic + and subscribes to messages on the "devices/{deviceId}/messages/devicebound/#" + topic. + + The circuit: + - Arduino MKR WiFi 1010 or MKR1000 + + This example code is in the public domain. +*/ + +#include +#include +#include +#include +#include // change to #include for MKR1000 + +#include "arduino_secrets.h" + +/////// Enter your sensitive data in arduino_secrets.h +const char ssid[] = SECRET_SSID; +const char pass[] = SECRET_PASS; +const char broker[] = SECRET_BROKER; +String deviceId = SECRET_DEVICE_ID; + +WiFiClient wifiClient; // Used for the TCP socket connection +BearSSLClient sslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECC508 +MqttClient mqttClient(sslClient); + +unsigned long lastMillis = 0; + +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!ECCX08.begin()) { + Serial.println("No ECCX08 present!"); + while (1); + } + + // reconstruct the self signed cert + ECCX08SelfSignedCert.beginReconstruction(0, 8); + ECCX08SelfSignedCert.setCommonName(ECCX08.serialNumber()); + ECCX08SelfSignedCert.endReconstruction(); + + // Set a callback to get the current time + // used to validate the servers certificate + ArduinoBearSSL.onGetTime(getTime); + + // Set the ECCX08 slot to use for the private key + // and the accompanying public certificate for it + sslClient.setEccSlot(0, ECCX08SelfSignedCert.bytes(), ECCX08SelfSignedCert.length()); + + // Set the client id used for MQTT as the device id + mqttClient.setId(deviceId); + + // Set the username to "//api-version=2018-06-30" and empty password + String username; + + username += broker; + username += "/"; + username += deviceId; + username += "/api-version=2018-06-30"; + + mqttClient.setUsernamePassword(username, ""); + + // Set the message callback, this function is + // called when the MQTTClient receives a message + mqttClient.onMessage(onMessageReceived); +} + +void loop() { + if (WiFi.status() != WL_CONNECTED) { + connectWiFi(); + } + + if (!mqttClient.connected()) { + // MQTT client is disconnected, connect + connectMQTT(); + } + + // poll for new MQTT messages and send keep alives + mqttClient.poll(); + + // publish a message roughly every 5 seconds. + if (millis() - lastMillis > 5000) { + lastMillis = millis(); + + publishMessage(); + } +} + +unsigned long getTime() { + // get the current time from the WiFi module + return WiFi.getTime(); +} + +void connectWiFi() { + Serial.print("Attempting to connect to SSID: "); + Serial.print(ssid); + Serial.print(" "); + + while (WiFi.begin(ssid, pass) != WL_CONNECTED) { + // failed, retry + Serial.print("."); + delay(5000); + } + Serial.println(); + + Serial.println("You're connected to the network"); + Serial.println(); +} + +void connectMQTT() { + Serial.print("Attempting to MQTT broker: "); + Serial.print(broker); + Serial.println(" "); + + while (!mqttClient.connect(broker, 8883)) { + // failed, retry + Serial.print("."); + Serial.println(mqttClient.connectError()); + delay(5000); + } + Serial.println(); + + Serial.println("You're connected to the MQTT broker"); + Serial.println(); + + // subscribe to a topic + mqttClient.subscribe("devices/" + deviceId + "/messages/devicebound/#"); +} + +void publishMessage() { + Serial.println("Publishing message"); + + // send message, the Print interface can be used to set the message contents + mqttClient.beginMessage("devices/" + deviceId + "/messages/events/"); + mqttClient.print("hello "); + mqttClient.print(millis()); + mqttClient.endMessage(); +} + +void onMessageReceived(int messageSize) { + // we received a message, print out the topic and contents + Serial.print("Received a message with topic '"); + Serial.print(mqttClient.messageTopic()); + Serial.print("', length "); + Serial.print(messageSize); + Serial.println(" bytes:"); + + // use the Stream interface to print the contents + while (mqttClient.available()) { + Serial.print((char)mqttClient.read()); + } + Serial.println(); + + Serial.println(); +} diff --git a/examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/arduino_secrets.h b/examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/arduino_secrets.h new file mode 100644 index 0000000..71d5644 --- /dev/null +++ b/examples/Azure IoT Hub/Azure_IoT_Hub_WiFi/arduino_secrets.h @@ -0,0 +1,9 @@ +// Fill in your WiFi networks SSID and password +#define SECRET_SSID "" +#define SECRET_PASS "" + +// Fill in the hostname of your Azure IoT Hub broker +#define SECRET_BROKER ".azure-devices.net" + +// Fill in the device id +#define SECRET_DEVICE_ID ""