-
Notifications
You must be signed in to change notification settings - Fork 0
/
Windo.ino
389 lines (328 loc) · 9.72 KB
/
Windo.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int incomingByte = 0;
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed
#define WLAN_SSID "Seb iFoon" // cannot be longer than 32 characters!
#define WLAN_PASS "123abcd-/"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define IDLE_TIMEOUT_MS 30000 // Amount of time to wait (in milliseconds) with no data
// received before closing the connection. If you know the server
// you're accessing is quick to respond, you can reduce this value.
// What page to grab!
#define API_KEY "af27abcce2eb087beb03b1b606dd5" // Sign up at https://www.worldweatheronline.com/
#define CITY "Rotterdam" // Provice/city, or postal code
#define WEBSITE "api.worldweatheronline.com"
#define WEBPAGE "/free/v2/weather.ashx?format=csv&q=" CITY "&key=" API_KEY
/**************************************************************************/
/*!
@brief Sets up the HW and the CC3000 module (called automatically
on startup)
*/
/**************************************************************************/
#define COLS 16
#define ROWS 2
uint32_t ip;
void setup(void)
{
Serial.begin(115200);
Serial.println(F("Welcome to Windo!\n"));
//
// Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC);
/* Initialise the module */
Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
Serial.println(F("ERR. INIT CC3000"));
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
}
bool connect(Adafruit_CC3000_Client& www) {
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
return false;
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);
}
ip = 0;
// Try looking up the website's IP address
Serial.print(WEBSITE); Serial.print(F(" -> "));
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
www = cc3000.connectTCP(ip, 80);
if (!www.connected()) {
cc3000.disconnect();
Serial.println(F("ERR. TCP CONNECT"));
Serial.println("ERROR: connection failed");
return false;
}
return true;
}
void disconnect(Adafruit_CC3000_Client& www) {
Serial.println(F("\n\nClosing"));
www.close();
Serial.println(F("\n\nDisconnecting"));
cc3000.disconnect();
}
void loop(void)
{
Adafruit_CC3000_Client www;
if (connect(www)) {
pollWeather(www);
disconnect(www);
}
delay(15 * 10000);
}
void pollWeather(Adafruit_CC3000_Client& www) {
/* Try connecting to the website.
Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
*/
www.fastrprint(F("GET "));
www.fastrprint(F(WEBPAGE));
www.fastrprint(F(" HTTP/1.1\r\n"));
www.fastrprint(F("Host: ")); www.fastrprint(F(WEBSITE)); www.fastrprint(F("\r\n"));
// www.fastrprint(F("Connection: Close")); www.fastrprint(F("\r\n"));
www.fastrprint(F("\r\n"));
www.println();
/* Read the HTTP status line */
String status = readLine(www);
if (status.compareTo("HTTP/1.1 200 OK") != 0) {
Serial.print(F("ERR. HTTP STATUS"));
Serial.print(F("ERROR: unexpected HTTP header - ")); Serial.println(status);
return;
}
status = "";
/* Read the HTTP headers */
long contentLength;
while (true) {
String name, value;
if (!readHeader(www, name, value)) {
Serial.println(F("ERR. HTTP HEAD"));
Serial.println(F("ERROR: problem with headers"));
return;
}
if (name.length() == 0 && value.length() == 0) {
break;
} else if (name.compareTo("Content-Length") == 0) {
contentLength = value.toInt();
}
}
/* Read the body */
long consumed = 0;
bool comment = false;
long row = 0;
long col = 0;
String cell;
while (consumed < contentLength) {
int16_t c = readChar(www);
if (c < 0) {
Serial.println(F("ERR. HTTP BODY"));
Serial.println(F("ERROR: content truncated"));
return;
}
consumed++;
switch (c) {
case '#':
comment = true;
break;
case '\n':
comment = false;
if (col > 0 || cell.length() > 0) {
handleCell(row, col, cell);
cell = "";
col = 0;
row++;
}
break;
case ',':
if (!comment) {
handleCell(row, col, cell);
cell = "";
col++;
}
break;
default:
if (!comment) {
cell += (char) c;
}
break;
}
}
}
void handleCell(long row, long col, String& cell) {
// Serial.print(F("row = ")); Serial.print(row); Serial.print(F(", col = "));
// Serial.print(col); Serial.print(F(": ")); Serial.println(cell);
if (row == 0 && col == 5) {
Serial.println(cell);
if((cell == "Sunny") || (cell == "Cloudy") || (cell == "Partly Cloudy")){
incomingByte = 85;
}
if(cell == "Patchy light"){
incomingByte = 90;
}
if(cell == "Patchy rain"){
incomingByte = 80;
}
}
Serial.println(incomingByte);
myservo.attach(9);
if(pos < incomingByte){
for(pos; pos < incomingByte; pos += 1)
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
}
if(pos > incomingByte){
for(pos; pos>incomingByte; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
} else {
if(pos == incomingByte)
myservo.detach();
}
}
bool readHeader(Adafruit_CC3000_Client& www, String& name, String& value) {
int16_t c;
/* Read header name */
while (true) {
c = readChar(www);
if (c < 0) {
return false; /* disconnect while reading header name */
} else if (c == ':') {
break; /* end of header name */
} else {
name += (char) c;
if (name.endsWith("\r\n")) {
/* Should signify the end of headers */
name = name.substring(0, name.length()-2);
return true;
}
}
}
/* Read header value */
while (true) {
/* Discard whitespace */
while (true) {
c = readChar(www);
if (c < 0) {
return false; /* disconnect */
} else if (c == ' ' || c == '\t') {
continue;
} else {
value += (char) c;
break;
}
}
/* Read until CRLF */
while (true) {
c = readChar(www);
if (c < 0) {
return false;
}
value += (char) c;
if (value.endsWith("\r\n")) {
value = value.substring(0, value.length()-2);
break;
}
}
/* Check for header continuation */
c = readChar(www);
if (c != ' ' && c != '\t') {
putBack(c);
break;
} else {
value += ' ';
}
}
}
int16_t buf = -1;
void putBack(char c) {
if (buf >= 0) {
Serial.println(F("ERROR: too many characters put back!"));
return;
}
buf = c;
}
int16_t readChar(Adafruit_CC3000_Client& www) {
if (buf >= 0) {
char c = (char) buf;
buf = -1;
return c;
}
while (www.connected()) {
if (www.available()) {
return www.read();
}
}
return -1;
}
String readLine(Adafruit_CC3000_Client& www) {
/* Read data until either the connection is closed, or the idle timeout is reached. */
String line = "";
int16_t c;
while (0 <= (c = readChar(www))) {
line += (char) c;
if (line.endsWith("\r\n")) {
return line.substring(0, line.length()-2);
}
}
return line;
}
/**************************************************************************/
/*!
@brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}