Skip to content

Commit 1158d66

Browse files
committed
Add example
1 parent 1ff3170 commit 1158d66

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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, 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), values from Cloud dashboard are received
11+
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 "arduino_secrets.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+
void setup() {
25+
/* Initialize serial and wait up to 5 seconds for port to open */
26+
Serial.begin(9600);
27+
while(!Serial) {}
28+
29+
/* Configure LED pin as an output */
30+
pinMode(LED_BUILTIN, OUTPUT);
31+
32+
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
33+
initProperties();
34+
35+
Serial.print("Attempting to connect to SSID: ");
36+
Serial.println(SECRET_SSID);
37+
unsigned long start = millis();
38+
WiFi.begin(SECRET_SSID, SECRET_PASS);
39+
while ((WiFi.status() != WL_CONNECTED) && ((millis() - start) < 3000)) {
40+
delay(100);
41+
}
42+
Serial.println("WiFi connected");
43+
44+
/* Initialize Arduino IoT Cloud library */
45+
ArduinoCloud.begin(client, udp);
46+
47+
setDebugMessageLevel(DBG_VERBOSE);
48+
ArduinoCloud.printDebugInfo();
49+
}
50+
51+
void loop() {
52+
ArduinoCloud.update();
53+
potentiometer = analogRead(A0);
54+
seconds = millis() / 1000;
55+
delay(100);
56+
}
57+
58+
/*
59+
* 'onLedChange' is called when the "led" property of your Thing changes
60+
*/
61+
void onLedChange() {
62+
Serial.print("LED set to ");
63+
Serial.println(led);
64+
digitalWrite(LED_BUILTIN, led);
65+
}

Diff for: examples/ArduinoIoTCloud-Client/arduino_secrets.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
4+
/* A complete list of supported boards with WiFi is available here:
5+
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what
6+
*/
7+
#if defined(BOARD_HAS_WIFI)
8+
#define SECRET_SSID ""
9+
#define SECRET_PASS ""
10+
#endif
11+
12+
/* ESP8266 ESP32*/
13+
#if defined(BOARD_HAS_SECRET_KEY)
14+
#define SECRET_DEVICE_KEY ""
15+
#endif
16+
17+
/* MKR GSM 1400 */
18+
#if defined(BOARD_HAS_GSM)
19+
#define SECRET_PIN ""
20+
#define SECRET_APN ""
21+
#define SECRET_LOGIN ""
22+
#define SECRET_PASS ""
23+
#endif
24+
25+
/* MKR WAN 1300/1310 */
26+
#if defined(BOARD_HAS_LORA)
27+
#define SECRET_APP_EUI ""
28+
#define SECRET_APP_KEY ""
29+
#endif
30+
31+
/* MKR NB 1500 */
32+
#if defined(BOARD_HAS_NB)
33+
#define SECRET_PIN ""
34+
#define SECRET_APN ""
35+
#define SECRET_LOGIN ""
36+
#define SECRET_PASS ""
37+
#endif
38+
39+
/* Portenta H7 + Ethernet shield */
40+
#if defined(BOARD_HAS_ETHERNET)
41+
#define SECRET_OPTIONAL_IP ""
42+
#define SECRET_OPTIONAL_DNS ""
43+
#define SECRET_OPTIONAL_GATEWAY ""
44+
#define SECRET_OPTIONAL_NETMASK ""
45+
#endif

Diff for: examples/ArduinoIoTCloud-Client/thingProperties.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#if defined(BOARD_HAS_WIFI)
2+
WiFiClient client;
3+
WiFiUDP udp;
4+
#elif defined(BOARD_HAS_GSM)
5+
#elif defined(BOARD_HAS_LORA)
6+
#elif defined(BOARD_HAS_NB)
7+
#elif defined(BOARD_HAS_ETHERNET)
8+
#else
9+
#error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what"
10+
#endif
11+
12+
#if defined(BOARD_HAS_SECRET_KEY)
13+
#define BOARD_ID ""
14+
#endif
15+
16+
void onLedChange();
17+
18+
bool led;
19+
int potentiometer;
20+
int seconds;
21+
22+
void initProperties() {
23+
#if defined(BOARD_HAS_SECRET_KEY)
24+
ArduinoCloud.setBoardId(BOARD_ID);
25+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
26+
#endif
27+
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET)
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+
}

0 commit comments

Comments
 (0)