Skip to content

Commit 8edc017

Browse files
committed
Add example to connecto to ArduinoIoTCloud and AWS
1 parent c7fc7a7 commit 8edc017

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud.
3+
4+
* Connect a potentiometer (or other analog sensor) to A0.
5+
* When the potentiometer (or sensor) value changes the data is sent to the Cloud.
6+
* When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF.
7+
8+
IMPORTANT:
9+
This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud.
10+
On a LoRa board, if it is configured as a class A device (default and preferred option),
11+
values from Cloud dashboard are received only after a value is sent to Cloud.
12+
13+
The full list of compatible boards can be found here:
14+
- https://github.com/arduino-libraries/ArduinoIoTCloud#what
15+
*/
16+
17+
#include "thingProperties.h"
18+
19+
unsigned long publishMillis = 0;
20+
unsigned long connectMillis = 0;
21+
22+
void setup() {
23+
/* Initialize serial and wait up to 5 seconds for port to open */
24+
Serial.begin(9600);
25+
while(!Serial);
26+
27+
/* Configure LED pin as an output */
28+
pinMode(LED_BUILTIN, OUTPUT);
29+
30+
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
31+
initProperties();
32+
33+
/* Initialize Arduino IoT Cloud library */
34+
ArduinoCloud.begin(ArduinoIoTPreferredConnection, true, "iot.arduino.cc");
35+
36+
setDebugMessageLevel(5);
37+
ArduinoCloud.printDebugInfo();
38+
39+
/* Initialize AWS Client */
40+
ArduinoBearSSL.onGetTime(getTime);
41+
sslClientAWS.setEccSlot(AWS_SLOT, AWS_CERTIFICATE);
42+
43+
mqttClientAWS.setId("ArduinoAWSClient");
44+
mqttClientAWS.onMessage(onMessageReceived);
45+
mqttClientAWS.setConnectionTimeout(10 * 1000);
46+
mqttClientAWS.setKeepAliveInterval(30 * 1000);
47+
mqttClientAWS.setCleanSession(false);
48+
}
49+
50+
void loop() {
51+
ArduinoCloud.update();
52+
potentiometer = analogRead(A0);
53+
seconds = millis() / 1000;
54+
55+
if (!ArduinoCloud.connected()) {
56+
return;
57+
}
58+
59+
if (AWSIoTPreferredConnection.check() != NetworkConnectionState::CONNECTED) {
60+
return;
61+
}
62+
63+
if (!mqttClientAWS.connected()) {
64+
if (millis() - connectMillis > 5000) {
65+
connectMillis = millis();
66+
// MQTT client is disconnected, connect
67+
if (!connectMQTT()) {
68+
return;
69+
}
70+
} else {
71+
return;
72+
}
73+
}
74+
75+
// poll for new MQTT messages and send keep alive
76+
mqttClientAWS.poll();
77+
78+
// publish a message roughly every 5 seconds.
79+
if (millis() - publishMillis > 5000) {
80+
publishMillis = millis();
81+
82+
publishMessage();
83+
}
84+
}
85+
86+
/*
87+
* 'onLedChange' is called when the "led" property of your Thing changes
88+
*/
89+
void onLedChange() {
90+
Serial.print("LED set to ");
91+
Serial.println(led);
92+
digitalWrite(LED_BUILTIN, led);
93+
}
94+
95+
void onMessageReceived(int messageSize)
96+
{
97+
// we received a message, print out the topic and contents
98+
Serial.print("Received a message with topic '");
99+
Serial.print(mqttClientAWS.messageTopic());
100+
Serial.print("', length ");
101+
Serial.print(messageSize);
102+
Serial.println(" bytes:");
103+
104+
for (int i = 0; i < messageSize; i++) {
105+
const char c = mqttClientAWS.read();
106+
Serial.print(c);
107+
}
108+
Serial.println();
109+
}
110+
111+
int connectMQTT() {
112+
Serial.print("Attempting to connect to MQTT broker: ");
113+
Serial.print(AWS_BROKER);
114+
Serial.println(" ");
115+
116+
if (!mqttClientAWS.connect(AWS_BROKER, 8883)) {
117+
// failed, retry
118+
Serial.print(".");
119+
return 0;
120+
}
121+
Serial.println();
122+
123+
Serial.println("You're connected to the MQTT broker");
124+
Serial.println();
125+
126+
// subscribe to a topic
127+
mqttClientAWS.subscribe("arduino/incoming");
128+
return 1;
129+
}
130+
131+
void publishMessage() {
132+
Serial.println("Publishing message");
133+
134+
// send message, the Print interface can be used to set the message contents
135+
mqttClientAWS.beginMessage("arduino/outgoing");
136+
mqttClientAWS.print("hello ");
137+
mqttClientAWS.print(millis());
138+
mqttClientAWS.endMessage();
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <Arduino_ConnectionHandler.h>
2+
3+
/* A complete list of supported boards with WiFi is available here:
4+
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what
5+
*/
6+
#if defined(BOARD_HAS_WIFI) && defined(USE_WIFI_CONNECTION)
7+
#define SECRET_SSID ""
8+
#define SECRET_PASS ""
9+
#endif
10+
11+
/* Portenta H7 + Ethernet shield */
12+
#if defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_MANUAL_CONNECTION)
13+
#define SECRET_OPTIONAL_IP ""
14+
#define SECRET_OPTIONAL_DNS ""
15+
#define SECRET_OPTIONAL_GATEWAY ""
16+
#define SECRET_OPTIONAL_NETMASK ""
17+
#endif
18+
19+
/* Portenta CAT.M1/NB IoT GNSS Shield */
20+
#if defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION)
21+
#define SECRET_PIN ""
22+
#define SECRET_APN ""
23+
#define SECRET_LOGIN ""
24+
#define SECRET_PASS ""
25+
#endif
26+
27+
/* Fill in the hostname of your AWS IoT broker */
28+
#define AWS_BROKER ""
29+
30+
#define AWS_SLOT 4
31+
32+
/* Fill in the boards public certificate */
33+
const char AWS_CERTIFICATE[] = R"(
34+
-----BEGIN CERTIFICATE-----
35+
-----END CERTIFICATE-----
36+
)";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
4+
#if !(defined(BOARD_STM32H7))
5+
#error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what"
6+
#endif
7+
8+
void onLedChange();
9+
10+
bool led;
11+
int potentiometer;
12+
int seconds;
13+
14+
void initProperties() {
15+
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
16+
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
17+
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
18+
}
19+
20+
//#define USE_ETHERNET_DHCP_CONNECTION
21+
//#define USE_ETHERNET_MANUAL_CONNECTION
22+
#define USE_WIFI_CONNECTION
23+
//#define USE_CATM1_NBIOT_CONNECTION
24+
25+
#include "arduino_secrets.h"
26+
27+
#if defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_CONNECTION)
28+
/* DHCP mode */
29+
EthernetConnectionHandler ArduinoIoTPreferredConnection;
30+
EthernetConnectionHandler AWSIoTPreferredConnection;
31+
#elif defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_MANUAL_CONNECTION)
32+
/* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */
33+
EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK);
34+
EthernetConnectionHandler AWSIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK);
35+
#elif defined(BOARD_HAS_WIFI) && defined(USE_WIFI_CONNECTION)
36+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
37+
WiFiConnectionHandler AWSIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
38+
#elif defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION)
39+
CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
40+
CatM1ConnectionHandler AWSIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
41+
#endif
42+
43+
BearSSLClient sslClientAWS(AWSIoTPreferredConnection.getClient());
44+
MqttClient mqttClientAWS(sslClientAWS);

0 commit comments

Comments
 (0)