Skip to content

Commit 4b067ff

Browse files
committed
add example using WiFi Client
1 parent 2b4aa5e commit 4b067ff

File tree

3 files changed

+110
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)