diff --git a/examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino b/examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino new file mode 100644 index 000000000..50c55b9f1 --- /dev/null +++ b/examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino @@ -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(); +} diff --git a/examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h b/examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h new file mode 100644 index 000000000..bbf995788 --- /dev/null +++ b/examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h @@ -0,0 +1,36 @@ +#include + +/* 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_SSID "" + #define SECRET_PASS "" +#endif + +/* Portenta H7 + Ethernet shield */ +#if defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_MANUAL_CONNECTION) + #define SECRET_OPTIONAL_IP "" + #define SECRET_OPTIONAL_DNS "" + #define SECRET_OPTIONAL_GATEWAY "" + #define SECRET_OPTIONAL_NETMASK "" +#endif + +/* Portenta CAT.M1/NB IoT GNSS Shield */ +#if defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION) + #define SECRET_PIN "" + #define SECRET_APN "" + #define SECRET_LOGIN "" + #define SECRET_PASS "" +#endif + +/* 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----- +)"; diff --git a/examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h b/examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h new file mode 100644 index 000000000..9c6b49d86 --- /dev/null +++ b/examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h @@ -0,0 +1,44 @@ +#include +#include + +#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_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); + EthernetConnectionHandler AWSIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); +#elif defined(BOARD_HAS_WIFI) && defined(USE_WIFI_CONNECTION) + WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS); + WiFiConnectionHandler AWSIoTPreferredConnection(SECRET_SSID, SECRET_PASS); +#elif defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION) + CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); + CatM1ConnectionHandler AWSIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); +#endif + +BearSSLClient sslClientAWS(AWSIoTPreferredConnection.getClient()); +MqttClient mqttClientAWS(sslClientAWS);