|
| 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 Notecard, 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 <Notecard.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 | +/* |
| 25 | + * Choose an interrupt capable pin to reduce polling and improve |
| 26 | + * the overall responsiveness of the ArduinoIoTCloud library |
| 27 | + */ |
| 28 | +// #define ATTN_PIN 9 |
| 29 | + |
| 30 | +void setup() { |
| 31 | + /* Initialize serial and wait up to 5 seconds for port to open */ |
| 32 | + Serial.begin(9600); |
| 33 | + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } |
| 34 | + |
| 35 | + /* Specify the level of detail for debug messages */ |
| 36 | + setDebugMessageLevel(DBG_INFO); |
| 37 | + |
| 38 | + /* Configure LED pin as an output */ |
| 39 | + pinMode(LED_BUILTIN, OUTPUT); |
| 40 | + |
| 41 | + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ |
| 42 | + initProperties(); |
| 43 | + |
| 44 | + /* Initialize Arduino IoT Cloud library */ |
| 45 | +#ifndef ATTN_PIN |
| 46 | + ArduinoCloud.begin(ArduinoIoTPreferredConnection); |
| 47 | + ArduinoCloud.setNotecardPollInterval(3000); // default: 1000ms, min: 250ms |
| 48 | +#else |
| 49 | + ArduinoCloud.begin(ArduinoIoTPreferredConnection, ATTN_PIN); |
| 50 | +#endif |
| 51 | + |
| 52 | + ArduinoCloud.printDebugInfo(); |
| 53 | +} |
| 54 | + |
| 55 | +void loop() { |
| 56 | + ArduinoCloud.update(); |
| 57 | + potentiometer = analogRead(A0); |
| 58 | + seconds = millis() / 1000; |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * 'onLedChange' is called when the "led" property of your Thing changes |
| 63 | + */ |
| 64 | +void onLedChange() { |
| 65 | + Serial.print("LED set to "); |
| 66 | + Serial.println(led); |
| 67 | + digitalWrite(LED_BUILTIN, led); |
| 68 | +} |
0 commit comments