1
1
#include < WiFi101.h>
2
2
#include < ArduinoCloudV2.h>
3
3
4
- #include " arduino_secrets.h"
5
4
// /////please enter your sensitive data in the Secret tab/arduino_secrets.h
6
5
char ssid[] = SECRET_SSID; // your network SSID (name)
7
6
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
8
7
int status = WL_IDLE_STATUS; // the WiFi radio's status
8
+ String serialString = " " ; // the string used to compose network messages from the received characters
9
9
10
10
WiFiClient wifiClient;
11
11
12
12
unsigned long getTime () {
13
13
return WiFi.getTime ();
14
14
}
15
15
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
-
45
16
void setup () {
46
17
// Initialize serial and wait for port to open:
47
18
Serial.begin (9600 );
48
- while (!Serial) {
49
- ; // wait for serial port to connect. Needed for native USB port only
50
- }
19
+ delay (7000 );
51
20
52
21
// check for the presence of the shield:
53
22
if (WiFi.status () == WL_NO_SHIELD) {
@@ -62,14 +31,20 @@ void setup() {
62
31
}
63
32
64
33
// attempt to connect to WiFi network:
65
- while (status != WL_CONNECTED) {
34
+ int attempts = 0 ;
35
+ while (status != WL_CONNECTED && attempts < 6 ) {
66
36
Serial.print (" Attempting to connect to WPA SSID: " );
67
37
Serial.println (ssid);
68
38
// Connect to WPA/WPA2 network:
69
39
status = WiFi.begin (ssid, pass);
70
40
71
41
// 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!" );
73
48
}
74
49
75
50
// you're connected now, so print out the data:
@@ -79,51 +54,82 @@ void setup() {
79
54
Serial.println (" Attempting to connect to Arduino Cloud ..." );
80
55
81
56
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 ) {
83
64
Serial.println (" Failed to connect to Arduino Cloud!" );
84
65
while (1 );
85
66
}
86
67
87
68
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);
92
69
93
70
CloudSerial.begin (9600 );
94
- lastMillis = millis ( );
71
+ CloudSerial. print ( " I'm ready for blinking! \n " );
95
72
}
96
73
97
74
void loop () {
98
75
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 ();
123
85
}
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 ();
126
90
}
127
91
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
+ }
129
96
}
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