Skip to content

Commit 03c5ad7

Browse files
committed
add example using WiFi Client
1 parent 6135a88 commit 03c5ad7

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
This sketch demonstrates how to use Arduino IoT Cloud without ArduinoConnectionHandler
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 only with WiFi.
10+
11+
The full list of compatible boards can be found here:
12+
- https://github.com/arduino-libraries/ArduinoIoTCloud#what
13+
*/
14+
15+
#include "thingProperties.h"
16+
17+
#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32)
18+
static int const LED_BUILTIN = 2;
19+
#endif
20+
21+
WiFiClient brokerClient;
22+
WiFiClient otaClient;
23+
WiFiUDP ntpClient;
24+
25+
void setup() {
26+
/* Initialize serial and wait up to 5 seconds for port to open */
27+
Serial.begin(9600);
28+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
29+
30+
int status = WL_IDLE_STATUS;
31+
while (status != WL_CONNECTED) {
32+
Serial.print("Attempting to connect to SSID: ");
33+
Serial.println(SECRET_WIFI_SSID);
34+
status = WiFi.begin(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
35+
delay(1000);
36+
}
37+
38+
/* Set the debug message level:
39+
* - DBG_ERROR: Only show error messages
40+
* - DBG_WARNING: Show warning and error messages
41+
* - DBG_INFO: Show info, warning, and error messages
42+
* - DBG_DEBUG: Show debug, info, warning, and error messages
43+
* - DBG_VERBOSE: Show all messages
44+
*/
45+
setDebugMessageLevel(DBG_INFO);
46+
47+
/* Configure LED pin as an output */
48+
pinMode(LED_BUILTIN, OUTPUT);
49+
50+
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
51+
initProperties();
52+
53+
/* Initialize Arduino IoT Cloud library */
54+
ArduinoCloud.begin(brokerClient, otaClient, ntpClient);
55+
56+
ArduinoCloud.printDebugInfo();
57+
}
58+
59+
void loop() {
60+
ArduinoCloud.update();
61+
potentiometer = analogRead(A0);
62+
seconds = millis() / 1000;
63+
}
64+
65+
/*
66+
* 'onLedChange' is called when the "led" property of your Thing changes
67+
*/
68+
void onLedChange() {
69+
Serial.print("LED set to ");
70+
Serial.println(led);
71+
digitalWrite(LED_BUILTIN, led);
72+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* A complete list of supported boards with WiFi is available here:
2+
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what
3+
*/
4+
#if defined(BOARD_HAS_WIFI)
5+
#define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME"
6+
#define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD"
7+
#endif
8+
9+
/* ESP8266 ESP32 */
10+
#if !defined(BOARD_HAS_SECURE_ELEMENT)
11+
#define SECRET_DEVICE_KEY "my-device-password"
12+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include "arduino_secrets.h"
3+
4+
#if !(defined(HAS_TCP) || defined(HAS_LORA))
5+
#error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what"
6+
#endif
7+
8+
#if !defined(BOARD_HAS_SECURE_ELEMENT)
9+
#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
10+
#endif
11+
12+
#if defined(HAS_LORA)
13+
#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14+
#endif
15+
16+
void onLedChange();
17+
18+
bool led;
19+
int potentiometer;
20+
int seconds;
21+
22+
void initProperties() {
23+
#if defined(HAS_TCP)
24+
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
25+
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
26+
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
27+
28+
#if !defined(BOARD_HAS_SECURE_ELEMENT)
29+
ArduinoCloud.setBoardId(BOARD_ID);
30+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
31+
#endif
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+

0 commit comments

Comments
 (0)