Skip to content

Commit 42d6584

Browse files
Alberto Iannacconeaentinger
Alberto Iannaccone
authored andcommitted
add new cloud blink sketch version working with security credentials
1 parent 96413da commit 42d6584

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

examples/ArduinoIoTCloud_ESP8266/thingProperties.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#include <Arduino_ConnectionHandler.h>
33

44
#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" /* "Thing ID" when selecting thing within Arduino Create */
5-
#define DEVICE_LOGIN_NAME "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
5+
#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
66

77
void onLedChange();
88

99
bool led;
1010

1111
void initProperties() {
1212
ArduinoCloud.setThingId(THING_ID);
13-
ArduinoCloud.setDeviceLoginName(DEVICE_LOGIN_NAME);
13+
ArduinoCloud.setBoardId(BOARD_ID);
1414
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
1515
ArduinoCloud.addProperty(led, READWRITE, ON_CHANGE, onLedChange);
1616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* This sketch is used during the getting started tutorial when
2+
initialising a Arduino cloud-enabled board with the Arduino
3+
cloud for the first time.
4+
5+
This sketch is compatible with:
6+
- ESP8266 boards
7+
*/
8+
9+
#include <ArduinoIoTCloud.h>
10+
#include <Arduino_ConnectionHandler.h>
11+
12+
#include "arduino_secrets.h"
13+
14+
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
15+
// handles connection to the network
16+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_NAME, SECRET_PASSWORD);
17+
18+
void setup() {
19+
setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4]
20+
Serial.begin(9600);
21+
while (!Serial); // waits for the serial to become available
22+
ArduinoCloud.setBoardId(SECRET_BOARD_ID);
23+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
24+
ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud
25+
pinMode(LED_BUILTIN, OUTPUT);
26+
}
27+
28+
void loop() {
29+
ArduinoCloud.update();
30+
// check if there is something waiting to be read
31+
if (ArduinoCloud.connected()) {
32+
if (CloudSerial.available()) {
33+
char character = CloudSerial.read();
34+
cloudSerialBuffer += character;
35+
// if a \n character has been received, there should be a complete command inside cloudSerialBuffer
36+
if (character == '\n') {
37+
handleString();
38+
}
39+
} else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check.
40+
handleString();
41+
}
42+
// Just to be able to simulate the board responses through the serial monitor
43+
if (Serial.available()) {
44+
CloudSerial.write(Serial.read());
45+
}
46+
}
47+
}
48+
void handleString() {
49+
// Don't proceed if the string is empty
50+
if (cloudSerialBuffer.equals("")) {
51+
return;
52+
}
53+
// Remove leading and trailing whitespaces
54+
cloudSerialBuffer.trim();
55+
// Make it uppercase;
56+
cloudSerialBuffer.toUpperCase();
57+
if (cloudSerialBuffer.equals("ON")) {
58+
digitalWrite(LED_BUILTIN, HIGH);
59+
} else if (cloudSerialBuffer.equals("OFF")) {
60+
digitalWrite(LED_BUILTIN, LOW);
61+
}
62+
sendString(cloudSerialBuffer);
63+
// Reset cloudSerialBuffer
64+
cloudSerialBuffer = "";
65+
}
66+
// sendString sends a string to the Arduino Cloud.
67+
void sendString(String stringToSend) {
68+
// send the characters one at a time
69+
char lastSentChar = 0;
70+
for (unsigned int i = 0; i < stringToSend.length(); i++) {
71+
lastSentChar = stringToSend.charAt(i);
72+
CloudSerial.write(lastSentChar);
73+
}
74+
// if the last sent character wasn't a '\n' add it
75+
if (lastSentChar != '\n') {
76+
CloudSerial.write('\n');
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef SECRET_WIFI_NAME
2+
#define SECRET_WIFI_NAME ""
3+
#warning "You need to define SECRET_WIFI_NAME in tab/arduino_secrets.h"
4+
#endif
5+
6+
#ifndef SECRET_PASSWORD
7+
#define SECRET_PASSWORD ""
8+
#warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h"
9+
#endif
10+
11+
#ifndef SECRET_BOARD_ID
12+
#define SECRET_BOARD_ID ""
13+
#warning "You need to define SECRET_BOARD_ID in tab/arduino_secrets.h"
14+
#endif
15+
16+
#ifndef SECRET_DEVICE_KEY
17+
#define SECRET_DEVICE_KEY ""
18+
#warning "You need to define SECRET_DEVICE_KEY in tab/arduino_secrets.h"
19+
#endif

src/ArduinoIoTCloud.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ArduinoIoTCloudClass {
113113
_thing_id = thing_id;
114114
};
115115
#ifdef BOARD_ESP
116-
inline void setDeviceLoginName(String const device_id) {
116+
inline void setBoardId(String const device_id) {
117117
_device_id = device_id;
118118
}
119119
inline void setSecretDeviceKey(String const password) {

0 commit comments

Comments
 (0)