-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLiveObjects_Arduino_TinyGSM.ino
444 lines (375 loc) · 12.8 KB
/
LiveObjects_Arduino_TinyGSM.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright (C) 2021 Orange
*
* This software is distributed under the terms and conditions of the 'BSD-3-Clause'
* license which can be found in the file 'LICENSE' in this package distribution
* or at 'https://opensource.org/licenses/BSD-3-Clause'.
*/
/**
* @file LiveObjects_Arduino_TinyGSM.ino
* @brief Advanced Arduino IoT SAFE which use a preloaded key and certificate to
* connect to LiveObjects.
*/
#define DEFAULT_TX_FREQUENCY 60
#include "arduino_secrets.h"
#include <ArduinoBearSSL.h>
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>
#include <TimeLib.h>
#define TINY_GSM_MODEM_SEQUANS_MONARCH
//#define DUMP_AT_COMMANDS
#ifdef STM32WB55xx
HardwareSerial SerialAT(D0, D1);
#else // default serial port hardware is working fine with Orange LoRa Explorer
#define SerialAT SERIAL_PORT_HARDWARE
#endif
#define SERIAL_AT_SPEED 115200 // 921600 by default, use AT+IPR to decrease it
#include <TinyGsmClient.h>
#include "IoTSAFE.h"
// Use a custom AID
static const uint8_t IOT_SAFE_CUSTOM_AID[] = {
0xA0, 0x00, 0x00, 0x02, 0x48, 0x04, 0x00
};
// Define the private key ID inside the IoT SAFE applet
static const uint8_t IOT_SAFE_PRIVATE_KEY_ID[] = { 0x01 };
// Define the certificate file ID inside the IoT SAFE applet
static const uint8_t IOT_SAFE_CLIENT_CERTIFICATE_FILE_ID[] = { 0x02 };
const char mqtt_user[] = SECRET_MQTTUSER;
const char mqtt_pass[] = SECRET_MQTTPASS;
const char mqtt_broker[] = "liveobjects.orange-business.com";
int mqtt_port = 8883;
const char mqtt_pubdata[] = "dev/data";
const char mqtt_pubcfg[] = "dev/cfg";
const char mqtt_subcfg[] = "dev/cfg/upd";
const char mqtt_subcmd[] = "dev/cmd";
const char* JSONdata = "{\"model\":\"github_sample_MKR\",\"value\":{\"uptime\":0}}";
const char* JSONcfg= "{\"cfg\":{\"transmit frequency (s)\":{\"t\":\"u32\",\"v\":0}}}";
uint32_t transmissionFrequency = DEFAULT_TX_FREQUENCY * 1000;
uint32_t lastTransmission = DEFAULT_TX_FREQUENCY * 1000;
uint32_t uptimeInSec = 0;
#ifdef _VARIANT_SODAQ_EXPLORER_
float temp = 0;
#endif
StaticJsonDocument<350> payload;
// Live Objects certificate (because certificate is validated by Rich OS)
static const unsigned char TA0_DN[] = {
0x30, 0x61, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
0x02, 0x55, 0x53, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A,
0x13, 0x0C, 0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x20, 0x49,
0x6E, 0x63, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13,
0x10, 0x77, 0x77, 0x77, 0x2E, 0x64, 0x69, 0x67, 0x69, 0x63, 0x65, 0x72,
0x74, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x20, 0x30, 0x1E, 0x06, 0x03, 0x55,
0x04, 0x03, 0x13, 0x17, 0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74,
0x20, 0x47, 0x6C, 0x6F, 0x62, 0x61, 0x6C, 0x20, 0x52, 0x6F, 0x6F, 0x74,
0x20, 0x43, 0x41
};
static const unsigned char TA0_RSA_N[] = {
0xE2, 0x3B, 0xE1, 0x11, 0x72, 0xDE, 0xA8, 0xA4, 0xD3, 0xA3, 0x57, 0xAA,
0x50, 0xA2, 0x8F, 0x0B, 0x77, 0x90, 0xC9, 0xA2, 0xA5, 0xEE, 0x12, 0xCE,
0x96, 0x5B, 0x01, 0x09, 0x20, 0xCC, 0x01, 0x93, 0xA7, 0x4E, 0x30, 0xB7,
0x53, 0xF7, 0x43, 0xC4, 0x69, 0x00, 0x57, 0x9D, 0xE2, 0x8D, 0x22, 0xDD,
0x87, 0x06, 0x40, 0x00, 0x81, 0x09, 0xCE, 0xCE, 0x1B, 0x83, 0xBF, 0xDF,
0xCD, 0x3B, 0x71, 0x46, 0xE2, 0xD6, 0x66, 0xC7, 0x05, 0xB3, 0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 0xA3,
0x90, 0xC3, 0xAF, 0x27
};
static const unsigned char TA0_RSA_E[] = {
0x01, 0x00, 0x01
};
static const br_x509_trust_anchor TAs[1] = {
{
{ (unsigned char *)TA0_DN, sizeof TA0_DN },
BR_X509_TA_CA,
{
BR_KEYTYPE_RSA,
{ .rsa = {
(unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,
(unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,
} }
}
}
};
#ifdef _VARIANT_SODAQ_EXPLORER_
void RED() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
void GREEN() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
void BLUE() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, LOW);
}
#elif defined(STM32WB55xx)
void RED() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
}
void GREEN() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, LOW);
}
void BLUE() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
#else
void RED() {
}
void GREEN() {
}
void BLUE() {
}
#endif
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SERIAL_PORT_MONITOR);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient nbClient(modem);
IoTSAFE iotSAFE(IOT_SAFE_CUSTOM_AID, sizeof(IOT_SAFE_CUSTOM_AID));
IoTSAFECertificate client_certificate;
BearSSLClient sslClient(nbClient,TAs,1);
MqttClient mqttClient(sslClient);
void connectionManager(bool _way);
void onMessageReceived(int messageSize);
void updateConfig();
extern "C" {
/**
* _gettimeofday() is called from time() which is used by srand() to generate
* random number. It is defined here in case this function is not defined in
* library.
*/
int __attribute__((weak)) _gettimeofday(struct timeval *tv, void *tz)
{
(void)tv;
(void)tz;
return 0;
}
}
size_t iotSafeSign(const br_ec_impl *impl, const br_hash_class *hf,
const void *hash_value, const br_ec_private_key *sk, void *sig)
{
return iotSAFE.sign(IOT_SAFE_PRIVATE_KEY_ID, sizeof(IOT_SAFE_PRIVATE_KEY_ID),
impl, hf, hash_value, sk, sig);
}
unsigned long getTime()
{
SERIAL_PORT_MONITOR.println("Get time");
modem.getGSMDateTime(DATE_FULL);
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
float timezone = 0;
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &timezone);
tmElements_t tm;
tm.Year = CalendarYrToTm(year);
tm.Month = month;
tm.Day = day;
tm.Hour = hour;
tm.Minute = minute;
tm.Second = second;
return makeTime(tm);
}
void setup() {
SERIAL_PORT_MONITOR.begin(115200);
//while (!SERIAL_PORT_MONITOR);
SERIAL_PORT_MONITOR.println("Start sample...");
SerialAT.begin(SERIAL_AT_SPEED);
SERIAL_PORT_MONITOR.println("Initializing modem...");
while (!SerialAT || !modem.init()) {
SERIAL_PORT_MONITOR.println("Init failed retrying ...");
delay(1000);
}
// Set a callback to get the current time
// used to validate the servers certificate
ArduinoBearSSL.onGetTime(getTime);
sslClient.setEccSign(iotSafeSign);
mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);
mqttClient.onMessage(onMessageReceived);
#ifdef _VARIANT_SODAQ_EXPLORER_
// Define temperature pin as input
pinMode(TEMP_SENSOR, INPUT);
#endif
#if defined(_VARIANT_SODAQ_EXPLORER_) || defined(STM32WB55xx)
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
RED();
#endif
connectionManager(1);
updateConfig();
}
void loop() {
if (millis() - lastTransmission > transmissionFrequency) {
// get data from sensors
SERIAL_PORT_MONITOR.println("Sampling data");
sampleData();
// connect and send data to Live Objects
SERIAL_PORT_MONITOR.println("Sending data to Live Objects");
if (!modem.waitForNetwork() || !mqttClient.connected())
connectionManager(1);
if (modem.isNetworkConnected()) {
SERIAL_PORT_MONITOR.println("Network connected");
}
sendData();
}
delay (1000);
mqttClient.poll();
}
void connectionManager(bool _way = 1) {
switch (_way) {
case 1:
RED();
SERIAL_PORT_MONITOR.println("Connecting to cellular network");
#ifdef PIN_NUMBER
// Unlock your SIM card with a PIN if needed
if ( PIN_NUMBER && modem.getSimStatus() != 3 ) {
modem.simUnlock(PIN_NUMBER);
}
#endif
SERIAL_PORT_MONITOR.print(".");
if (!modem.waitForNetwork()) {
SERIAL_PORT_MONITOR.println(" Unable to connect to network!");
return;
}
SERIAL_PORT_MONITOR.println("You're connected to the network");
SERIAL_PORT_MONITOR.print("Connecting to MQTT broker '");
SERIAL_PORT_MONITOR.print(mqtt_broker);
SERIAL_PORT_MONITOR.println("'");
while (true) {
// OBKG process can be triggered by OTA and can take some time on the applet as:
// - a new key pair must be generated,
// - the CSR must be send through OTA
// - the certificate must be send back by OTA
SERIAL_PORT_MONITOR.println("Waiting 25 seconds to let time for the IoT SAFE OBKG process");
delay(25000);
client_certificate =
iotSAFE.readCertificate(IOT_SAFE_CLIENT_CERTIFICATE_FILE_ID,
sizeof(IOT_SAFE_CLIENT_CERTIFICATE_FILE_ID));
sslClient.setEccCert(client_certificate.getCertificate());
mqttClient.setId(client_certificate.getCertificateCommonName());
SERIAL_PORT_MONITOR.print("CertificateCommonName: ");
SERIAL_PORT_MONITOR.println(client_certificate.getCertificateCommonName());
if (!mqttClient.connect(mqtt_broker, mqtt_port))
SERIAL_PORT_MONITOR.println("Unable to connect, retry later");
else
break;
}
SERIAL_PORT_MONITOR.println("You're connected to the MQTT broker");
SERIAL_PORT_MONITOR.println();
GREEN();
mqttClient.subscribe(mqtt_subcfg);
mqttClient.subscribe(mqtt_subcmd);
mqttClient.poll();
break;
case 0:
SERIAL_PORT_MONITOR.println("Closing MQTT connection...");
mqttClient.stop();
SERIAL_PORT_MONITOR.println("Disconnecting from cellular network...");
modem.gprsDisconnect();
SERIAL_PORT_MONITOR.println("Offline.\n");
break;
}
}
void publishMessage(const char* topic, const char* _buffer) {
BLUE();
SERIAL_PORT_MONITOR.print("Publishing message on topic '");
mqttClient.beginMessage(topic);
mqttClient.print(_buffer);
mqttClient.endMessage();
SERIAL_PORT_MONITOR.print(topic);
SERIAL_PORT_MONITOR.println("':");
SERIAL_PORT_MONITOR.println(_buffer);
delay(5000);
GREEN();
}
void onMessageReceived(int messageSize) {
BLUE();
String topic = mqttClient.messageTopic();
SERIAL_PORT_MONITOR.print("Received a message with topic '");
SERIAL_PORT_MONITOR.print(topic);
SERIAL_PORT_MONITOR.println("':");
char _buffer[300];
byte i=0;
while (mqttClient.available())
_buffer[i++] = (char)mqttClient.read();
_buffer[i]=0;
SERIAL_PORT_MONITOR.println(_buffer);
payload.clear();
deserializeJson(payload, _buffer);
if (topic == F("dev/cfg/upd"))
updateConfig();
else if (topic == F("dev/cmd"))
command();
GREEN();
}
void command() {
lastTransmission = 0;
connectionManager(0);
modem.restart();
delay(5000);
}
void updateConfig() {
if (payload.containsKey(F("cid"))) {
if (payload[F("cfg")].containsKey(F("transmit frequency (s)"))) {
transmissionFrequency = payload[F("cfg")][F("transmit frequency (s)")][F("v")].as<int>() * 1000;
payload[F("cfg")][F("transmit frequency (s)")][F("v")] = transmissionFrequency / 1000;
}
}
else {
payload.clear();
deserializeJson(payload, JSONcfg);
payload[F("cfg")][F("transmit frequency (s)")][F("v")] = transmissionFrequency / 1000;
}
char _buffer[300];
serializeJson(payload, _buffer);
publishMessage(mqtt_pubcfg, _buffer);
}
void sampleData() {
uptimeInSec = millis()/1000;
#ifdef _VARIANT_SODAQ_EXPLORER_
// 10mV per C, 0C is 500mV
float mVolts = (float)analogRead(TEMP_SENSOR) * 3300.0 / 1023.0;
temp = (mVolts - 500.0) / 10.0;
#endif
}
void sendData() {
payload.clear();
deserializeJson(payload, JSONdata);
payload[F("value")][F("uptime")] = uptimeInSec;
#ifdef _VARIANT_SODAQ_EXPLORER_
payload[F("value")][F("temperature")] = temp;
#endif
char _buffer[300];
serializeJson(payload, _buffer);
publishMessage(mqtt_pubdata, _buffer);
lastTransmission = millis();
}