Skip to content

Commit b35be00

Browse files
committed
Add example with OTA delay and dynamic transport override
1 parent 4974635 commit b35be00

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <Arduino_ConnectionHandler.h>
2+
3+
/* MKR1000, MKR WiFi 1010 */
4+
#if defined(BOARD_HAS_WIFI)
5+
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
6+
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
7+
#endif
8+
9+
/* ESP8266 */
10+
#if defined(BOARD_ESP8266)
11+
#define SECRET_DEVICE_KEY "my-device-password"
12+
#endif
13+
14+
/* MKR GSM 1400 */
15+
#if defined(BOARD_HAS_GSM)
16+
#define SECRET_PIN ""
17+
#define SECRET_APN ""
18+
#define SECRET_LOGIN ""
19+
#define SECRET_PASS ""
20+
#endif
21+
22+
/* MKR WAN 1300/1310 */
23+
#if defined(BOARD_HAS_LORA)
24+
#define SECRET_APP_EUI ""
25+
#define SECRET_APP_KEY ""
26+
#endif
27+
28+
/* MKR NB 1500 */
29+
#if defined(BOARD_HAS_NB)
30+
#define SECRET_PIN ""
31+
#define SECRET_APN ""
32+
#define SECRET_LOGIN ""
33+
#define SECRET_PASS ""
34+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
#include "arduino_secrets.h"
4+
5+
ConnectionHandler& __attribute__((weak)) get_default_connection_handler() {
6+
#if defined(BOARD_HAS_WIFI)
7+
static WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
8+
#elif defined(BOARD_HAS_GSM)
9+
static GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
10+
#elif defined(BOARD_HAS_LORA)
11+
static LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, _lora_class::CLASS_A);
12+
#elif defined(BOARD_HAS_NB)
13+
static NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
14+
#endif
15+
return ArduinoIoTPreferredConnection;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
4+
#if defined(BOARD_HAS_WIFI)
5+
#elif defined(BOARD_HAS_GSM)
6+
#elif defined(BOARD_HAS_LORA)
7+
#elif defined(BOARD_HAS_NB)
8+
#else
9+
#error "Arduino IoT Cloud currently only supports MKR1000, MKR WiFi 1010, MKR WAN 1300/1310, MKR NB 1500 and MKR GSM 1400"
10+
#endif
11+
12+
#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
13+
#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14+
15+
void onLedChange();
16+
17+
bool led;
18+
int potentiometer;
19+
int seconds;
20+
21+
void initProperties() {
22+
#if defined(BOARD_ESP8266)
23+
ArduinoCloud.setBoardId(BOARD_ID);
24+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
25+
#endif
26+
ArduinoCloud.setThingId(THING_ID);
27+
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
28+
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
29+
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
30+
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
31+
#elif defined(BOARD_HAS_LORA)
32+
ArduinoCloud.addProperty(led, 1, READWRITE, ON_CHANGE, onLedChange);
33+
ArduinoCloud.addProperty(potentiometer, 2, READ, ON_CHANGE);
34+
ArduinoCloud.addProperty(seconds, 3, READ, 5 * MINUTES);
35+
#endif
36+
}
37+
38+
ConnectionHandler& __attribute__((weak)) get_default_connection_handler();

0 commit comments

Comments
 (0)