-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathMKR1000_Cloud_Blink.ino
149 lines (117 loc) · 3.76 KB
/
MKR1000_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <WiFi101.h> // change to WiFiNINA.h if you are using the MKR WiFi 1010 or MKR Vidor 4000
#include <ArduinoCloudV2.h>
#include "arduino_secrets.h"
#define TIMEOUT 7000
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
WiFiClient wifiClient;
unsigned long getTime() {
return WiFi.getTime();
}
int position;
void onPositionUpdate() {
Serial.print("New position value: ");
Serial.println(position);
}
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
int timeout = millis() + TIMEOUT;
while (!Serial && (millis() < timeout)) {}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
if (!ArduinoCloud.begin(wifiClient)) {
Serial.println("Starting Arduino Cloud failed!");
while (true);
}
// attempt to connect to WiFi network:
int attempts = 0;
while (status != WL_CONNECTED && attempts < 6) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
attempts++;
}
if (status != WL_CONNECTED) {
Serial.println("Failed to connect to Wifi!");
while (true);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
Serial.println();
Serial.println("Attempting to connect to Arduino Cloud");
ArduinoCloud.onGetTime(getTime);
attempts = 0;
while (!ArduinoCloud.connect() && attempts < 10) {
Serial.print(".");
attempts++;
}
if (attempts >= 10) {
Serial.println("Failed to connect to Arduino Cloud!");
while (1);
}
Serial.println("Successfully connected to Arduino Cloud :)");
ArduinoCloud.addProperty(position, READ, 10*SECONDS, onPositionUpdate);
CloudSerial.begin(9600);
CloudSerial.print("I'm ready for blinking!\n");
}
void loop() {
ArduinoCloud.poll();
// 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') {
manageString();
}
}
else // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check.
{
manageString();
}
// Just to be able to simulate the board responses through the serial monitor
if (Serial.available()) {
CloudSerial.write(Serial.read());
}
}
void manageString() {
// Don't proceed if the string is empty
if (cloudSerialBuffer.equals("")) return;
// Remove whitespaces
cloudSerialBuffer.trim();
// Make it uppercase;
cloudSerialBuffer.toUpperCase();
if (cloudSerialBuffer.equals("ON")) {
digitalWrite(6, HIGH);
}
if (cloudSerialBuffer.equals("OFF")) {
digitalWrite(6, 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');
}
}