-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWiFiWeatherGFX.ino
More file actions
546 lines (468 loc) · 14.4 KB
/
Copy pathWiFiWeatherGFX.ino
File metadata and controls
546 lines (468 loc) · 14.4 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Graphics
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "my_icons.h"
// Display
#define OLED_RESET -1
//#define OLED_RESET D2
#define SCREEN_WIDTH 128 // OLED display width, in pixels (does not work)
#define SCREEN_HEIGHT 64 // OLED display height, in pixels (does not work)
#define SSD1306_128_64 // Must be defined in the library Adafruit_SSD1306.h
Adafruit_SSD1306 display(OLED_RESET);
#include <SPI.h>
#include <Wire.h>
bool displayStatus = true;
#define DISPLAY_DIM_LEVEL 800
#define DISPLAY_OFF_LEVEL 10
// DH11 Sensor
#include <DHTesp.h>
#include <math.h>
DHTesp dht;
// WiFi
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// WatchDog (Interrupts)
#include <Ticker.h>
#define WatchDogSeconds 1
#define WatchDogMaxCounter 10
Ticker watchDogTicker;
volatile int watchDogCounter = 0;
#include "constant.h"
#define CITY "Marina%20Del%20Rey"
//#define CITY "Miami"
//#define CITY "New%20Orleans"
const char* ssid = SSDI;
const char* password = PASS;
const char* nowcast_url = "http://api.openweathermap.org/data/2.5/weather?q=" CITY ",us&mode=json&APPID=" APPID "&units=metric";
const char* forecast_url = "http://api.openweathermap.org/data/2.5/forecast?q=" CITY ",us&mode=json&APPID=" APPID "&units=metric";
//const char* forecast_url = "http://api.openweathermap.org/data/2.5/weather?q=Los%20Angeles,us&mode=json&APPID=" APPID "&units=metric";
HTTPClient http;
unsigned long respond_time = 0; // time that store the request moment
//#define WAIT_TIME 5000 // How ofter do the request (todo change to DEFINE)
//#define UPDATE_TIME 5000 // How do we wait before the next loop
//#define WAIT_TIME 60000 // How ofter do the request (todo change to DEFINE) 1 min
#define WAIT_TIME 60000 // How ofter do the request (todo change to DEFINE) 1 min
#define UPDATE_TIME 3000 // How do we wait before the next loop 2 sec
//json
#include <ArduinoJson.h>
float forecast_max_temp, forecast_min_temp;
bool WiFi_isconnected, WiFi_isanswer, WiFi_coldrun;
// Time
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
TimeChangeRule PDT = { "PDT", Second, Sun, Mar, 2, -420 }; //Daylight time = UTC - 7 hours
TimeChangeRule PST = { "PST", First, Sun, Nov, 2, -480 }; //Standard time = UTC - 8 hours
Timezone Pacific(PDT, PST);
struct Frame{
String update_time;
int weather_code;
String dht_string;
String temp;
String temp_range;
String weather_str;
bool umbrella;
float rain;
} frame;
/// SUPPORTING FUNCTION
// Memory
extern "C" {
#include "user_interface.h"
}
void mf()
{
// Print free memory
Serial.print("freeMemory = ");
Serial.println(system_get_free_heap_size());
}
void blinki(int n)
{
// Blink LED n time
unsigned long dtime = 100;
for (int i = 1; i <= n; i++)
{
digitalWrite(LED_BUILTIN,HIGH);
delay(dtime);
digitalWrite(LED_BUILTIN,LOW);
delay(dtime);
}
}
inline String deg()
{
// Return degree symbol
return String(char(247)); // 265, 9
}
inline String uptime()
{
// Return uptime string
return String((millis()- respond_time) / 1000);
}
String timeStr(time_t t)
{
// Return string time from time
// ToDo - return for LA, maybe with clock...
String timeStr;
t = Pacific.toLocal(t);
//timeStr = String(year(t))+"."+String(month(t))+"."+String(day(t))+" "+String(hour(t))+":"+String(minute(t))+":"+String(second(t));
timeStr = String(monthShortStr(month(t)))+" "+String(day(t))+" "+String(hour(t))+":"+String(minute(t));
return timeStr;
}
void clearFrame()
{
frame.update_time = "";
frame.weather_code = 0;
frame.dht_string = "";
frame.temp = "";
frame.temp_range = "";
frame.weather_str = "";
frame.rain = 0;
}
/// WatchDog
void watchDogManager_ISR()
{
watchDogCounter++;
if(watchDogCounter >= WatchDogMaxCounter){
Serial.println("Reset by WatchDog");
ESP.reset();
}
}
/// WIFI CONNECT FUNCTIONS
bool wifi_connect()
{
// Connect to WiFi, using 10 attemps. Return status of connection
int count=0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
bool WiFi_status = (WiFi.status() == WL_CONNECTED);
while (!WiFi_status and count < 10) {
delay(500);
count++;
}
if(WiFi_status)
{
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("No connection");
}
return WiFi_status;
}
/// DHT FUNCTIONS
void getDHT()
{
// Display text from DHT sensors
// Sensor 1
float humidity = round(dht.getHumidity());
float temperature = round(dht.getTemperature());
String temperature_str = String(int(temperature));
String humidity_str = String(int(humidity));
String Sensor1 = "" + temperature_str + deg() + "," + humidity_str + "%";
String time_str = String(millis());
frame.dht_string = dht.getStatusString();
if (dht.getStatus() == 0)
{
frame.dht_string += " DH11 " + Sensor1;
}
Serial.println(frame.dht_string);
}
/// GET AND PARSE JSON FUNCTIONS
bool getJson(const char* url, String * answer)
{
// Get json string by http client. Return if the string was returned.
Serial.println(url);
http.begin(url); //HTTPS ?
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
*answer = http.getString();
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
return (httpCode == HTTP_CODE_OK);
}
bool getNowcast()
{
// Get current weather
String respond;
Serial.println("Getting nowcast");
if(getJson(nowcast_url, &respond))
{
Serial.println("Processing the nowcast");
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(respond);
if(root.success())
{
Serial.println(respond);
JsonObject& weather = root["weather"][0];
JsonObject& main = root["main"];
//Status
const char* weather_str = weather["main"];
int code = weather["id"];
// Temperature
float temp = main["temp"];
float temp_max = main["temp_max"];
float temp_min = main["temp_min"];
//Time
time_t weatherTime = root["dt"];
// Rain
frame.rain = 0;
JsonObject& rain = weather["rain"];
if (rain.success())
{
float r = rain["3h"];
frame.rain = r;
}
frame.weather_code = code;
frame.weather_str = weather_str;
frame.temp = String(round(temp),0) + deg();
frame.temp_range = String(round(temp_min),0) + "-" + String(round(temp_max),0) + deg();
frame.update_time = timeStr(weatherTime);
return true;
}
else
{
Serial.println("Error processing nowcast");
frame.weather_code = -1;
frame.weather_str = "Processing error";
frame.temp = "???";
frame.temp_range = "";
}
}
else
{
Serial.println("Error getting nowcast json");
}
return false;
}
bool getForecast()
{
// Get weather forecast
String respond;
float temp_max = -999;
float temp_min = 999;
float temp_get;
float rain_get = 0;
Serial.println("Getting forecast");
if(getJson(forecast_url, &respond))
{
String tmp = respond;
Serial.println("Redusing forecast");
int count = 0;
for(int c=0; c<5; c++) // find only 4 items
{
count = respond.indexOf("\"dt\"", count+1);
}
//mf();
if(count > 2)
respond = respond.substring(0, count-2) + "]}";
//mf();
Serial.println(respond);
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(respond);
//mf();
if(root.success())
{
Serial.println("Parsing forecast");
Serial.println("Getting size:");
int walk = min(4, int(root["list"].size()));
Serial.println("Size: " + String(root["list"].size()));
for (int i = 0; i<walk; i++)
{
JsonObject& weather = root["list"][i];
JsonObject& main = weather["main"];
JsonObject& rain = weather["rain"];
int code = weather["id"];
temp_get = main["temp"];
temp_max = max(temp_max, temp_get);
temp_min = min(temp_min, temp_get);
if (code >= 900)
{
const char* weather_str = weather["main"];
frame.weather_code = code;
frame.weather_str = weather_str;
}
// list.rain.3h
// list.wind.speed
if (rain.success())
{
float r = rain["3h"];
rain_get += r;
}
weather.printTo(Serial);
Serial.println();
}
frame.rain += rain_get;
frame.temp_range = String(round(temp_min),0) + "-" + String(round(temp_max),0) + deg();
// Serial.print(frame.rain); Serial.print(" | " + frame.temp_range);
}
else
{
Serial.println("Parsing forecast error");
}
return true;
}
return false;
}
/// DISPLAY FUNCTIONS
void displayFrame()
{
display.setTextSize(2);
display.setCursor(69+2, 18); display.print(frame.temp);
display.setCursor(44+4, 38); display.print(frame.temp_range);
int code = frame.weather_code;
Serial.print("Rain: "); Serial.println(frame.rain);
if(frame.rain >= 0.1 && code < 900)
{
display.drawBitmap(X_ICON, Y_ICON, umbrella_icon, W_ICON, H_ICON, WHITE);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(12, 24);
display.print(String(frame.rain));
display.setTextColor(WHITE);
}
else
{
if (code == 800) // clear
{
display.drawBitmap(X_ICON, Y_ICON, clear_icon, W_ICON, H_ICON, WHITE);
}
else if(code == 801 ) // partial cloud
{
display.drawBitmap(X_ICON, Y_ICON, partial_cloud_icon, W_ICON, H_ICON, WHITE);
}
else if(code > 801 && code < 805) // cloud
{
display.drawBitmap(X_ICON, Y_ICON, cloud_icon, W_ICON, H_ICON, 1);
}
else if(code >= 900 ) // Additional or extrime
{
display.drawBitmap(X_ICON, Y_ICON, alert_icon, W_ICON, H_ICON, WHITE); // TODO - CHANGE
}
else if(code >= 700 && code < 800) // Atmosphere
{
display.drawBitmap(X_ICON, Y_ICON, hase_icon, W_ICON, H_ICON, WHITE);
}
else if(code >= 200 && code < 700) // All sort of rain, show, thunder
{
display.drawBitmap(X_ICON, Y_ICON, rain_icon, W_ICON, H_ICON, WHITE);
}
else // Unknown
{
display.drawBitmap(X_ICON, Y_ICON, unknown_icon, W_ICON, H_ICON, WHITE); // TODO - CHANGE
}
}
display.setTextSize(1);
display.setCursor(0, 56); display.print(frame.weather_str);
display.setCursor(104, 0); display.print(code);
display.setCursor(12, 0); display.print(frame.update_time);
}
void displayWiFi()
{
if((WiFi.status() == WL_CONNECTED)) {
display.drawBitmap(X_WIFI, Y_WIFI, wifi_connected, W_WIFI, H_WIFI, WHITE); // TODO - CHANGE
}
else
{
display.drawBitmap(X_WIFI, Y_WIFI, wifi_error, W_WIFI, H_WIFI, WHITE); // TODO - CHANGE
}
display.setTextSize(1);
display.setCursor(104, 8);
display.print(uptime());
}
void displayDHT()
{
display.setTextSize(1);
display.setCursor(0,8);
display.print(frame.dht_string);
}
void setContrast()
{
int A0_read = analogRead(A0);
//uint8_t brightness = map(A0_read, 0, 1024, 0, 255);
if (A0_read >DISPLAY_OFF_LEVEL )
{
wakeDisplay();
if (A0_read > DISPLAY_DIM_LEVEL )
display.dim(false);
else
display.dim(true);
}
else
sleepDisplay();
Serial.print("Photoresistor: "); Serial.println(A0_read); //Serial.print(" - "); Serial.println(brightness);
}
void sleepDisplay() {
display.ssd1306_command(SSD1306_DISPLAYOFF);
displayStatus = false;
}
void wakeDisplay() {
if (!displayStatus)
{
display.ssd1306_command(SSD1306_DISPLAYON);
displayStatus = true;
}
}
void setup()
{
blinki(5);
// Setup Serial
Serial.begin(115200);
delay(500); // Give some time to connest to the Serial
/// Attach watchdog
watchDogTicker.attach(WatchDogSeconds, watchDogManager_ISR);
pinMode(LED_BUILTIN,OUTPUT);
// Setup DHT11
dht.setup(13, DHTesp::DHT11); // Connect DHT sensor to GPIO 13
//dht.setup(2); // Connect DHT sensor to GPIO 2 which is D4
//dht_2.setup(13); // Connect temp DHT sensor to GPIO 13 which is D7
//setup Graphix
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
display.display();
display.setTextColor(WHITE);
// symbolTable();
// Setup WiFi
WiFi_coldrun = true; // Check if we just startes the device
// Setup json
clearFrame();
mf();
}
void loop()
{
watchDogCounter = 0; // Reset WatchDog counter.
setContrast();
blinki(2);
digitalWrite(LED_BUILTIN,LOW);
getDHT();
if((WiFi.status() == WL_CONNECTED)) {
if ((millis() >= respond_time + WAIT_TIME) || WiFi_coldrun) // We will wait first wait_time because millis and respond_time will be 0 on start
{
Serial.print("WiFi update: " + String(millis()) + " >= " + String(respond_time + WAIT_TIME)); Serial.println("");
//WiFi_isanswer = wifi_nowcast();
//mf();
if (getNowcast())
{
respond_time = millis();
WiFi_coldrun = false;
getForecast(); // basic information is recived, get forecast
}
}
}
display.clearDisplay();
displayWiFi();
displayFrame();
displayDHT();
display.display();
mf();
delay(UPDATE_TIME);
Serial.println("Counter: " + String(watchDogCounter));
}