Skip to content

Commit d372651

Browse files
umbynosaentinger
authored andcommitted
Add new Cloud Blink example (#53)
* add new CloudBlink example * remove some comments * fix errors and improvements * move example sketch in proper folder * add new CloudBlink example * remove some comments * fix errors and improvements * move example sketch in proper folder * apply code formatting according to examples_formatter.conf
1 parent 0fb6c6f commit d372651

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <WiFiConnectionManager.h>
3+
4+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
5+
char ssid[] = SECRET_WIFI_NAME; // your network SSID (name)
6+
char pass[] = SECRET_PASSWORD; // your network password (use for WPA, or use as key for WEP)
7+
8+
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
9+
// handles connection to the network
10+
ConnectionManager *ArduinoIoTPreferredConnection = new WiFiConnectionManager(SECRET_WIFI_NAME, SECRET_PASSWORD);
11+
12+
void setup() {
13+
setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4]
14+
Serial.begin(9600);
15+
while (!Serial); // waits for the serial to become available
16+
ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud
17+
while (ArduinoCloud.connected()); // needed to wait for the inizialization of CloudSerial
18+
CloudSerial.print("I'm ready for blinking!\n");
19+
}
20+
21+
void loop() {
22+
ArduinoCloud.update();
23+
// check if there is something waiting to be read
24+
if (CloudSerial.available()) {
25+
char character = CloudSerial.read();
26+
cloudSerialBuffer += character;
27+
// if a \n character has been received, there should be a complete command inside cloudSerialBuffer
28+
if (character == '\n') {
29+
handleString();
30+
}
31+
} else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check.
32+
handleString();
33+
}
34+
// Just to be able to simulate the board responses through the serial monitor
35+
if (Serial.available()) {
36+
CloudSerial.write(Serial.read());
37+
}
38+
}
39+
void handleString() {
40+
// Don't proceed if the string is empty
41+
if (cloudSerialBuffer.equals("")) {
42+
return;
43+
}
44+
// Remove leading and trailing whitespaces
45+
cloudSerialBuffer.trim();
46+
// Make it uppercase;
47+
cloudSerialBuffer.toUpperCase();
48+
if (cloudSerialBuffer.equals("ON")) {
49+
digitalWrite(LED_BUILTIN, HIGH);
50+
} else if (cloudSerialBuffer.equals("OFF")) {
51+
digitalWrite(LED_BUILTIN, LOW);
52+
}
53+
sendString(cloudSerialBuffer);
54+
// Reset cloudSerialBuffer
55+
cloudSerialBuffer = "";
56+
}
57+
// sendString sends a string to the Arduino Cloud.
58+
void sendString(String stringToSend) {
59+
// send the characters one at a time
60+
char lastSentChar = 0;
61+
for (int i = 0; i < stringToSend.length(); i++) {
62+
lastSentChar = stringToSend.charAt(i);
63+
CloudSerial.write(lastSentChar);
64+
}
65+
// if the last sent character wasn't a '\n' add it
66+
if (lastSentChar != '\n') {
67+
CloudSerial.write('\n');
68+
}
69+
}

0 commit comments

Comments
 (0)