|
| 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 | +} |
0 commit comments