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