Skip to content

Commit 88e9cde

Browse files
Alberto Iannacconefacchinm
Alberto Iannaccone
authored andcommitted
Fix getting started CloudSerial sketch
1 parent cb8b549 commit 88e9cde

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

Diff for: examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino

+75-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
#include <WiFi101.h>
1+
#include <WiFi101.h> // change to WiFiNINA.h if you are using the MKR WiFi 1010 or MKR Vidor 4000
22
#include <ArduinoCloudV2.h>
3-
43
#include "arduino_secrets.h"
4+
5+
#define TIMEOUT 7000
6+
57
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
68
char ssid[] = SECRET_SSID; // your network SSID (name)
79
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
810
int status = WL_IDLE_STATUS; // the WiFi radio's status
11+
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
912

1013
WiFiClient wifiClient;
1114

@@ -16,9 +19,8 @@ unsigned long getTime() {
1619
void setup() {
1720
//Initialize serial and wait for port to open:
1821
Serial.begin(9600);
19-
while (!Serial) {
20-
; // wait for serial port to connect. Needed for native USB port only
21-
}
22+
int timeout = millis() + TIMEOUT;
23+
while (!Serial && (millis() < timeout)) {}
2224

2325
// check for the presence of the shield:
2426
if (WiFi.status() == WL_NO_SHIELD) {
@@ -33,42 +35,106 @@ void setup() {
3335
}
3436

3537
// attempt to connect to WiFi network:
36-
while (status != WL_CONNECTED) {
38+
int attempts = 0;
39+
while (status != WL_CONNECTED && attempts < 6) {
3740
Serial.print("Attempting to connect to WPA SSID: ");
3841
Serial.println(ssid);
3942
// Connect to WPA/WPA2 network:
4043
status = WiFi.begin(ssid, pass);
4144

4245
// wait 10 seconds for connection:
4346
delay(10000);
47+
attempts++;
48+
}
49+
50+
if (status != WL_CONNECTED) {
51+
Serial.println("Failed to connect to Wifi!");
52+
while (true);
4453
}
4554

4655
// you're connected now, so print out the data:
4756
Serial.print("You're connected to the network");
4857

4958
Serial.println();
50-
Serial.println("Attempting to connect to Arduino Cloud ...");
59+
Serial.println("Attempting to connect to Arduino Cloud");
5160

5261
ArduinoCloud.onGetTime(getTime);
53-
if (!ArduinoCloud.connect()) {
62+
63+
attempts = 0;
64+
while (!ArduinoCloud.connect() && attempts < 10) {
65+
Serial.print(".");
66+
attempts++;
67+
}
68+
69+
if (attempts >= 10) {
5470
Serial.println("Failed to connect to Arduino Cloud!");
5571
while (1);
5672
}
5773

5874
Serial.println("Successfully connected to Arduino Cloud :)");
5975

6076
CloudSerial.begin(9600);
77+
CloudSerial.print("I'm ready for blinking!\n");
6178
}
6279

6380
void loop() {
6481
ArduinoCloud.poll();
6582

83+
// check if there is something waiting to be read
6684
if (CloudSerial.available()) {
67-
Serial.write(CloudSerial.read());
85+
char character = CloudSerial.read();
86+
cloudSerialBuffer += character;
87+
88+
// if a \n character has been received, there should be a complete command inside cloudSerialBuffer
89+
if (character == '\n') {
90+
manageString();
91+
}
92+
}
93+
else // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check.
94+
{
95+
manageString();
6896
}
6997

98+
// Just to be able to simulate the board responses through the serial monitor
7099
if (Serial.available()) {
71100
CloudSerial.write(Serial.read());
72101
}
73102
}
74103

104+
void manageString() {
105+
// Don't proceed if the string is empty
106+
if (cloudSerialBuffer.equals("")) return;
107+
108+
// Remove whitespaces
109+
cloudSerialBuffer.trim();
110+
111+
// Make it uppercase;
112+
cloudSerialBuffer.toUpperCase();
113+
114+
if (cloudSerialBuffer.equals("ON")) {
115+
digitalWrite(6, HIGH);
116+
}
117+
if (cloudSerialBuffer.equals("OFF")) {
118+
digitalWrite(6, LOW);
119+
}
120+
121+
sendString(cloudSerialBuffer);
122+
123+
// Reset cloudSerialBuffer
124+
cloudSerialBuffer = "";
125+
}
126+
127+
// sendString sends a string to the Arduino Cloud.
128+
void sendString(String stringToSend) {
129+
// send the characters one at a time
130+
char lastSentChar = 0;
131+
for (int i = 0; i < stringToSend.length(); i++) {
132+
lastSentChar = stringToSend.charAt(i);
133+
CloudSerial.write(lastSentChar);
134+
}
135+
136+
// if the last sent character wasn't a '\n' add it
137+
if (lastSentChar != '\n') {
138+
CloudSerial.write('\n');
139+
}
140+
}

0 commit comments

Comments
 (0)