Skip to content

Commit e10673e

Browse files
committed
Add AWS IoT GSM example
1 parent 085ce90 commit e10673e

File tree

2 files changed

+173
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)