|
| 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 | +} |
0 commit comments