Skip to content

Commit 238469d

Browse files
committed
support SSL module certificates management
1 parent 209ec18 commit 238469d

File tree

7 files changed

+523
-10
lines changed

7 files changed

+523
-10
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
ArduinoMqttClient - SSLCertificateManagement_Example
3+
4+
This example shows how to upload a self signed certificate
5+
by GSMSSLClient's APIs and connects to a MQTT broker
6+
and publishes a message to a topic once a second .
7+
8+
The circuit:
9+
- Arduino MKRGSM1400
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <ArduinoMqttClient.h>
15+
#include <MKRGSM.h>
16+
#include "arduino_secrets.h"
17+
18+
19+
const char PINNUMBER[] = SECRET_PINNUMBER;
20+
// APN data
21+
const char GPRS_APN[] = SECRET_GPRS_APN;
22+
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
23+
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
24+
25+
// initialize the library instance
26+
GSMSSLClient client;
27+
GPRS gprs;
28+
GSM gsmAccess(true);
29+
MqttClient mqttClient(client);
30+
31+
// replace with your brooker, port and topic
32+
const char broker[] = "";
33+
int port = 8883;
34+
const char topic[] = "";
35+
36+
const long interval = 1000;
37+
unsigned long previousMillis = 0;
38+
39+
int count = 0;
40+
41+
42+
void setup() {
43+
//Initialize serial and wait for port to open:
44+
Serial.begin(9600);
45+
while (!Serial) {
46+
; // wait for serial port to connect. Needed for native USB port only
47+
}
48+
49+
bool connected = false;
50+
// After starting the modem with GSM.begin()
51+
// attach the shield to the GPRS network with the APN, login and password
52+
while (!connected) {
53+
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
54+
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
55+
connected = true;
56+
} else {
57+
Serial.println("Not connected");
58+
delay(1000);
59+
}
60+
}
61+
62+
Serial.println("You're connected to the network");
63+
Serial.println();
64+
// uncomment this and fill the array in Arduino_secrets if you want tou
65+
// use your roots certificate pay attention this metod call an errase
66+
// for any of the certs stored on the module flash after the first time
67+
// the erase funciotn no longer works after the first run because erase
68+
// only the standard cert used by arduino mrgsm+
69+
70+
//client.setUserRoots(SECRET_GSM_ROOT_CERTS, SECRET_GSM_ROOT_SIZE);
71+
client.setPrivateCertificate(SECRET_CERT, "MKRGSM01", sizeof(SECRET_CERT));
72+
client.setPrivateKey(SECRET_KEY, "MKRGSMKEY01", sizeof(SECRET_KEY));
73+
client.setClientName("MKRGSM01");
74+
client.setKeyName("MKRGSMKEY01");
75+
client.setServerName("Let_s_Encrypt_Authority_X3");
76+
client.setProfileSSL(1);
77+
78+
79+
Serial.print("Attempting to connect to the MQTT broker: ");
80+
Serial.println(broker);
81+
82+
if (!mqttClient.connect(broker, port)) {
83+
84+
Serial.print("MQTT connection failed! Error code = ");
85+
Serial.println(mqttClient.connectError());
86+
87+
while (1);
88+
}
89+
90+
Serial.println("You're connected to the MQTT broker!");
91+
Serial.println();
92+
93+
94+
}
95+
96+
void loop() {
97+
// call poll() regularly to allow the library to send MQTT keep alives which
98+
// avoids being disconnected by the broker
99+
mqttClient.poll();
100+
101+
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
102+
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
103+
unsigned long currentMillis = millis();
104+
105+
if (currentMillis - previousMillis >= interval) {
106+
// save the last time a message was sent
107+
previousMillis = currentMillis;
108+
109+
Serial.print("Sending message to topic: ");
110+
Serial.println(topic);
111+
Serial.print("hello ");
112+
Serial.println(count);
113+
114+
// send message, the Print interface can be used to set the message contents
115+
mqttClient.beginMessage(topic);
116+
mqttClient.print("hello ");
117+
mqttClient.print(count);
118+
if (!mqttClient.endMessage()) {
119+
Serial.println("transmission error");
120+
}
121+
122+
Serial.println();
123+
124+
count++;
125+
}
126+
}

0 commit comments

Comments
 (0)