1
- #include < WiFi101.h>
1
+ #include < WiFi101.h> // change to WiFiNINA.h if you are using the MKR WiFi 1010 or MKR Vidor 4000
2
2
#include < ArduinoCloudV2.h>
3
-
4
3
#include " arduino_secrets.h"
4
+
5
+ #define TIMEOUT 7000
6
+
5
7
// /////please enter your sensitive data in the Secret tab/arduino_secrets.h
6
8
char ssid[] = SECRET_SSID; // your network SSID (name)
7
9
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
8
10
int status = WL_IDLE_STATUS; // the WiFi radio's status
11
+ String cloudSerialBuffer = " " ; // the string used to compose network messages from the received characters
9
12
10
13
WiFiClient wifiClient;
11
14
@@ -16,9 +19,8 @@ unsigned long getTime() {
16
19
void setup () {
17
20
// Initialize serial and wait for port to open:
18
21
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)) {}
22
24
23
25
// check for the presence of the shield:
24
26
if (WiFi.status () == WL_NO_SHIELD) {
@@ -33,42 +35,106 @@ void setup() {
33
35
}
34
36
35
37
// attempt to connect to WiFi network:
36
- while (status != WL_CONNECTED) {
38
+ int attempts = 0 ;
39
+ while (status != WL_CONNECTED && attempts < 6 ) {
37
40
Serial.print (" Attempting to connect to WPA SSID: " );
38
41
Serial.println (ssid);
39
42
// Connect to WPA/WPA2 network:
40
43
status = WiFi.begin (ssid, pass);
41
44
42
45
// wait 10 seconds for connection:
43
46
delay (10000 );
47
+ attempts++;
48
+ }
49
+
50
+ if (status != WL_CONNECTED) {
51
+ Serial.println (" Failed to connect to Wifi!" );
52
+ while (true );
44
53
}
45
54
46
55
// you're connected now, so print out the data:
47
56
Serial.print (" You're connected to the network" );
48
57
49
58
Serial.println ();
50
- Serial.println (" Attempting to connect to Arduino Cloud ... " );
59
+ Serial.println (" Attempting to connect to Arduino Cloud" );
51
60
52
61
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 ) {
54
70
Serial.println (" Failed to connect to Arduino Cloud!" );
55
71
while (1 );
56
72
}
57
73
58
74
Serial.println (" Successfully connected to Arduino Cloud :)" );
59
75
60
76
CloudSerial.begin (9600 );
77
+ CloudSerial.print (" I'm ready for blinking!\n " );
61
78
}
62
79
63
80
void loop () {
64
81
ArduinoCloud.poll ();
65
82
83
+ // check if there is something waiting to be read
66
84
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 ();
68
96
}
69
97
98
+ // Just to be able to simulate the board responses through the serial monitor
70
99
if (Serial.available ()) {
71
100
CloudSerial.write (Serial.read ());
72
101
}
73
102
}
74
103
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