Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork27
Added an example for unor4 for AWS Iot core#24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
andreagilardoni wants to merge1 commit intoarduino:masterChoose a base branch fromandreagilardoni:unor4-example
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
230 changes: 230 additions & 0 deletionsexamples/AWS IoT/AWS_IoT_Unor4/AWS_IoT_Giga_WiFi.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| /* | ||
| AWS IoT WiFi | ||
| This sketch securely connects to an AWS IoT using MQTT over WiFi. | ||
| It uses a a WiFiSSLClient configured with a private key and | ||
| a public certificate for SSL/TLS authentication. | ||
| Use openssl to generate a compatible prive key (prime256v1) and | ||
| a CSR to upload to AWS IoT core | ||
| # openssl ecparam -name prime256v1 -genkey -noout -out private-key.pem | ||
| # openssl req -new -key private-key.pem -out csr.csr -days 3650 | ||
| It publishes a message every 5 seconds to arduino/outgoing | ||
| topic and subscribes to messages on the arduino/incoming | ||
| topic. | ||
| This example code is in the public domain. | ||
| */ | ||
| #include <ArduinoMqttClient.h> | ||
| #include <Arduino_ConnectionHandler.h> | ||
| #include <Arduino_JSON.h> | ||
| #include <NTPClient.h> | ||
| #include <WiFi.h> | ||
| #include <WiFiUdp.h> | ||
| #include <time.h> | ||
| #include <RTC.h> | ||
| #include "arduino_secrets.h" | ||
| // Enter your sensitive data in arduino_secrets.h | ||
| constexpr char broker[] { SECRET_BROKER }; | ||
| constexpr unsigned port { SECRET_PORT }; | ||
| const char* certificate { SECRET_CERTIFICATE }; | ||
| const char* privateKey { PRIVATE_KEY }; | ||
| constexpr char ssid[] { SECRET_SSID }; | ||
| constexpr char pass[] { SECRET_PASS }; | ||
| WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS); | ||
| WiFiUDP NTPUdp; | ||
| NTPClient timeClient(NTPUdp); | ||
| WiFiSSLClient sslClient; | ||
| MqttClient mqttClient(sslClient); | ||
| unsigned long lastMillis { 0 }; | ||
| void setup() | ||
| { | ||
| Serial.begin(115200); | ||
| // Wait for Serial Monitor or start after 2.5s | ||
| for (const auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(250)); | ||
| // Set the callbacks for connectivity management | ||
| conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); | ||
| conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); | ||
| conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); | ||
| // Configure TLS key/certificate pair | ||
| sslClient.setCertificate(certificate); | ||
| sslClient.setPrivateKey(privateKey); | ||
| // mqttClient.setId("Your Thing ID"); | ||
| mqttClient.onMessage(onMessageReceived); | ||
| timeClient.begin(); | ||
| } | ||
| void loop() | ||
| { | ||
| // Automatically manage connectivity | ||
| const auto conStatus = conMan.check(); | ||
| if (conStatus != NetworkConnectionState::CONNECTED) | ||
| return; | ||
| if (!mqttClient.connected()) { | ||
| // MQTT client is disconnected, connect | ||
| connectMQTT(); | ||
| } | ||
| // poll for new MQTT messages and send keep alives | ||
| mqttClient.poll(); | ||
| // publish a message roughly every 5 seconds. | ||
| if (millis() - lastMillis > 5000) { | ||
| lastMillis = millis(); | ||
| publishMessage(); | ||
| } | ||
| } | ||
| void setNtpTime() | ||
| { | ||
| timeClient.forceUpdate(); | ||
| const auto epoch = timeClient.getEpochTime(); | ||
| set_time(epoch); | ||
| } | ||
| unsigned long getTime() | ||
| { | ||
| const auto now = time(NULL); | ||
| return now; | ||
| } | ||
| void connectMQTT() | ||
| { | ||
| Serial.print("Attempting to MQTT broker: "); | ||
| Serial.print(broker); | ||
| Serial.print(":"); | ||
| Serial.print(port); | ||
| Serial.println(); | ||
| int status; | ||
| while ((status = mqttClient.connect(broker, port)) == 0) { | ||
| // failed, retry | ||
| Serial.println(status); | ||
| delay(100000); | ||
| } | ||
| Serial.println(); | ||
| Serial.println("You're connected to the MQTT broker"); | ||
| Serial.println(); | ||
| // subscribe to a topic with QoS 1 | ||
| constexpr char incomingTopic[] { "arduino/incoming" }; | ||
| constexpr MqttQos incomingQoS = MqttQos1; | ||
| Serial.print("Subscribing to topic: "); | ||
| Serial.print(incomingTopic); | ||
| Serial.print(" with QoS "); | ||
| Serial.println(incomingQoS); | ||
| mqttClient.subscribe(incomingTopic, incomingQoS); | ||
| } | ||
| void publishMessage() | ||
| { | ||
| Serial.println("Publishing message"); | ||
| JSONVar payload; | ||
| String msg = "Hello, World! "; | ||
| msg += millis(); | ||
| payload["message"] = msg; | ||
| payload["rssi"] = WiFi.RSSI(); | ||
| JSONVar message; | ||
| message["ts"] = static_cast<unsigned long>(time(nullptr)); | ||
| message["payload"] = payload; | ||
| String messageString = JSON.stringify(message); | ||
| Serial.println(messageString); | ||
| // send message, the Print interface can be used to set the message contents | ||
| constexpr char outgoingTopic[] { "arduino/outgoing" }; | ||
| mqttClient.beginMessage(outgoingTopic); | ||
| mqttClient.print(messageString); | ||
| mqttClient.endMessage(); | ||
| } | ||
| void onMessageReceived(int messageSize) | ||
| { | ||
| // we received a message, print out the topic and contents | ||
| Serial.println(); | ||
| Serial.print("Received a message with topic '"); | ||
| Serial.print(mqttClient.messageTopic()); | ||
| Serial.print("', length "); | ||
| Serial.print(messageSize); | ||
| Serial.println(" bytes:"); | ||
| /* | ||
| // Message from AWS MQTT Test Client | ||
| { | ||
| "message": "Hello from AWS IoT console" | ||
| } | ||
| */ | ||
| char bytes[messageSize] {}; | ||
| for (int i = 0; i < messageSize; i++) | ||
| bytes[i] = mqttClient.read(); | ||
| JSONVar jsonMessage = JSON.parse(bytes); | ||
| auto text = jsonMessage["message"]; | ||
| Serial.print("["); | ||
| Serial.print(time(nullptr)); | ||
| Serial.print("] "); | ||
| Serial.print("Message: "); | ||
| Serial.println(text); | ||
| Serial.println(); | ||
| } | ||
| void onNetworkConnect() | ||
| { | ||
| Serial.println(">>>> CONNECTED to network"); | ||
| printWifiStatus(); | ||
| setNtpTime(); | ||
| connectMQTT(); | ||
| } | ||
| void onNetworkDisconnect() | ||
| { | ||
| Serial.println(">>>> DISCONNECTED from network"); | ||
| } | ||
| void onNetworkError() | ||
| { | ||
| Serial.println(">>>> ERROR"); | ||
| } | ||
| void printWifiStatus() | ||
| { | ||
| // print the SSID of the network you're attached to: | ||
| Serial.print("SSID: "); | ||
| Serial.println(WiFi.SSID()); | ||
| // print the received signal strength: | ||
| Serial.print("signal strength (RSSI):"); | ||
| Serial.print(WiFi.RSSI()); | ||
| Serial.println(" dBm"); | ||
| Serial.println(); | ||
| // print your board's IP address: | ||
| Serial.print("Local IP: "); | ||
| Serial.println(WiFi.localIP()); | ||
| Serial.print("Local GW: "); | ||
| Serial.println(WiFi.gatewayIP()); | ||
| Serial.println(); | ||
| } |
20 changes: 20 additions & 0 deletionsexamples/AWS IoT/AWS_IoT_Unor4/arduino_secrets.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Fill in your WiFi networks SSID and password | ||
| #define SECRET_SSID "" | ||
| #define SECRET_PASS "" | ||
| // Fill in the hostname of your AWS IoT broker | ||
| #define SECRET_BROKER "xxxxxxxxxxxxxx.iot.xx-xxxx-x.amazonaws.com"" | ||
| #define SECRET_PORT 8883 | ||
| // Fill in the board public certificate | ||
| const char SECRET_CERTIFICATE[] = R"( | ||
| -----BEGIN CERTIFICATE----- | ||
| -----END CERTIFICATE----- | ||
| )"; | ||
| // Fill in the board private key | ||
| const char PRIVATE_KEY[] = R"( | ||
| -----BEGIN RSA PRIVATE KEY----- | ||
| -----END RSA PRIVATE KEY----- | ||
| )"; | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.