Skip to content

Commit cc13683

Browse files
committed
Add "Block for OTA" example
1 parent 7d4e9e0 commit cc13683

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <Arduino_ConnectionHandler.h>
2+
3+
/* A complete list of supported boards with WiFi is available here:
4+
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what
5+
*/
6+
#if defined(BOARD_HAS_WIFI)
7+
#define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME"
8+
#define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD"
9+
#endif
10+
11+
/* ESP8266 ESP32 */
12+
#if defined(BOARD_HAS_SECRET_KEY)
13+
#define SECRET_DEVICE_KEY "my-device-password"
14+
#endif
15+
16+
/* MKR GSM 1400 */ /* MKR NB 1500 */ /* Portenta CAT.M1/NB IoT GNSS Shield */
17+
/* Portenta H7 and C33 + Portenta Mid Carrier + 4G Module */
18+
#if defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || \
19+
defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR)
20+
#define SECRET_PIN ""
21+
#define SECRET_APN ""
22+
#define SECRET_LOGIN ""
23+
#define SECRET_PASS ""
24+
#endif
25+
26+
/* MKR WAN 1300/1310 */
27+
#if defined(BOARD_HAS_LORA)
28+
#define SECRET_APP_EUI ""
29+
#define SECRET_APP_KEY ""
30+
#endif
31+
32+
/* Portenta H7 + Ethernet shield */
33+
#if defined(BOARD_HAS_ETHERNET)
34+
#define SECRET_OPTIONAL_IP ""
35+
#define SECRET_OPTIONAL_DNS ""
36+
#define SECRET_OPTIONAL_GATEWAY ""
37+
#define SECRET_OPTIONAL_NETMASK ""
38+
#endif
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
#include "arduino_secrets.h"
4+
5+
#if !(defined(HAS_TCP) || defined(HAS_LORA))
6+
#error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what"
7+
#endif
8+
9+
#if defined(BOARD_HAS_SECRET_KEY)
10+
#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
11+
#endif
12+
13+
#if defined(HAS_LORA)
14+
#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
15+
#endif
16+
17+
void onLedChange();
18+
19+
bool led;
20+
int potentiometer;
21+
int seconds;
22+
23+
void initProperties() {
24+
#if defined(BOARD_HAS_SECRET_KEY)
25+
ArduinoCloud.setBoardId(BOARD_ID);
26+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
27+
#endif
28+
#if defined(HAS_TCP)
29+
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
30+
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
31+
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
32+
#elif defined(HAS_LORA)
33+
ArduinoCloud.addProperty(led, 1, Permission::ReadWrite).onUpdate(onLedChange);
34+
ArduinoCloud.addProperty(potentiometer, 2, Permission::Read).publishOnChange(10);
35+
ArduinoCloud.addProperty(seconds, 3, Permission::Read).publishEvery(5 * MINUTES);
36+
37+
ArduinoCloud.setThingId(THING_ID);
38+
#endif
39+
}
40+
41+
#if defined(BOARD_HAS_ETHERNET)
42+
/* DHCP mode */
43+
//EthernetConnectionHandler ArduinoIoTPreferredConnection;
44+
/* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */
45+
EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK);
46+
#elif defined(BOARD_HAS_WIFI)
47+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
48+
#elif defined(BOARD_HAS_GSM)
49+
GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
50+
#elif defined(BOARD_HAS_LORA)
51+
LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, NULL, _lora_class::CLASS_A);
52+
#elif defined(BOARD_HAS_NB)
53+
NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
54+
#elif defined(BOARD_HAS_CATM1_NBIOT)
55+
CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
56+
#elif defined(BOARD_HAS_CELLULAR)
57+
CellularConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
58+
#endif

0 commit comments

Comments
 (0)