|
| 1 | +#include <ArduinoBearSSL.h> |
| 2 | +#include <ArduinoECCX08.h> |
| 3 | +#include <ArduinoMqttClient.h> |
| 4 | +#include <Arduino_ConnectionHandler.h> |
| 5 | +#include <Arduino_JSON.h> |
| 6 | +#include <NTPClient.h> |
| 7 | +#include <mbed_mktime.h> |
| 8 | + |
| 9 | +#include "arduino_secrets.h" |
| 10 | + |
| 11 | +// Enter your sensitive data in arduino_secrets.h |
| 12 | +constexpr char broker[] { SECRET_BROKER }; |
| 13 | +constexpr unsigned port { SECRET_PORT }; |
| 14 | +const char* certificate { SECRET_CERTIFICATE }; |
| 15 | + |
| 16 | +#include <Ethernet.h> |
| 17 | +#include <EthernetUdp.h> |
| 18 | +EthernetConnectionHandler conMan; |
| 19 | +EthernetClient tcpClient; |
| 20 | +EthernetUDP NTPUdp; |
| 21 | + |
| 22 | +NTPClient timeClient(NTPUdp); |
| 23 | +BearSSLClient sslClient(tcpClient); |
| 24 | +MqttClient mqttClient(sslClient); |
| 25 | + |
| 26 | +unsigned long lastMillis { 0 }; |
| 27 | + |
| 28 | +void setup() |
| 29 | +{ |
| 30 | + Serial.begin(115200); |
| 31 | + |
| 32 | + // Wait for Serial Monitor or start after 2.5s |
| 33 | + for (const auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(250)); |
| 34 | + |
| 35 | + // Set the callbacks for connectivity management |
| 36 | + conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); |
| 37 | + conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); |
| 38 | + conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); |
| 39 | + |
| 40 | + // Check for HSM |
| 41 | + if (!ECCX08.begin()) { |
| 42 | + Serial.println("No ECCX08 present!"); |
| 43 | + while (1) |
| 44 | + ; |
| 45 | + } |
| 46 | + |
| 47 | + // Configure TLS to use HSM and the key/certificate pair |
| 48 | + ArduinoBearSSL.onGetTime(getTime); |
| 49 | + sslClient.setEccSlot(0, certificate); |
| 50 | + // mqttClient.setId("Your Thing ID"); |
| 51 | + mqttClient.onMessage(onMessageReceived); |
| 52 | + |
| 53 | + timeClient.begin(); |
| 54 | +} |
| 55 | + |
| 56 | +void loop() |
| 57 | +{ |
| 58 | + // Automatically manage connectivity |
| 59 | + const auto conStatus = conMan.check(); |
| 60 | + |
| 61 | + if (conStatus != NetworkConnectionState::CONNECTED) |
| 62 | + return; |
| 63 | + |
| 64 | + if (!mqttClient.connected()) { |
| 65 | + // MQTT client is disconnected, connect |
| 66 | + connectMQTT(); |
| 67 | + } |
| 68 | + |
| 69 | + // poll for new MQTT messages and send keep alives |
| 70 | + mqttClient.poll(); |
| 71 | + |
| 72 | + // publish a message roughly every 5 seconds. |
| 73 | + if (millis() - lastMillis > 5000) { |
| 74 | + lastMillis = millis(); |
| 75 | + |
| 76 | + publishMessage(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void setNtpTime() |
| 81 | +{ |
| 82 | + timeClient.forceUpdate(); |
| 83 | + const auto epoch = timeClient.getEpochTime(); |
| 84 | + set_time(epoch); |
| 85 | +} |
| 86 | + |
| 87 | +unsigned long getTime() |
| 88 | +{ |
| 89 | + const auto now = time(NULL); |
| 90 | + return now; |
| 91 | +} |
| 92 | + |
| 93 | +void connectMQTT() |
| 94 | +{ |
| 95 | + Serial.print("Attempting to MQTT broker: "); |
| 96 | + Serial.print(broker); |
| 97 | + Serial.print(":"); |
| 98 | + Serial.print(port); |
| 99 | + Serial.println(); |
| 100 | + |
| 101 | + int status; |
| 102 | + while ((status = mqttClient.connect(broker, port)) == 0) { |
| 103 | + // failed, retry |
| 104 | + Serial.println(status); |
| 105 | + delay(1000); |
| 106 | + } |
| 107 | + Serial.println(); |
| 108 | + |
| 109 | + Serial.println("You're connected to the MQTT broker"); |
| 110 | + Serial.println(); |
| 111 | + |
| 112 | + // subscribe to a topic with QoS 1 |
| 113 | + constexpr char incomingTopic[] { "arduino/incoming" }; |
| 114 | + constexpr int incomingQoS { 1 }; |
| 115 | + Serial.print("Subscribing to topic: "); |
| 116 | + Serial.print(incomingTopic); |
| 117 | + Serial.print(" with QoS "); |
| 118 | + Serial.println(incomingQoS); |
| 119 | + mqttClient.subscribe(incomingTopic, incomingQoS); |
| 120 | +} |
| 121 | + |
| 122 | +void publishMessage() |
| 123 | +{ |
| 124 | + Serial.println("Publishing message"); |
| 125 | + |
| 126 | + JSONVar payload; |
| 127 | + String msg = "Hello, World! "; |
| 128 | + msg += millis(); |
| 129 | + payload["message"] = msg; |
| 130 | + |
| 131 | + JSONVar message; |
| 132 | + message["ts"] = static_cast<unsigned long>(time(nullptr)); |
| 133 | + message["payload"] = payload; |
| 134 | + |
| 135 | + String messageString = JSON.stringify(message); |
| 136 | + Serial.println(messageString); |
| 137 | + |
| 138 | + // send message, the Print interface can be used to set the message contents |
| 139 | + constexpr char outgoingTopic[] { "arduino/outgoing" }; |
| 140 | + |
| 141 | + mqttClient.beginMessage(outgoingTopic); |
| 142 | + mqttClient.print(messageString); |
| 143 | + mqttClient.endMessage(); |
| 144 | +} |
| 145 | + |
| 146 | +void onMessageReceived(int messageSize) |
| 147 | +{ |
| 148 | + // we received a message, print out the topic and contents |
| 149 | + Serial.println(); |
| 150 | + Serial.print("Received a message with topic '"); |
| 151 | + Serial.print(mqttClient.messageTopic()); |
| 152 | + Serial.print("', length "); |
| 153 | + Serial.print(messageSize); |
| 154 | + Serial.println(" bytes:"); |
| 155 | + |
| 156 | + /* |
| 157 | + // Message from AWS MQTT Test Client |
| 158 | + { |
| 159 | + "message": "Hello from AWS IoT console" |
| 160 | + } |
| 161 | + */ |
| 162 | + |
| 163 | + char bytes[messageSize] {}; |
| 164 | + for (int i = 0; i < messageSize; i++) |
| 165 | + bytes[i] = mqttClient.read(); |
| 166 | + |
| 167 | + JSONVar jsonMessage = JSON.parse(bytes); |
| 168 | + auto text = jsonMessage["message"]; |
| 169 | + |
| 170 | + Serial.print("["); |
| 171 | + Serial.print(time(nullptr)); |
| 172 | + Serial.print("] "); |
| 173 | + Serial.print("Message: "); |
| 174 | + Serial.println(text); |
| 175 | + |
| 176 | + Serial.println(); |
| 177 | +} |
| 178 | + |
| 179 | +void onNetworkConnect() |
| 180 | +{ |
| 181 | + Serial.println(">>>> CONNECTED to network"); |
| 182 | + printEthernetStatus(); |
| 183 | + |
| 184 | + setNtpTime(); |
| 185 | + connectMQTT(); |
| 186 | +} |
| 187 | + |
| 188 | +void onNetworkDisconnect() |
| 189 | +{ |
| 190 | + Serial.println(">>>> DISCONNECTED from network"); |
| 191 | +} |
| 192 | + |
| 193 | +void onNetworkError() |
| 194 | +{ |
| 195 | + Serial.println(">>>> ERROR"); |
| 196 | +} |
| 197 | + |
| 198 | +void printEthernetStatus() |
| 199 | +{ |
| 200 | + // print your board's IP address: |
| 201 | + Serial.print("Local IP: "); |
| 202 | + Serial.println(Ethernet.localIP()); |
| 203 | + Serial.print("Local GW: "); |
| 204 | + Serial.println(Ethernet.gatewayIP()); |
| 205 | + Serial.println(); |
| 206 | +} |
0 commit comments