|
| 1 | +#include "arduino_secrets.h" |
| 2 | +/* |
| 3 | + This sketch demonstrates how to connect to ArduinoIoTCloud and AWS IoT core. |
| 4 | +
|
| 5 | + The full list of compatible boards can be found here: |
| 6 | + - https://github.com/arduino-libraries/ArduinoIoTCloud#what |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "thingProperties.h" |
| 10 | +#include "aws_secrets.h" |
| 11 | + |
| 12 | +Client& getDefaultClient() { |
| 13 | + switch(ArduinoIoTPreferredConnection.getInterface()) { |
| 14 | + |
| 15 | +#ifdef BOARD_HAS_WIFI |
| 16 | + case NetworkAdapter::WIFI: |
| 17 | + static WiFiClient wclient; |
| 18 | + return wclient; |
| 19 | +#endif |
| 20 | + |
| 21 | +#ifdef BOARD_HAS_ETHERNET |
| 22 | + case NetworkAdapter::ETHERNET: |
| 23 | + static EthernetClient eclient; |
| 24 | + return eclient; |
| 25 | +#endif |
| 26 | + |
| 27 | + default: |
| 28 | + Serial.println("Error: could not create default AWS client"); |
| 29 | + break; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +unsigned long publishMillis = 0; |
| 34 | +unsigned long connectMillis = 0; |
| 35 | + |
| 36 | +BearSSLClient sslClientAWS(getDefaultClient()); |
| 37 | +MqttClient mqttClientAWS(sslClientAWS); |
| 38 | + |
| 39 | +void setup() { |
| 40 | + /* Initialize serial and wait up to 5 seconds for port to open */ |
| 41 | + Serial.begin(9600); |
| 42 | + |
| 43 | + /* Configure LED pin as an output */ |
| 44 | + pinMode(LED_BUILTIN, OUTPUT); |
| 45 | + |
| 46 | + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ |
| 47 | + initProperties(); |
| 48 | + |
| 49 | + /* Initialize Arduino IoT Cloud library */ |
| 50 | + ArduinoCloud.begin(ArduinoIoTPreferredConnection, true, "iot.arduino.cc"); |
| 51 | + |
| 52 | + setDebugMessageLevel(5); |
| 53 | + ArduinoCloud.printDebugInfo(); |
| 54 | + |
| 55 | + /* Initialize AWS Client */ |
| 56 | + ArduinoBearSSL.onGetTime(getTime); |
| 57 | + sslClientAWS.setEccSlot(AWS_SLOT, AWS_CERTIFICATE); |
| 58 | + |
| 59 | + mqttClientAWS.setId("ArduinoAWSClient"); |
| 60 | + mqttClientAWS.onMessage(onMessageReceived); |
| 61 | + mqttClientAWS.setConnectionTimeout(10 * 1000); |
| 62 | + mqttClientAWS.setKeepAliveInterval(30 * 1000); |
| 63 | + mqttClientAWS.setCleanSession(false); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + ArduinoCloud.update(); |
| 68 | + potentiometer = analogRead(A0); |
| 69 | + seconds = millis() / 1000; |
| 70 | + |
| 71 | + if (!ArduinoCloud.connected()) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (!mqttClientAWS.connected()) { |
| 76 | + if (millis() - connectMillis > 5000) { |
| 77 | + connectMillis = millis(); |
| 78 | + // MQTT client is disconnected, connect |
| 79 | + if (!connectMQTT()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + } else { |
| 83 | + return; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // poll for new MQTT messages and send keep alive |
| 88 | + mqttClientAWS.poll(); |
| 89 | + |
| 90 | + // publish a message roughly every 5 seconds. |
| 91 | + if (millis() - publishMillis > 5000) { |
| 92 | + publishMillis = millis(); |
| 93 | + |
| 94 | + publishMessage(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * 'onLedChange' is called when the "led" property of your Thing changes |
| 100 | + */ |
| 101 | +void onLedChange() { |
| 102 | + Serial.print("LED set to "); |
| 103 | + Serial.println(led); |
| 104 | + digitalWrite(LED_BUILTIN, led); |
| 105 | +} |
| 106 | + |
| 107 | +void onMessageReceived(int messageSize) |
| 108 | +{ |
| 109 | + // we received a message, print out the topic and contents |
| 110 | + Serial.print("Received a message with topic '"); |
| 111 | + Serial.print(mqttClientAWS.messageTopic()); |
| 112 | + Serial.print("', length "); |
| 113 | + Serial.print(messageSize); |
| 114 | + Serial.println(" bytes:"); |
| 115 | + |
| 116 | + for (int i = 0; i < messageSize; i++) { |
| 117 | + const char c = mqttClientAWS.read(); |
| 118 | + Serial.print(c); |
| 119 | + } |
| 120 | + Serial.println(); |
| 121 | +} |
| 122 | + |
| 123 | +int connectMQTT() { |
| 124 | + Serial.print("Attempting to connect to MQTT broker: "); |
| 125 | + Serial.print(AWS_BROKER); |
| 126 | + Serial.println(" "); |
| 127 | + |
| 128 | + if (!mqttClientAWS.connect(AWS_BROKER, 8883)) { |
| 129 | + // failed, retry |
| 130 | + Serial.print("."); |
| 131 | + return 0; |
| 132 | + } |
| 133 | + Serial.println(); |
| 134 | + |
| 135 | + Serial.println("You're connected to the MQTT broker"); |
| 136 | + Serial.println(); |
| 137 | + |
| 138 | + // subscribe to a topic |
| 139 | + mqttClientAWS.subscribe("arduino/incoming"); |
| 140 | + return 1; |
| 141 | +} |
| 142 | + |
| 143 | +void publishMessage() { |
| 144 | + Serial.println("Publishing message"); |
| 145 | + |
| 146 | + // send message, the Print interface can be used to set the message contents |
| 147 | + mqttClientAWS.beginMessage("arduino/outgoing"); |
| 148 | + mqttClientAWS.print("hello "); |
| 149 | + mqttClientAWS.print(millis()); |
| 150 | + mqttClientAWS.endMessage(); |
| 151 | +} |
0 commit comments