|
| 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), values from Cloud dashboard are received |
| 11 | + 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 "arduino_secrets.h" |
| 18 | +#include "thingProperties.h" |
| 19 | + |
| 20 | +#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) |
| 21 | +static int const LED_BUILTIN = 2; |
| 22 | +#endif |
| 23 | + |
| 24 | +void setup() { |
| 25 | + /* Initialize serial and wait up to 5 seconds for port to open */ |
| 26 | + Serial.begin(9600); |
| 27 | + while(!Serial) {} |
| 28 | + |
| 29 | + /* Configure LED pin as an output */ |
| 30 | + pinMode(LED_BUILTIN, OUTPUT); |
| 31 | + |
| 32 | + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ |
| 33 | + initProperties(); |
| 34 | + |
| 35 | + Serial.print("Attempting to connect to SSID: "); |
| 36 | + Serial.println(SECRET_SSID); |
| 37 | + unsigned long start = millis(); |
| 38 | + WiFi.begin(SECRET_SSID, SECRET_PASS); |
| 39 | + while ((WiFi.status() != WL_CONNECTED) && ((millis() - start) < 3000)) { |
| 40 | + delay(100); |
| 41 | + } |
| 42 | + Serial.println("WiFi connected"); |
| 43 | + |
| 44 | + /* Initialize Arduino IoT Cloud library */ |
| 45 | + ArduinoCloud.begin(client, udp); |
| 46 | + |
| 47 | + setDebugMessageLevel(DBG_VERBOSE); |
| 48 | + ArduinoCloud.printDebugInfo(); |
| 49 | +} |
| 50 | + |
| 51 | +void loop() { |
| 52 | + ArduinoCloud.update(); |
| 53 | + potentiometer = analogRead(A0); |
| 54 | + seconds = millis() / 1000; |
| 55 | + delay(100); |
| 56 | +} |
| 57 | + |
| 58 | +/* |
| 59 | + * 'onLedChange' is called when the "led" property of your Thing changes |
| 60 | + */ |
| 61 | +void onLedChange() { |
| 62 | + Serial.print("LED set to "); |
| 63 | + Serial.println(led); |
| 64 | + digitalWrite(LED_BUILTIN, led); |
| 65 | +} |
0 commit comments