-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_weather_display.ino
456 lines (365 loc) · 11.5 KB
/
simple_weather_display.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* Easy Matrix Weather Display
* Weather Matrix Display
* by Jefferson Paredes
* Version: 1.0.0
*
* Simple introduction to Internet of Things project
* Check this project on Instructables.com
* Link: https://www.instructables.com/id/Weather-Matrix-Display/
*
* Contribute to improve this project on github!
* Link: https://github.com/imjeffparedes/iot-simple-weather-matrix-display
*
* Need Help?
* Please email me here: imjeffparedes@gmail.com
* Please Follow me on instagram: @imjeffparedes
* Licensed under Creative Commons BY-NC-SA
*/
#include <Thread.h>
#include <SPI.h>
#include <WiFi101.h>
#include<HexFont.h>
#include <ArduinoJson.h>
/***** Weather Objects ****/
Thread updateWeatherThread = Thread();
Thread checkInternetThread = Thread();
// Lets use thread to check the condition every interval
Thread apiRequestThread = Thread();
// set time interval of each request in seconds
int timeInterval = 10;
// set time counter to start
int timeCount = 0;
char ssid[] = ""; // your network SSID (name)
char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
String apiKey = "8f0ff191defb4a20b5583518171203"; // your apixu api key
String coordinates = "34.0522,118.2437"; // your location coordinates
int status = WL_IDLE_STATUS;
char server[] = "api.apixu.com"; // your api address
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
// save json result here
String readResult;
// tells if the data is already collected
bool dataCollected = false;
// curent weather condition
String currentCondition;
String currentConditionCode;
/* 1000 Sunny
* 1003 Partly Cloudy
* 1006 Cloudy
* 1009 Overcast
* 1030 Mist
* 1063 Patchy rain nearby
* 1066 Patchy snow nearby
* 1069 Patchy sleet nearby
* 1072 Patchy freezing drizzle nearby
* 1087 Thundery outbreaks in nearby
* 1114 Blowing snow
* 1117 Blizzard
* 1135 Fog
* 1147 Freezing fog
* 1150 Patchy light drizzle
* 1153 Light drizzle
* 1168 Freezing drizzle
* 1171 Heavy freezing drizzle
* 1180 Patchy light rain
* 1183 Light rain
* 1186 Moderate rain at times
* 1189 Moderate rain
* 1192 Heavy rain at times
* 1195 Heavy rain
* 1198 Light freezing rain
* 1201 Moderate or heavy freezing rain
* 1204 Light sleet
* 1207 Moderate or heavy sleet
* 1210 Patchy light snow
* 1213 Light snow
* 1216 Patchy moderate snow
* 1219 Moderate snow
* 1222 Patchy heavy snow
* 1225 Heavy snow
* 1237 Ice pellets
* 1240 Light rain shower
* 1243 Moderate or heavy rain shower
* 1246 Torrential rain shower
* 1249 Light sleet showers
* 1252 Moderate or heavy sleet
* 1255 Light snow showers
* 1258 Moderate or heavy snow showers
* 1261 Light showers of ice pellets
* 1264 Moderate or heavy showers
* 1273 Patchy light rain in area with thunder
* 1276 Moderate or heavy rain in area with thunder
* 1279 Patchy light snow in area
* 1282 Moderate or heavy snow in area with thunder
*
*/
/**********End of weather objects ********/
/***** Display Objects ************/
Thread refreshThread = Thread();
Thread animateThread = Thread();
// 2-dimensional array of row pin numbers:
const int col[8] = {
10,3,2,7,0,8,12,13
};
// 2-dimensional array of column pin numbers:
const int row[8] = {
6,11,20,9,5,21,4,1
};
// 2-dimensional array of pixels:
int pixels[8][8],count=1000;
int supportedASCICount = 0;
byte currentChar[8] , nextChar[8] ;
int currentLetter = 0 ;
String message = "Loading...";
/**********End of display objects ********/
Thread sampleLoopThread = Thread();
void setup() {
// initialize the I/O pins as outputs
// iterate over the pins:
for (int thisPin = 0; thisPin < 8; thisPin++) {
// initialize the output pins:
pinMode(col[thisPin], OUTPUT);
pinMode(row[thisPin], OUTPUT);
// take the col pins (i.e. the cathodes) high to ensure that
// the LEDS are off:
digitalWrite(col[thisPin], HIGH);
}
for (int x = 0; x < 8; x++) {
currentChar[x] = cp437_font[supportedAsci.indexOf(" ")][x];
}
refreshThread.onRun(refreshScreen);
animateThread.onRun(slideRight);
animateThread.setInterval(100);
setupChar();
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// start update weather
checkInternetThread.onRun(checkConnection);
// set one second delay for every loop
// this will be the basis of request interval
// wait 10 seconds for connection:
checkInternetThread.setInterval(1);
// start weather request
apiRequestThread.onRun(getCondition);
// set one second delay for every loop
// this will be the basis of request interval
apiRequestThread.setInterval(5000);
// start update weather
updateWeatherThread.onRun(updateWeatherCondition);
// set one second delay for every loop
// this will be the basis of request interval
updateWeatherThread.setInterval(100);
}
void loop() {
// refresh screen display
// checks if thread should run
if(refreshThread.shouldRun())
refreshThread.run();
if(animateThread.shouldRun())
animateThread.run();
// checks if thread should run
if(checkInternetThread.shouldRun())
checkInternetThread.run();
// checks if thread should run
if(apiRequestThread.shouldRun())
apiRequestThread.run();
// checks if thread should run
if(updateWeatherThread.shouldRun())
updateWeatherThread.run();
}
void setupChar(){
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
bool v = covertToBin(currentChar[x])[y] == '1';
if(v){
pixels[x][y] = LOW;
}else{
pixels[x][y] = HIGH;
}
}
}
}
void refreshScreen() {
// iterate over the rows (anodes):
for (int thisRow = 0; thisRow < 8; thisRow++) {
// take the row pin (anode) high:
digitalWrite(row[thisRow], HIGH);
// iterate over the columns (cathodes):
for (int thisCol = 0; thisCol < 8; thisCol++) {
// get the state of the current pixel;
int thisPixel = pixels[thisRow][thisCol];
// when the row is HIGH and the column is LOW,
// the LED where they meet turns on:
digitalWrite(col[thisCol], thisPixel);
// turn the pixel off:
if (thisPixel == LOW) {
digitalWrite(col[thisCol], HIGH);
}
}
// take the row pin low to turn off the whole row:
digitalWrite(row[thisRow], LOW);
}
}
String covertToBin(byte rowHex){
String binString = String(rowHex,BIN);
int binStringCount = binString.length();
for(int i=0; i< 8-binStringCount ;i++){
binString = "0" +binString;
}
return binString;
}
int currentCol = 0;
void slideRight(){
int colTransferStart = 7-currentCol;
// move all active led one column to left
for(int x= 0 ; x<colTransferStart ;x++){
currentChar[x] = currentChar[x+1];
}
// copy current letter starts from last column of previous
int colCountOfCurrentLetter = 0;
for(int x= colTransferStart ; x<8 ;x++){
currentChar[x] = cp437_font[supportedAsci.indexOf( message[currentLetter] )][ colCountOfCurrentLetter];
colCountOfCurrentLetter++;
}
currentCol++;
if(currentCol > 7){
currentCol=0;
currentLetter++;
}
//if message is complete
if(currentLetter==message.length()){
currentLetter=0;
}
setupChar();
}
/***** Weather Data Function ******/
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
// this function will make api request
void makeRequest(){
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /v1/current.json?key="+ apiKey +"&q=" +coordinates+ " HTTP/1.1");
client.println("Host: api.apixu.com");
client.println("Connection: close");
client.println();
}
}
// loop
void getCondition(){
// if we reached the time interval
// do a request
if(timeInterval == timeCount && status == WL_CONNECTED){
dataCollected = false;
makeRequest();
//set time count back to start
timeCount = 0;
}
// count time after 1 second
timeCount++;
}
void updateWeatherCondition(){
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
readResult += c;
Serial.write(c);
}
// if the server's disconnected, stop the client:
if (!client.connected() && !dataCollected)
{
Serial.println();
// Copy from start of json result
// if we have { means that the start of json values
String rawJson = readResult.substring(readResult.indexOf("{")) ;
// initialize json parser
DynamicJsonBuffer jsonBuffer;
JsonObject& result = jsonBuffer.parseObject(rawJson);
//update display text to new weather condition
message = "";
//get the current condition
String condition = result["current"]["condition"]["text"];
appendMessage(condition);
appendMessage(" ");
String temp_c = result["current"]["temp_c"] ;
appendMessage(temp_c);
appendMessage("C");
appendMessage(" ");
String humidity = result["current"]["humidity"];
appendMessage(humidity);
appendMessage("H");
appendMessage(" ");
Serial.println("Current condition: "+ message);
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
Serial.println("Connection closed.");
dataCollected = true;
}
}
int checkInternetCounter = 0;
int checkInternetTimeInterval = 10000;
bool connectionStatusPrinted = false;
void checkConnection(){
//update display message when connecting to Wifi
if(status!= WL_CONNECTED && message!="Connecting to WIFI..."){
changeMessage("Connecting to WIFI...");
connectionStatusPrinted = false;
}
//check if needed to connect to wifi
if(checkInternetCounter == checkInternetTimeInterval && status!= WL_CONNECTED){
// attempt to connect to WiFi network:
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
Serial.println("Please wait...");
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
checkInternetCounter = 0;
}
checkInternetCounter++;
//count again if check internet time interval is already met
if(checkInternetCounter>checkInternetTimeInterval)
checkInternetCounter = 0;
//update display message when checking data
if(status == WL_CONNECTED && !connectionStatusPrinted){
Serial.println("Connected to wifi");
changeMessage("Checking data...");
connectionStatusPrinted = true;
printWiFiStatus();
}
}
void changeMessage(String msg){
message = msg;
// add long space after displaying message
message+=" ";
currentLetter = 0;
}
void appendMessage(String msg){
message += msg;
currentLetter = 0;
}