|
| 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 and Lora enabled boards supported by Arduino IoT Cloud. |
| 10 | + On a LoRa board, if it is configuered 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 | + This sketch is compatible with: |
| 14 | + - MKR 1000 |
| 15 | + - MKR WIFI 1010 |
| 16 | + - MKR GSM 1400 |
| 17 | + - MKR NB 1500 |
| 18 | + - MKR WAN 1300/1310 |
| 19 | + - Nano 33 IoT |
| 20 | + - ESP 8266 |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "arduino_secrets.h" |
| 24 | +#include "thingProperties.h" |
| 25 | + |
| 26 | +#if defined(ESP32) |
| 27 | +static int const LED_BUILTIN = 2; |
| 28 | +#endif |
| 29 | + |
| 30 | +/* |
| 31 | +// Can use it as soon as https://github.com/arduino-libraries/Arduino_ConnectionHandler/pull/63 gets merged |
| 32 | +
|
| 33 | +ConnectionHandler& get_default_connection_handler() { |
| 34 | + char ssid[MAX_LEN]; |
| 35 | + char password[MAX_LEN]; |
| 36 | + retrieveCredentialsFromFile("/fs/credential.txt", ssid, password); |
| 37 | + static WiFiConnectionHandlerDynamic ArduinoIoTPreferredConnection(ssid, password); |
| 38 | + return ArduinoIoTPreferredConnection; |
| 39 | +} |
| 40 | +*/ |
| 41 | + |
| 42 | +bool always_deny() { |
| 43 | + return false; |
| 44 | +} |
| 45 | + |
| 46 | +bool always_allow() { |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +static bool ask_user_via_serial_first_run = true; |
| 51 | +bool ask_user_via_serial() { |
| 52 | + if (ask_user_via_serial_first_run) { |
| 53 | + Serial.println("Apply OTA? y / [n]"); |
| 54 | + ask_user_via_serial_first_run = false; |
| 55 | + } |
| 56 | + if (Serial.available()) { |
| 57 | + char c = Serial.read(); |
| 58 | + if (c == 'y' || c == 'Y') { |
| 59 | + return true; |
| 60 | + } |
| 61 | + } |
| 62 | + return false; |
| 63 | +} |
| 64 | + |
| 65 | +void setup() { |
| 66 | + /* Initialize serial and wait up to 5 seconds for port to open */ |
| 67 | + Serial.begin(9600); |
| 68 | + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000); ) { } |
| 69 | + |
| 70 | + /* Configure LED pin as an output */ |
| 71 | + pinMode(LED_BUILTIN, OUTPUT); |
| 72 | + |
| 73 | + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ |
| 74 | + initProperties(); |
| 75 | + |
| 76 | + /* Initialize Arduino IoT Cloud library */ |
| 77 | + ArduinoCloud.begin(get_default_connection_handler()); |
| 78 | + |
| 79 | + ArduinoCloud.onOTARequestCb(always_deny); |
| 80 | + |
| 81 | + setDebugMessageLevel(DBG_INFO); |
| 82 | + ArduinoCloud.printDebugInfo(); |
| 83 | +} |
| 84 | + |
| 85 | +void loop() { |
| 86 | + ArduinoCloud.update(); |
| 87 | + potentiometer = analogRead(A0); |
| 88 | + seconds = millis() / 1000; |
| 89 | +} |
| 90 | + |
| 91 | +/* |
| 92 | + * 'onLedChange' is called when the "led" property of your Thing changes |
| 93 | + */ |
| 94 | +void onLedChange() { |
| 95 | + Serial.print("LED set to "); |
| 96 | + Serial.println(led); |
| 97 | + digitalWrite(LED_BUILTIN, led); |
| 98 | +} |
0 commit comments