Skip to content

Commit 520748e

Browse files
author
Alberto Iannaccone
committed
blink via cloud monitor
1 parent 5f11cb8 commit 520748e

File tree

1 file changed

+74
-68
lines changed

1 file changed

+74
-68
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,22 @@
11
#include <WiFi101.h>
22
#include <ArduinoCloudV2.h>
33

4-
#include "arduino_secrets.h"
54
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
65
char ssid[] = SECRET_SSID; // your network SSID (name)
76
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
87
int status = WL_IDLE_STATUS; // the WiFi radio's status
8+
String serialString = ""; // the string used to compose network messages from the received characters
99

1010
WiFiClient wifiClient;
1111

1212
unsigned long getTime() {
1313
return WiFi.getTime();
1414
}
1515

16-
// Thing properties
17-
int position;
18-
bool valid = true;
19-
float ratio = 2.47;
20-
String welcome = "ciao";
21-
22-
// Last time when the WiFi connection was checked
23-
unsigned long lastMillis = 0;
24-
25-
void onPositionUpdate() {
26-
Serial.print("New position value: ");
27-
Serial.println(position);
28-
}
29-
30-
void onWelcomeUpdate() {
31-
Serial.print("New state value: ");
32-
Serial.println(welcome);
33-
}
34-
35-
void onValidUpdate() {
36-
Serial.print("New valid value: ");
37-
Serial.println(valid);
38-
}
39-
40-
void onRatioUpdate() {
41-
Serial.print("New ratio value: ");
42-
Serial.println(ratio);
43-
}
44-
4516
void setup() {
4617
//Initialize serial and wait for port to open:
4718
Serial.begin(9600);
48-
while (!Serial) {
49-
; // wait for serial port to connect. Needed for native USB port only
50-
}
19+
delay(7000);
5120

5221
// check for the presence of the shield:
5322
if (WiFi.status() == WL_NO_SHIELD) {
@@ -62,14 +31,20 @@ void setup() {
6231
}
6332

6433
// attempt to connect to WiFi network:
65-
while (status != WL_CONNECTED) {
34+
int attempts = 0;
35+
while (status != WL_CONNECTED && attempts < 6) {
6636
Serial.print("Attempting to connect to WPA SSID: ");
6737
Serial.println(ssid);
6838
// Connect to WPA/WPA2 network:
6939
status = WiFi.begin(ssid, pass);
7040

7141
// wait 10 seconds for connection:
72-
delay(5000);
42+
delay(10000);
43+
attempts++;
44+
}
45+
46+
if (status != WL_CONNECTED) {
47+
Serial.println("Failed to connect to Wifi!");
7348
}
7449

7550
// you're connected now, so print out the data:
@@ -79,51 +54,82 @@ void setup() {
7954
Serial.println("Attempting to connect to Arduino Cloud ...");
8055

8156
ArduinoCloud.onGetTime(getTime);
82-
if (!ArduinoCloud.connect()) {
57+
58+
attempts = 0;
59+
while (!ArduinoCloud.connect() && attempts < 10) {
60+
attempts++;
61+
}
62+
63+
if (attempts >= 3) {
8364
Serial.println("Failed to connect to Arduino Cloud!");
8465
while (1);
8566
}
8667

8768
Serial.println("Successfully connected to Arduino Cloud :)");
88-
ArduinoCloud.addProperty(welcome, READWRITE, ON_CHANGE, onWelcomeUpdate);
89-
ArduinoCloud.addProperty(position, READWRITE, ON_CHANGE, onPositionUpdate);
90-
ArduinoCloud.addProperty(valid, READWRITE, ON_CHANGE, onValidUpdate);
91-
ArduinoCloud.addProperty(ratio, READWRITE, ON_CHANGE, onRatioUpdate);
9269

9370
CloudSerial.begin(9600);
94-
lastMillis = millis();
71+
CloudSerial.print("I'm ready for blinking!\n");
9572
}
9673

9774
void loop() {
9875
ArduinoCloud.poll();
99-
Serial.println("loop updated");
100-
/*
101-
Serial.println(".");
102-
welcome += "!";
103-
ratio += 0.4355;
104-
valid = !valid;
105-
position += 1;
106-
*/
107-
if (millis() - lastMillis > 20000) {
108-
Serial.println("..Check WiFi status..");
109-
bool error = false;
110-
// Check Wifi status
111-
while (WiFi.status() != WL_CONNECTED) {
112-
error = true;
113-
Serial.print("..Reconnection to connect to WPA SSID: ");
114-
Serial.println(ssid);
115-
status = WiFi.begin(ssid, pass);
116-
// wait 10 seconds for connection:
117-
delay(2000);
118-
}
119-
if(error) {
120-
Serial.println("..Reconnected to the Nework!");
121-
// Call the reconnect method to clean up the ArduinoCloud connection
122-
ArduinoCloud.reconnect(wifiClient);
76+
77+
// check if there is something waiting to be read
78+
if (CloudSerial.available()) {
79+
char character = CloudSerial.read();
80+
serialString += character;
81+
82+
// if a \n character has been received, there should be a complete command inside serialString
83+
if (character == '\n') {
84+
manageString();
12385
}
124-
delay(500);
125-
lastMillis = millis();
86+
}
87+
else // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check.
88+
{
89+
manageString();
12690
}
12791

128-
delay(2000);
92+
// Just to be able to simulate MKR1000's responses through the serial monitor
93+
if (Serial.available()) {
94+
CloudSerial.write(Serial.read());
95+
}
12996
}
97+
98+
void manageString() {
99+
// Don't proceed if the string is empty
100+
if (serialString.equals("")) return;
101+
102+
// Remove whitespaces
103+
serialString.trim();
104+
105+
// Make it uppercase;
106+
serialString.toUpperCase();
107+
108+
if (serialString.equals("ON")) {
109+
digitalWrite(6, HIGH);
110+
}
111+
if (serialString.equals("OFF")) {
112+
digitalWrite(6, LOW);
113+
}
114+
115+
// Send back the command you just applied. This way the Angular frontend can stay synchronized with the MKR1000 state.
116+
sendString(serialString);
117+
118+
// Reset serialString
119+
serialString = "";
120+
}
121+
122+
// sendString sends a string to the Arduino Cloud.
123+
void sendString(String stringToSend) {
124+
// send the characters one at a time
125+
char lastSentChar = 0;
126+
for (int i = 0; i < stringToSend.length(); i++) {
127+
lastSentChar = stringToSend.charAt(i);
128+
CloudSerial.write(lastSentChar);
129+
}
130+
131+
// if the last sent character wasn't a '\n' add it
132+
if (lastSentChar != '\n') {
133+
CloudSerial.write('\n');
134+
}
135+
}

0 commit comments

Comments
 (0)