|
| 1 | +/* |
| 2 | + AWS IoT WiFi |
| 3 | +
|
| 4 | + This sketch securely connects to an AWS IoT using MQTT over WiFi. |
| 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 | + - Arduino MKR WiFi 1010 or MKR1000 |
| 14 | +
|
| 15 | + This example code is in the public domain. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include <ArduinoBearSSL.h> |
| 19 | +#include <ArduinoECCX08.h> |
| 20 | +#include <ArduinoMqttClient.h> |
| 21 | +#include <WiFiNINA.h> // change to #include <WiFi101.h> for MKR1000 |
| 22 | + |
| 23 | +#include "arduino_secrets.h" |
| 24 | + |
| 25 | +/////// Enter your sensitive data in arduino_secrets.h |
| 26 | +const char ssid[] = SECRET_SSID; |
| 27 | +const char pass[] = SECRET_PASS; |
| 28 | +const char broker[] = SECRET_BROKER; |
| 29 | +const char* certificate = SECRET_CERTIFICATE; |
| 30 | + |
| 31 | +WiFiClient wifiClient; // Used for the TCP socket connection |
| 32 | +BearSSLClient sslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECC508 |
| 33 | +MqttClient mqttClient(sslClient); |
| 34 | + |
| 35 | +unsigned long lastMillis = 0; |
| 36 | + |
| 37 | +void setup() { |
| 38 | + Serial.begin(115200); |
| 39 | + while (!Serial); |
| 40 | + |
| 41 | + if (!ECCX08.begin()) { |
| 42 | + Serial.println("No ECCX08 present!"); |
| 43 | + while (1); |
| 44 | + } |
| 45 | + |
| 46 | + // Set a callback to get the current time |
| 47 | + // used to validate the servers certificate |
| 48 | + ArduinoBearSSL.onGetTime(getTime); |
| 49 | + |
| 50 | + // Set the ECCX08 slot to use for the private key |
| 51 | + // and the accompanying public certificate for it |
| 52 | + sslClient.setEccSlot(0, certificate); |
| 53 | + |
| 54 | + // Optional, set the client id used for MQTT, |
| 55 | + // each device that is connected to the broker |
| 56 | + // must have a unique client id. The MQTTClient will generate |
| 57 | + // a client id for you based on the millis() value if not set |
| 58 | + // |
| 59 | + // mqttClient.setId("clientId"); |
| 60 | + |
| 61 | + // Set the message callback, this function is |
| 62 | + // called when the MQTTClient receives a message |
| 63 | + mqttClient.onMessage(onMessageReceived); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + if (WiFi.status() != WL_CONNECTED) { |
| 68 | + connectWiFi(); |
| 69 | + } |
| 70 | + |
| 71 | + if (!mqttClient.connected()) { |
| 72 | + // MQTT client is disconnected, connect |
| 73 | + connectMQTT(); |
| 74 | + } |
| 75 | + |
| 76 | + // poll for new MQTT messages and send keep alives |
| 77 | + mqttClient.poll(); |
| 78 | + |
| 79 | + // publish a message roughly every 5 seconds. |
| 80 | + if (millis() - lastMillis > 5000) { |
| 81 | + lastMillis = millis(); |
| 82 | + |
| 83 | + publishMessage(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +unsigned long getTime() { |
| 88 | + // get the current time from the WiFi module |
| 89 | + return WiFi.getTime(); |
| 90 | +} |
| 91 | + |
| 92 | +void connectWiFi() { |
| 93 | + Serial.print("Attempting to connect to SSID: "); |
| 94 | + Serial.print(ssid); |
| 95 | + Serial.print(" "); |
| 96 | + |
| 97 | + while (WiFi.begin(ssid, pass) != WL_CONNECTED) { |
| 98 | + // failed, retry |
| 99 | + Serial.print("."); |
| 100 | + delay(5000); |
| 101 | + } |
| 102 | + Serial.println(); |
| 103 | + |
| 104 | + Serial.println("You're connected to the network"); |
| 105 | + Serial.println(); |
| 106 | +} |
| 107 | + |
| 108 | +void connectMQTT() { |
| 109 | + Serial.print("Attempting to MQTT broker: "); |
| 110 | + Serial.print(broker); |
| 111 | + Serial.println(" "); |
| 112 | + |
| 113 | + while (!mqttClient.connect(broker, 8883)) { |
| 114 | + // failed, retry |
| 115 | + Serial.print("."); |
| 116 | + delay(5000); |
| 117 | + } |
| 118 | + Serial.println(); |
| 119 | + |
| 120 | + Serial.println("You're connected to the MQTT broker"); |
| 121 | + Serial.println(); |
| 122 | + |
| 123 | + // subscribe to a topic |
| 124 | + mqttClient.subscribe("arduino/incoming"); |
| 125 | +} |
| 126 | + |
| 127 | +void publishMessage() { |
| 128 | + Serial.println("Publishing message"); |
| 129 | + |
| 130 | + // send message, the Print interface can be used to set the message contents |
| 131 | + mqttClient.beginMessage("arduino/outgoing"); |
| 132 | + mqttClient.print("hello "); |
| 133 | + mqttClient.print(millis()); |
| 134 | + mqttClient.endMessage(); |
| 135 | +} |
| 136 | + |
| 137 | +void onMessageReceived(int messageSize) { |
| 138 | + // we received a message, print out the topic and contents |
| 139 | + Serial.print("Received a message with topic '"); |
| 140 | + Serial.print(mqttClient.messageTopic()); |
| 141 | + Serial.print("', length "); |
| 142 | + Serial.print(messageSize); |
| 143 | + Serial.println(" bytes:"); |
| 144 | + |
| 145 | + // use the Stream interface to print the contents |
| 146 | + while (mqttClient.available()) { |
| 147 | + Serial.print((char)mqttClient.read()); |
| 148 | + } |
| 149 | + Serial.println(); |
| 150 | + |
| 151 | + Serial.println(); |
| 152 | +} |
0 commit comments