-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example to connecto to ArduinoIoTCloud and AWS
- Loading branch information
Showing
4 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino
This file contains 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,139 @@ | ||
/* | ||
This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud. | ||
* Connect a potentiometer (or other analog sensor) to A0. | ||
* When the potentiometer (or sensor) value changes the data is sent to the Cloud. | ||
* When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. | ||
IMPORTANT: | ||
This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. | ||
On a LoRa board, if it is configured as a class A device (default and preferred option), | ||
values from Cloud dashboard are received only after a value is sent to Cloud. | ||
The full list of compatible boards can be found here: | ||
- https://github.com/arduino-libraries/ArduinoIoTCloud#what | ||
*/ | ||
|
||
#include "thingProperties.h" | ||
|
||
unsigned long publishMillis = 0; | ||
unsigned long connectMillis = 0; | ||
|
||
void setup() { | ||
/* Initialize serial and wait up to 5 seconds for port to open */ | ||
Serial.begin(9600); | ||
while(!Serial); | ||
|
||
/* Configure LED pin as an output */ | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
|
||
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ | ||
initProperties(); | ||
|
||
/* Initialize Arduino IoT Cloud library */ | ||
ArduinoCloud.begin(ArduinoIoTPreferredConnection, true, "iot.arduino.cc"); | ||
|
||
setDebugMessageLevel(5); | ||
ArduinoCloud.printDebugInfo(); | ||
|
||
/* Initialize AWS Client */ | ||
ArduinoBearSSL.onGetTime(getTime); | ||
sslClientAWS.setEccSlot(AWS_SLOT, AWS_CERTIFICATE); | ||
|
||
mqttClientAWS.setId("ArduinoAWSClient"); | ||
mqttClientAWS.onMessage(onMessageReceived); | ||
mqttClientAWS.setConnectionTimeout(10 * 1000); | ||
mqttClientAWS.setKeepAliveInterval(30 * 1000); | ||
mqttClientAWS.setCleanSession(false); | ||
} | ||
|
||
void loop() { | ||
ArduinoCloud.update(); | ||
potentiometer = analogRead(A0); | ||
seconds = millis() / 1000; | ||
|
||
if (!ArduinoCloud.connected()) { | ||
return; | ||
} | ||
|
||
if (AWSIoTPreferredConnection.check() != NetworkConnectionState::CONNECTED) { | ||
return; | ||
} | ||
|
||
if (!mqttClientAWS.connected()) { | ||
if (millis() - connectMillis > 5000) { | ||
connectMillis = millis(); | ||
// MQTT client is disconnected, connect | ||
if (!connectMQTT()) { | ||
return; | ||
} | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
// poll for new MQTT messages and send keep alive | ||
mqttClientAWS.poll(); | ||
|
||
// publish a message roughly every 5 seconds. | ||
if (millis() - publishMillis > 5000) { | ||
publishMillis = millis(); | ||
|
||
publishMessage(); | ||
} | ||
} | ||
|
||
/* | ||
* 'onLedChange' is called when the "led" property of your Thing changes | ||
*/ | ||
void onLedChange() { | ||
Serial.print("LED set to "); | ||
Serial.println(led); | ||
digitalWrite(LED_BUILTIN, led); | ||
} | ||
|
||
void onMessageReceived(int messageSize) | ||
{ | ||
// we received a message, print out the topic and contents | ||
Serial.print("Received a message with topic '"); | ||
Serial.print(mqttClientAWS.messageTopic()); | ||
Serial.print("', length "); | ||
Serial.print(messageSize); | ||
Serial.println(" bytes:"); | ||
|
||
for (int i = 0; i < messageSize; i++) { | ||
const char c = mqttClientAWS.read(); | ||
Serial.print(c); | ||
} | ||
Serial.println(); | ||
} | ||
|
||
int connectMQTT() { | ||
Serial.print("Attempting to connect to MQTT broker: "); | ||
Serial.print(AWS_BROKER); | ||
Serial.println(" "); | ||
|
||
if (!mqttClientAWS.connect(AWS_BROKER, 8883)) { | ||
// failed, retry | ||
Serial.print("."); | ||
return 0; | ||
} | ||
Serial.println(); | ||
|
||
Serial.println("You're connected to the MQTT broker"); | ||
Serial.println(); | ||
|
||
// subscribe to a topic | ||
mqttClientAWS.subscribe("arduino/incoming"); | ||
return 1; | ||
} | ||
|
||
void publishMessage() { | ||
Serial.println("Publishing message"); | ||
|
||
// send message, the Print interface can be used to set the message contents | ||
mqttClientAWS.beginMessage("arduino/outgoing"); | ||
mqttClientAWS.print("hello "); | ||
mqttClientAWS.print(millis()); | ||
mqttClientAWS.endMessage(); | ||
} |
This file contains 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,25 @@ | ||
#include <Arduino_ConnectionHandler.h> | ||
|
||
/* A complete list of supported boards with WiFi is available here: | ||
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what | ||
*/ | ||
#if defined(BOARD_HAS_WIFI) && defined(USE_WIFI_CONNECTION) | ||
#define SECRET_WIFI_SSID "" | ||
#define SECRET_WIFI_PASS "" | ||
#endif | ||
|
||
/* Portenta H7 + Ethernet shield */ | ||
#if defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_MANUAL_CONNECTION) | ||
#define SECRET_ETH_OPTIONAL_IP "" | ||
#define SECRET_ETH_OPTIONAL_DNS "" | ||
#define SECRET_ETH_OPTIONAL_GATEWAY "" | ||
#define SECRET_ETH_OPTIONAL_NETMASK "" | ||
#endif | ||
|
||
/* Portenta CAT.M1/NB IoT GNSS Shield */ | ||
#if defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION) | ||
#define SECRET_CATM_PIN "" | ||
#define SECRET_CATM_APN "" | ||
#define SECRET_CATM_LOGIN "" | ||
#define SECRET_CATM_PASS "" | ||
#endif |
This file contains 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,10 @@ | ||
/* Fill in the hostname of your AWS IoT broker */ | ||
#define AWS_BROKER "" | ||
|
||
#define AWS_SLOT 4 | ||
|
||
/* Fill in the boards public certificate */ | ||
const char AWS_CERTIFICATE[] = R"( | ||
-----BEGIN CERTIFICATE----- | ||
-----END CERTIFICATE----- | ||
)"; |
This file contains 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,45 @@ | ||
#include <ArduinoIoTCloud.h> | ||
#include <Arduino_ConnectionHandler.h> | ||
#include "aws_config.h" | ||
|
||
#if !(defined(BOARD_STM32H7)) | ||
#error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" | ||
#endif | ||
|
||
void onLedChange(); | ||
|
||
bool led; | ||
int potentiometer; | ||
int seconds; | ||
|
||
void initProperties() { | ||
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); | ||
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); | ||
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); | ||
} | ||
|
||
//#define USE_ETHERNET_DHCP_CONNECTION | ||
//#define USE_ETHERNET_MANUAL_CONNECTION | ||
#define USE_WIFI_CONNECTION | ||
//#define USE_CATM1_NBIOT_CONNECTION | ||
|
||
#include "arduino_secrets.h" | ||
|
||
#if defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_CONNECTION) | ||
/* DHCP mode */ | ||
EthernetConnectionHandler ArduinoIoTPreferredConnection; | ||
EthernetConnectionHandler AWSIoTPreferredConnection; | ||
#elif defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_MANUAL_CONNECTION) | ||
/* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */ | ||
EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_ETH_OPTIONAL_IP, SECRET_ETH_OPTIONAL_DNS, SECRET_ETH_OPTIONAL_GATEWAY, SECRET_ETH_OPTIONAL_NETMASK); | ||
EthernetConnectionHandler AWSIoTPreferredConnection(SECRET_ETH_OPTIONAL_IP, SECRET_ETH_OPTIONAL_DNS, SECRET_ETH_OPTIONAL_GATEWAY, SECRET_ETH_OPTIONAL_NETMASK); | ||
#elif defined(BOARD_HAS_WIFI) && defined(USE_WIFI_CONNECTION) | ||
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); | ||
WiFiConnectionHandler AWSIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); | ||
#elif defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION) | ||
CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_CATM_PIN, SECRET_CATM_APN, SECRET_CATM_LOGIN, SECRET_CATM_PASS); | ||
CatM1ConnectionHandler AWSIoTPreferredConnection(SECRET_CATM_PIN, SECRET_CATM_APN, SECRET_CATM_LOGIN, SECRET_CATM_PASS); | ||
#endif | ||
|
||
BearSSLClient sslClientAWS(AWSIoTPreferredConnection.getClient()); | ||
MqttClient mqttClientAWS(sslClientAWS); |