Skip to content

Commit 67f89d0

Browse files
committed
Add Azure IoT Hub GSM, NB, and WiFi examples
1 parent 3e64617 commit 67f89d0

File tree

6 files changed

+532
-0
lines changed

6 files changed

+532
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
Azure IoT Hub GSM
3+
4+
This sketch securely connects to an Azure IoT Hub using MQTT over GSM/3G.
5+
It uses a private key stored in the ATECC508A and a self signed public
6+
certificate for SSL/TLS authetication.
7+
8+
It publishes a message every 5 seconds to "devices/{deviceId}/messages/events/" topic
9+
and subscribes to messages on the "devices/{deviceId}/messages/devicebound/#"
10+
topic.
11+
12+
The circuit:
13+
- MKR GSM 1400 board
14+
- Antenna
15+
- SIM card with a data plan
16+
- LiPo battery
17+
18+
This example code is in the public domain.
19+
*/
20+
21+
#include <ArduinoBearSSL.h>
22+
#include <ArduinoECCX08.h>
23+
#include <utility/ECCX08SelfSignedCert.h>
24+
#include <ArduinoMqttClient.h>
25+
#include <MKRGSM.h>
26+
27+
#include "arduino_secrets.h"
28+
29+
/////// Enter your sensitive data in arduino_secrets.h
30+
const char pinnumber[] = SECRET_PINNUMBER;
31+
const char gprs_apn[] = SECRET_GPRS_APN;
32+
const char gprs_login[] = SECRET_GPRS_LOGIN;
33+
const char gprs_password[] = SECRET_GPRS_PASSWORD;
34+
const char broker[] = SECRET_BROKER;
35+
String deviceId = SECRET_DEVICE_ID;
36+
37+
GSM gsmAccess;
38+
GPRS gprs;
39+
40+
GSMClient gsmClient; // Used for the TCP socket connection
41+
BearSSLClient sslClient(gsmClient); // Used for SSL/TLS connection, integrates with ECC508
42+
MqttClient mqttClient(sslClient);
43+
44+
unsigned long lastMillis = 0;
45+
46+
void setup() {
47+
Serial.begin(9600);
48+
while (!Serial);
49+
50+
if (!ECCX08.begin()) {
51+
Serial.println("No ECCX08 present!");
52+
while (1);
53+
}
54+
55+
// reconstruct the self signed cert
56+
ECCX08SelfSignedCert.beginReconstruction(0, 8);
57+
ECCX08SelfSignedCert.setCommonName(ECCX08.serialNumber());
58+
ECCX08SelfSignedCert.endReconstruction();
59+
60+
// Set a callback to get the current time
61+
// used to validate the servers certificate
62+
ArduinoBearSSL.onGetTime(getTime);
63+
64+
// Set the ECCX08 slot to use for the private key
65+
// and the accompanying public certificate for it
66+
sslClient.setEccSlot(0, ECCX08SelfSignedCert.bytes(), ECCX08SelfSignedCert.length());
67+
68+
// Set the client id used for MQTT as the device id
69+
mqttClient.setId(deviceId);
70+
71+
// Set the username to "<broker>/<device id>/api-version=2018-06-30" and empty password
72+
String username;
73+
74+
username += broker;
75+
username += "/";
76+
username += deviceId;
77+
username += "/api-version=2018-06-30";
78+
79+
mqttClient.setUsernamePassword(username, "");
80+
81+
// Set the message callback, this function is
82+
// called when the MQTTClient receives a message
83+
mqttClient.onMessage(onMessageReceived);
84+
}
85+
86+
void loop() {
87+
if (gsmAccess.status() != GSM_READY || gprs.status() != GPRS_READY) {
88+
connectGSM();
89+
}
90+
91+
if (!mqttClient.connected()) {
92+
// MQTT client is disconnected, connect
93+
connectMQTT();
94+
}
95+
96+
// poll for new MQTT messages and send keep alives
97+
mqttClient.poll();
98+
99+
// publish a message roughly every 5 seconds.
100+
if (millis() - lastMillis > 5000) {
101+
lastMillis = millis();
102+
103+
publishMessage();
104+
}
105+
}
106+
107+
unsigned long getTime() {
108+
// get the current time from the cellular module
109+
return gsmAccess.getTime();
110+
}
111+
112+
void connectGSM() {
113+
Serial.println("Attempting to connect to the cellular network");
114+
115+
while ((gsmAccess.begin(pinnumber) != GSM_READY) ||
116+
(gprs.attachGPRS(gprs_apn, gprs_login, gprs_password) != GPRS_READY)) {
117+
// failed, retry
118+
Serial.print(".");
119+
delay(1000);
120+
}
121+
122+
Serial.println("You're connected to the cellular network");
123+
Serial.println();
124+
}
125+
126+
void connectMQTT() {
127+
Serial.print("Attempting to MQTT broker: ");
128+
Serial.print(broker);
129+
Serial.println(" ");
130+
131+
while (!mqttClient.connect(broker, 8883)) {
132+
// failed, retry
133+
Serial.print(".");
134+
Serial.println(mqttClient.connectError());
135+
delay(5000);
136+
}
137+
Serial.println();
138+
139+
Serial.println("You're connected to the MQTT broker");
140+
Serial.println();
141+
142+
// subscribe to a topic
143+
mqttClient.subscribe("devices/" + deviceId + "/messages/devicebound/#");
144+
}
145+
146+
void publishMessage() {
147+
Serial.println("Publishing message");
148+
149+
// send message, the Print interface can be used to set the message contents
150+
mqttClient.beginMessage("devices/" + deviceId + "/messages/events/");
151+
mqttClient.print("hello ");
152+
mqttClient.print(millis());
153+
mqttClient.endMessage();
154+
}
155+
156+
void onMessageReceived(int messageSize) {
157+
// we received a message, print out the topic and contents
158+
Serial.print("Received a message with topic '");
159+
Serial.print(mqttClient.messageTopic());
160+
Serial.print("', length ");
161+
Serial.print(messageSize);
162+
Serial.println(" bytes:");
163+
164+
// use the Stream interface to print the contents
165+
while (mqttClient.available()) {
166+
Serial.print((char)mqttClient.read());
167+
}
168+
Serial.println();
169+
170+
Serial.println();
171+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// GSM settings
2+
#define SECRET_PINNUMBER ""
3+
#define SECRET_GPRS_APN "GPRS_APN" // replace your GPRS APN
4+
#define SECRET_GPRS_LOGIN "login" // replace with your GPRS login
5+
#define SECRET_GPRS_PASSWORD "password" // replace with your GPRS password
6+
7+
// Fill in the hostname of your Azure IoT Hub broker
8+
#define SECRET_BROKER "<hub name>.azure-devices.net"
9+
10+
// Fill in the device id
11+
#define SECRET_DEVICE_ID "<device id>"
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
Azure IoT Hub NB
3+
4+
This sketch securely connects to an Azure IoT Hub using MQTT over NB IoT/LTE Cat M1.
5+
It uses a private key stored in the ATECC508A and a self signed public
6+
certificate for SSL/TLS authetication.
7+
8+
It publishes a message every 5 seconds to "devices/{deviceId}/messages/events/" topic
9+
and subscribes to messages on the "devices/{deviceId}/messages/devicebound/#"
10+
topic.
11+
12+
The circuit:
13+
- MKR NB 1500 board
14+
- Antenna
15+
- SIM card with a data plan
16+
- LiPo battery
17+
18+
This example code is in the public domain.
19+
*/
20+
21+
#include <ArduinoBearSSL.h>
22+
#include <ArduinoECCX08.h>
23+
#include <utility/ECCX08SelfSignedCert.h>
24+
#include <ArduinoMqttClient.h>
25+
#include <MKRNB.h>
26+
27+
#include "arduino_secrets.h"
28+
29+
/////// Enter your sensitive data in arduino_secrets.h
30+
const char pinnumber[] = SECRET_PINNUMBER;
31+
const char broker[] = SECRET_BROKER;
32+
String deviceId = SECRET_DEVICE_ID;
33+
34+
NB nbAccess;
35+
GPRS gprs;
36+
37+
NBClient nbClient; // Used for the TCP socket connection
38+
BearSSLClient sslClient(nbClient); // Used for SSL/TLS connection, integrates with ECC508
39+
MqttClient mqttClient(sslClient);
40+
41+
unsigned long lastMillis = 0;
42+
43+
void setup() {
44+
Serial.begin(9600);
45+
while (!Serial);
46+
47+
if (!ECCX08.begin()) {
48+
Serial.println("No ECCX08 present!");
49+
while (1);
50+
}
51+
52+
// reconstruct the self signed cert
53+
ECCX08SelfSignedCert.beginReconstruction(0, 8);
54+
ECCX08SelfSignedCert.setCommonName(ECCX08.serialNumber());
55+
ECCX08SelfSignedCert.endReconstruction();
56+
57+
// Set a callback to get the current time
58+
// used to validate the servers certificate
59+
ArduinoBearSSL.onGetTime(getTime);
60+
61+
// Set the ECCX08 slot to use for the private key
62+
// and the accompanying public certificate for it
63+
sslClient.setEccSlot(0, ECCX08SelfSignedCert.bytes(), ECCX08SelfSignedCert.length());
64+
65+
// Set the client id used for MQTT as the device id
66+
mqttClient.setId(deviceId);
67+
68+
// Set the username to "<broker>/<device id>/api-version=2018-06-30" and empty password
69+
String username;
70+
71+
username += broker;
72+
username += "/";
73+
username += deviceId;
74+
username += "/api-version=2018-06-30";
75+
76+
mqttClient.setUsernamePassword(username, "");
77+
78+
// Set the message callback, this function is
79+
// called when the MQTTClient receives a message
80+
mqttClient.onMessage(onMessageReceived);
81+
}
82+
83+
void loop() {
84+
if (nbAccess.status() != NB_READY || gprs.status() != GPRS_READY) {
85+
connectNB();
86+
}
87+
88+
if (!mqttClient.connected()) {
89+
// MQTT client is disconnected, connect
90+
connectMQTT();
91+
}
92+
93+
// poll for new MQTT messages and send keep alives
94+
mqttClient.poll();
95+
96+
// publish a message roughly every 5 seconds.
97+
if (millis() - lastMillis > 5000) {
98+
lastMillis = millis();
99+
100+
publishMessage();
101+
}
102+
}
103+
104+
unsigned long getTime() {
105+
// get the current time from the cellular module
106+
return nbAccess.getTime();
107+
}
108+
109+
void connectNB() {
110+
Serial.println("Attempting to connect to the cellular network");
111+
112+
while ((nbAccess.begin(pinnumber) != NB_READY) ||
113+
(gprs.attachGPRS() != GPRS_READY)) {
114+
// failed, retry
115+
Serial.print(".");
116+
delay(1000);
117+
}
118+
119+
Serial.println("You're connected to the cellular network");
120+
Serial.println();
121+
}
122+
123+
void connectMQTT() {
124+
Serial.print("Attempting to MQTT broker: ");
125+
Serial.print(broker);
126+
Serial.println(" ");
127+
128+
while (!mqttClient.connect(broker, 8883)) {
129+
// failed, retry
130+
Serial.print(".");
131+
Serial.println(mqttClient.connectError());
132+
delay(5000);
133+
}
134+
Serial.println();
135+
136+
Serial.println("You're connected to the MQTT broker");
137+
Serial.println();
138+
139+
// subscribe to a topic
140+
mqttClient.subscribe("devices/" + deviceId + "/messages/devicebound/#");
141+
}
142+
143+
void publishMessage() {
144+
Serial.println("Publishing message");
145+
146+
// send message, the Print interface can be used to set the message contents
147+
mqttClient.beginMessage("devices/" + deviceId + "/messages/events/");
148+
mqttClient.print("hello ");
149+
mqttClient.print(millis());
150+
mqttClient.endMessage();
151+
}
152+
153+
void onMessageReceived(int messageSize) {
154+
// we received a message, print out the topic and contents
155+
Serial.print("Received a message with topic '");
156+
Serial.print(mqttClient.messageTopic());
157+
Serial.print("', length ");
158+
Serial.print(messageSize);
159+
Serial.println(" bytes:");
160+
161+
// use the Stream interface to print the contents
162+
while (mqttClient.available()) {
163+
Serial.print((char)mqttClient.read());
164+
}
165+
Serial.println();
166+
167+
Serial.println();
168+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// NB settings
2+
#define SECRET_PINNUMBER ""
3+
4+
// Fill in the hostname of your Azure IoT Hub broker
5+
#define SECRET_BROKER "<hub name>.azure-devices.net"
6+
7+
// Fill in the device id
8+
#define SECRET_DEVICE_ID "<device id>"

0 commit comments

Comments
 (0)