|
| 1 | +/* |
| 2 | + This sketch demonstrates how to optimize OTA in case of complex loop(). |
| 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 | +#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) |
| 20 | +static int const LED_BUILTIN = 2; |
| 21 | +#endif |
| 22 | + |
| 23 | +bool block_for_ota { false }; |
| 24 | +bool ota_started { false }; |
| 25 | + |
| 26 | +bool onOTARequestCallback() { |
| 27 | + block_for_ota = true; |
| 28 | + ota_started = true; |
| 29 | + return true; |
| 30 | +} |
| 31 | + |
| 32 | +constexpr unsigned long printInterval { 1000 }; |
| 33 | +unsigned long printNow { printInterval }; |
| 34 | + |
| 35 | +void setup() { |
| 36 | + /* Initialize serial and wait up to 5 seconds for port to open */ |
| 37 | + Serial.begin(9600); |
| 38 | + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } |
| 39 | + |
| 40 | + /* Set the debug message level: |
| 41 | + * - DBG_ERROR: Only show error messages |
| 42 | + * - DBG_WARNING: Show warning and error messages |
| 43 | + * - DBG_INFO: Show info, warning, and error messages |
| 44 | + * - DBG_DEBUG: Show debug, info, warning, and error messages |
| 45 | + * - DBG_VERBOSE: Show all messages |
| 46 | + */ |
| 47 | + setDebugMessageLevel(DBG_VERBOSE); |
| 48 | + |
| 49 | + /* Configure LED pin as an output */ |
| 50 | + pinMode(LED_BUILTIN, OUTPUT); |
| 51 | + |
| 52 | + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ |
| 53 | + initProperties(); |
| 54 | + |
| 55 | + /* Initialize Arduino IoT Cloud library */ |
| 56 | + ArduinoCloud.begin(ArduinoIoTPreferredConnection); |
| 57 | + |
| 58 | + /* Setup OTA callback */ |
| 59 | + ArduinoCloud.onOTARequestCb(onOTARequestCallback); |
| 60 | + |
| 61 | + ArduinoCloud.printDebugInfo(); |
| 62 | +} |
| 63 | + |
| 64 | +void loop() { |
| 65 | + // When OTA is available, stay there until it completes. |
| 66 | + // The rest of the loop() does not run and the sketch |
| 67 | + // restarts automatically at the end of the OTA process. |
| 68 | + while (block_for_ota) { |
| 69 | + ArduinoCloud.update(); |
| 70 | + if (ota_started) { |
| 71 | + Serial.print("Waiting for OTA to finish..."); |
| 72 | + ota_started = false; |
| 73 | + } |
| 74 | + if (millis() > printNow) { |
| 75 | + Serial.print("."); |
| 76 | + printNow = millis() + printInterval; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + ArduinoCloud.update(); |
| 81 | + potentiometer = analogRead(A0); |
| 82 | + seconds = millis() / 1000; |
| 83 | + |
| 84 | + if (millis() > printNow) { |
| 85 | + Serial.println(millis()); |
| 86 | + printNow = millis() + printInterval; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + * 'onLedChange' is called when the "led" property of your Thing changes |
| 92 | + */ |
| 93 | +void onLedChange() { |
| 94 | + Serial.print("LED set to "); |
| 95 | + Serial.println(led); |
| 96 | + digitalWrite(LED_BUILTIN, led); |
| 97 | +} |
0 commit comments