Skip to content

Commit 8f31b72

Browse files
committed
Add AWS IoT NB example
1 parent e10673e commit 8f31b72

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
AWS IoT NB
3+
4+
This sketch securely connects to an AWS IoT using MQTT over NB IoT/LTE Cat M1.
5+
It uses a private key stored in the ATECC508A and a public
6+
certificate for SSL/TLS authetication.
7+
8+
It publishes a message every 5 seconds to arduino/outgoing
9+
topic and subscribes to messages on the arduino/incoming
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 <ArduinoMqttClient.h>
24+
#include <MKRNB.h>
25+
26+
#include "arduino_secrets.h"
27+
28+
/////// Enter your sensitive data in arduino_secrets.h
29+
const char pinnumber[] = SECRET_PINNUMBER;
30+
const char broker[] = SECRET_BROKER;
31+
const char* certificate = SECRET_CERTIFICATE;
32+
33+
NB nbAccess;
34+
GPRS gprs;
35+
36+
NBClient nbClient; // Used for the TCP socket connection
37+
BearSSLClient sslClient(nbClient); // Used for SSL/TLS connection, integrates with ECC508
38+
MqttClient mqttClient(sslClient);
39+
40+
unsigned long lastMillis = 0;
41+
42+
void setup() {
43+
Serial.begin(115200);
44+
while (!Serial);
45+
46+
if (!ECCX08.begin()) {
47+
Serial.println("No ECCX08 present!");
48+
while (1);
49+
}
50+
51+
// Set a callback to get the current time
52+
// used to validate the servers certificate
53+
ArduinoBearSSL.onGetTime(getTime);
54+
55+
// Set the ECCX08 slot to use for the private key
56+
// and the accompanying public certificate for it
57+
sslClient.setEccSlot(0, certificate);
58+
59+
// Optional, set the client id used for MQTT,
60+
// each device that is connected to the broker
61+
// must have a unique client id. The MQTTClient will generate
62+
// a client id for you based on the millis() value if not set
63+
//
64+
// mqttClient.setId("clientId");
65+
66+
// Set the message callback, this function is
67+
// called when the MQTTClient receives a message
68+
mqttClient.onMessage(onMessageReceived);
69+
}
70+
71+
void loop() {
72+
if (nbAccess.status() != NB_READY || gprs.status() != GPRS_READY) {
73+
connectNB();
74+
}
75+
76+
if (!mqttClient.connected()) {
77+
// MQTT client is disconnected, connect
78+
connectMQTT();
79+
}
80+
81+
// poll for new MQTT messages and send keep alives
82+
mqttClient.poll();
83+
84+
// publish a message roughly every 5 seconds.
85+
if (millis() - lastMillis > 5000) {
86+
lastMillis = millis();
87+
88+
publishMessage();
89+
}
90+
}
91+
92+
unsigned long getTime() {
93+
// get the current time from the NB module
94+
return nbAccess.getTime();
95+
}
96+
97+
void connectNB() {
98+
Serial.println("Attempting to connect to the cellular network");
99+
100+
while ((nbAccess.begin(pinnumber) != NB_READY) ||
101+
(gprs.attachGPRS() != GPRS_READY)) {
102+
// failed, retry
103+
Serial.print(".");
104+
delay(1000);
105+
}
106+
107+
Serial.println("You're connected to the cellular network");
108+
Serial.println();
109+
}
110+
111+
void connectMQTT() {
112+
Serial.print("Attempting to MQTT broker: ");
113+
Serial.print(broker);
114+
Serial.println(" ");
115+
116+
while (!mqttClient.connect(broker, 8883)) {
117+
// failed, retry
118+
Serial.print(".");
119+
delay(5000);
120+
}
121+
Serial.println();
122+
123+
Serial.println("You're connected to the MQTT broker");
124+
Serial.println();
125+
126+
// subscribe to a topic
127+
mqttClient.subscribe("arduino/incoming");
128+
}
129+
130+
void publishMessage() {
131+
Serial.println("Publishing message");
132+
133+
// send message, the Print interface can be used to set the message contents
134+
mqttClient.beginMessage("arduino/outgoing");
135+
mqttClient.print("hello ");
136+
mqttClient.print(millis());
137+
mqttClient.endMessage();
138+
}
139+
140+
void onMessageReceived(int messageSize) {
141+
// we received a message, print out the topic and contents
142+
Serial.print("Received a message with topic '");
143+
Serial.print(mqttClient.messageTopic());
144+
Serial.print("', length ");
145+
Serial.print(messageSize);
146+
Serial.println(" bytes:");
147+
148+
// use the Stream interface to print the contents
149+
while (mqttClient.available()) {
150+
Serial.print((char)mqttClient.read());
151+
}
152+
Serial.println();
153+
154+
Serial.println();
155+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// NB settings
2+
#define SECRET_PINNUMBER ""
3+
4+
// Fill in the hostname of your AWS IoT broker
5+
#define SECRET_BROKER "xxxxxxxxxxxxxx.iot.xx-xxxx-x.amazonaws.com"
6+
7+
// Fill in the boards public certificate
8+
const char SECRET_CERTIFICATE[] = R"(
9+
-----BEGIN CERTIFICATE-----
10+
11+
-----END CERTIFICATE-----
12+
)";

0 commit comments

Comments
 (0)