1 #include <FS.h> //this needs to be first, or it all crashes and burns...
3 #include <WiFi.h> //https://github.com/esp8266/Arduino
7 #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
9 #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
11 #include <time.h> // Built-in
12 #include <SPI.h> // Built-in
14 #include <HTTPClient.h>
16 #include <GxEPD2_BW.h>
17 #include <U8g2_for_Adafruit_GFX.h>
22 #include "weather_types.h"
24 #define SCREEN_WIDTH 800 // Set for landscape mode
25 #define SCREEN_HEIGHT 480
27 enum alignment {LEFT, RIGHT, CENTER};
29 // Connections for e.g. Waveshare ESP32 e-Paper Driver Board
30 static const uint8_t EPD_BUSY = 25;
31 static const uint8_t EPD_CS = 15;
32 static const uint8_t EPD_RST = 26;
33 static const uint8_t EPD_DC = 27;
34 static const uint8_t EPD_SCK = 13;
35 static const uint8_t EPD_MISO = 12; // Master-In Slave-Out not used, as no data from display
36 static const uint8_t EPD_MOSI = 14;
38 GxEPD2_BW<GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT> display(GxEPD2_750_T7(/*CS=*/ EPD_CS, /*DC=*/ EPD_DC, /*RST=*/ EPD_RST, /*BUSY=*/ EPD_BUSY)); // B/W display
39 //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT> display(GxEPD2_750(/*CS=*/ EPD_CS, /*DC=*/ EPD_DC, /*RST=*/ EPD_RST, /*BUSY=*/ EPD_BUSY)); // 3-colour displays
40 // use GxEPD_BLACK or GxEPD_WHITE or GxEPD_RED or GxEPD_YELLOW depending on display type
42 U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;
46 //define your default values here, if there are different values in config.json, they are overwritten.
47 char api_key[40] = ""; // See: https://openweathermap.org/ // It's free to get an API key, but don't take more than 60 readings/minute!
48 char latitude[8] = "55.5";
49 char longitude[8] = "37.5";
50 char language[4] = "RU"; // NOTE: Only the weather description is translated by OWM
51 // Examples: Arabic (AR) Czech (CZ) English (EN) Greek (EL) Persian(Farsi) (FA) Galician (GL) Hungarian (HU) Japanese (JA)
52 // Korean (KR) Latvian (LA) Lithuanian (LT) Macedonian (MK) Slovak (SK) Slovenian (SL) Vietnamese (VI)
54 char current_weather_server[255] = "";
55 char current_weather_uri[255] = "";
56 char current_weather_pressure[40] = "PRESSURE";
57 char current_weather_temperature[40] = "TEMPERATURE_C";
58 char current_weather_humidity[40] = "HUMIDITY";
59 char current_weather_windspeed[40] = "";
60 char current_weather_lux[40] = "LUX";
62 const char server[] = "api.openweathermap.org";
64 int SleepDuration = 10;
66 //flag for saving data
67 bool shouldSaveConfig = false;
69 //callback notifying us of the need to save config
70 void saveConfigCallback () {
71 Serial.println("Should save config");
72 shouldSaveConfig = true;
76 display.init(115200, true, 2); // init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration, bool pulldown_rst_mode)
78 SPI.begin(EPD_SCK, EPD_MISO, EPD_MOSI, EPD_CS);
79 u8g2Fonts.begin(display); // connect u8g2 procedures to Adafruit GFX
80 u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
81 u8g2Fonts.setFontDirection(0); // left to right (this is default)
82 u8g2Fonts.setForegroundColor(GxEPD_BLACK); // apply Adafruit GFX color
83 u8g2Fonts.setBackgroundColor(GxEPD_WHITE); // apply Adafruit GFX color
85 display.fillScreen(GxEPD_WHITE);
86 display.setFullWindow();
91 // The extra parameters to be configured (can be either global or just in the setup)
92 // After connecting, parameter.getValue() will get you the configured value
93 // id/name placeholder/prompt default length
94 WiFiManagerParameter custom_api_key("api_key", "OWN API key", api_key, 40);
95 WiFiManagerParameter custom_latitude("latitide", "Latitude", latitude, 8);
96 WiFiManagerParameter custom_longitude("longitude", "Longitude", longitude, 8);
97 WiFiManagerParameter custom_language("language", "Language", language, 4);
98 WiFiManagerParameter custom_current_weather_server("current_weather_server", "Weather server", current_weather_server, 255);
99 WiFiManagerParameter custom_current_weather_uri("current_weather_uri", "Weather URI", current_weather_uri, 255);
100 WiFiManagerParameter custom_current_weather_pressure("current_weather_pressure", "Pressure key", current_weather_pressure, 40);
101 WiFiManagerParameter custom_current_weather_temperature("current_weather_temperature", "Temperature key", current_weather_temperature, 40);
102 WiFiManagerParameter custom_current_weather_humidity("current_weather_humidity", "Humidity key", current_weather_humidity, 40);
103 WiFiManagerParameter custom_current_weather_windspeed("current_weather_windspeed", "Windspeed key", current_weather_windspeed, 40);
104 WiFiManagerParameter custom_current_weather_lux("current_weather_lux", "Lux key", current_weather_lux, 40);
107 //Local intialization. Once its business is done, there is no need to keep it around
108 WiFiManager wifiManager;
110 //set config save notify callback
111 wifiManager.setSaveConfigCallback(saveConfigCallback);
113 //add all your parameters here
114 wifiManager.addParameter(&custom_api_key);
115 wifiManager.addParameter(&custom_latitude);
116 wifiManager.addParameter(&custom_longitude);
117 wifiManager.addParameter(&custom_language);
118 wifiManager.addParameter(&custom_current_weather_server);
119 wifiManager.addParameter(&custom_current_weather_uri);
120 wifiManager.addParameter(&custom_current_weather_pressure);
121 wifiManager.addParameter(&custom_current_weather_temperature);
122 wifiManager.addParameter(&custom_current_weather_humidity);
123 wifiManager.addParameter(&custom_current_weather_windspeed);
124 wifiManager.addParameter(&custom_current_weather_lux);
126 wifiManager.setTimeout(300);
128 if (!wifiManager.startConfigPortal()) {
129 Serial.println("failed to connect and hit timeout");
131 //reset and try again, or maybe put it to deep sleep
135 //if you get here you have connected to the WiFi
136 Serial.println("connected...");
138 //read updated parameters
139 strcpy(api_key, custom_api_key.getValue());
140 strcpy(latitude, custom_latitude.getValue());
141 strcpy(longitude, custom_longitude.getValue());
142 strcpy(language, custom_language.getValue());
143 strcpy(current_weather_server, custom_current_weather_server.getValue());
144 strcpy(current_weather_uri, custom_current_weather_uri.getValue());
145 strcpy(current_weather_pressure, custom_current_weather_pressure.getValue());
146 strcpy(current_weather_temperature, custom_current_weather_temperature.getValue());
147 strcpy(current_weather_humidity, custom_current_weather_humidity.getValue());
148 strcpy(current_weather_windspeed, custom_current_weather_windspeed.getValue());
149 strcpy(current_weather_lux, custom_current_weather_lux.getValue());
151 //save the custom parameters to FS
152 if (shouldSaveConfig) {
153 Serial.println("saving config");
154 DynamicJsonDocument json(1024);
155 json["api_key"] = api_key;
156 json["latitude"] = latitude;
157 json["longitude"] = longitude;
158 json["language"] = language;
159 json["current_weather_server"] = current_weather_server;
160 json["current_weather_uri"] = current_weather_uri;
161 json["current_weather_pressure"] = current_weather_pressure;
162 json["current_weather_temperature"] = current_weather_temperature;
163 json["current_weather_humidity"] = current_weather_humidity;
164 json["current_weather_windspeed"] = current_weather_windspeed;
165 json["current_weather_lux"] = current_weather_lux;
167 File configFile = SPIFFS.open("/config.json", "w");
169 Serial.println("failed to open config file for writing");
172 serializeJson(json, Serial);
173 serializeJson(json, configFile);
181 byte need_setup = false;
182 // put your setup code here, to run once:
183 Serial.begin(115200);
186 //read configuration from FS json
187 Serial.println("mounting FS...");
189 if (SPIFFS.begin(true)) {
190 Serial.println("mounted file system");
191 if (SPIFFS.exists("/config.json")) {
192 //file exists, reading and loading
193 Serial.println("reading config file");
194 File configFile = SPIFFS.open("/config.json", "r");
196 Serial.println("opened config file");
197 size_t size = configFile.size();
198 // Allocate a buffer to store contents of the file.
199 std::unique_ptr<char[]> buf(new char[size]);
201 configFile.readBytes(buf.get(), size);
203 DynamicJsonDocument json(1024);
204 auto deserializeError = deserializeJson(json, buf.get());
205 serializeJson(json, Serial);
206 if ( ! deserializeError ) {
207 Serial.println("\nparsed json");
208 if (json.containsKey("api_key")) { strcpy(api_key, json["api_key"]); }
209 if (json.containsKey("latitude")) { strcpy(latitude, json["latitude"]); }
210 if (json.containsKey("longitude")) { strcpy(longitude, json["longitude"]); }
211 if (json.containsKey("language")) { strcpy(language, json["language"]); }
212 if (json.containsKey("current_weather_server")) { strcpy(current_weather_server, json["current_weather_server"]); }
213 if (json.containsKey("current_weather_uri")) { strcpy(current_weather_uri, json["current_weather_uri"]); }
214 if (json.containsKey("current_weather_pressure")) { strcpy(current_weather_pressure, json["current_weather_pressure"]); }
215 if (json.containsKey("current_weather_temperature")) { strcpy(current_weather_temperature, json["current_weather_temperature"]); }
216 if (json.containsKey("current_weather_humidity")) { strcpy(current_weather_humidity, json["current_weather_humidity"]); }
217 if (json.containsKey("current_weather_windspeed")) { strcpy(current_weather_windspeed, json["current_weather_windspeed"]); }
218 if (json.containsKey("current_weather_lux")) { strcpy(current_weather_lux, json["current_weather_lux"]); }
220 Serial.println("failed to load json config");
230 Serial.println("failed to mount FS");
237 WiFiManager wifiManager;
238 wifiManager.setTimeout(10);
239 if(!wifiManager.autoConnect()) {
240 Serial.println("failed to connect and hit timeout");
246 Serial.println("\nlocal ip");
247 Serial.println(WiFi.localIP());
256 int HourlyDT[MaxHourlyFC];
257 float HourlyTemp[MaxHourlyFC];
258 float HourlyFeelsLike[MaxHourlyFC];
259 float HourlyPressure[MaxHourlyFC];
260 float HourlyHumidity[MaxHourlyFC];
261 float HourlyClouds[MaxHourlyFC];
262 float HourlyWindSpeed[MaxHourlyFC];
263 float HourlyRain[MaxHourlyFC];
264 float HourlySnow[MaxHourlyFC];
265 float HourlyPrecip[MaxHourlyFC];
267 int DailyDT[MaxDailyFC];
268 float DailyTempMin[MaxDailyFC];
269 float DailyTempMax[MaxDailyFC];
270 float DailyFeelsLikeMin[MaxDailyFC];
271 float DailyFeelsLikeMax[MaxDailyFC];
272 float DailyPressure[MaxDailyFC];
273 float DailyHumidity[MaxDailyFC];
274 float DailyClouds[MaxDailyFC];
275 float DailyWindSpeed[MaxDailyFC];
276 float DailyRain[MaxDailyFC];
277 float DailySnow[MaxDailyFC];
278 float DailyPrecip[MaxDailyFC];
280 String UnixTime(int unix_time) {
281 time_t tm = unix_time;
282 struct tm *now_tm = localtime(&tm);
284 strftime(output, sizeof(output), "%H:%M %d/%m/%y", now_tm);
288 String UnixTimeOnly(int unix_time) {
289 time_t tm = unix_time;
290 struct tm *now_tm = localtime(&tm);
292 strftime(output, sizeof(output), "%H:%M", now_tm);
296 String UnixDate(int unix_time) {
297 time_t tm = unix_time;
298 struct tm *now_tm = localtime(&tm);
300 strftime(output, sizeof(output), "%d/%m/%y", now_tm);
304 bool DecodeWeather(WiFiClient& json) {
305 DynamicJsonDocument doc(35 * 1024);
306 // Deserialize the JSON document
307 DeserializationError error = deserializeJson(doc, json);
308 // Test if parsing succeeds.
310 Serial.print(F("deserializeJson() failed: "));
311 Serial.println(error.c_str());
314 // convert it to a JsonObject
315 JsonObject root = doc.as<JsonObject>();
317 TimeZoneOffset = root["timezone_offset"].as<int>();
319 SunRise = root["current"]["sunrise"].as<int>();
320 SunSet = root["current"]["sunset"].as<int>();
322 CurrentWeather.dt = root["current"]["dt"].as<int>();
323 CurrentWeather.temp = root["current"]["temp"].as<float>();
324 CurrentWeather.feels_like = root["current"]["feels_like"].as<float>();
325 CurrentWeather.pressure = 0.75 * root["current"]["pressure"].as<float>();
326 CurrentWeather.humidity = root["current"]["humidity"].as<float>();
327 CurrentWeather.clouds = root["current"]["clouds"].as<float>();
328 CurrentWeather.wind_speed = root["current"]["wind_speed"].as<float>();
329 CurrentWeather.wind_deg = root["current"]["wind_deg"].as<float>();
330 CurrentWeather.rain = root["current"]["rain"]["1h"].as<float>();
331 CurrentWeather.snow = root["current"]["snow"]["1h"].as<float>();
332 CurrentWeather.description = root["current"]["weather"][0]["description"].as<char*>();
333 CurrentWeather.icon = root["current"]["weather"][0]["icon"].as<char*>();
335 struct timeval now = { .tv_sec = CurrentWeather.dt };
336 settimeofday(&now, NULL);
338 JsonArray hourly = root["hourly"];
339 for (byte i=0; i<MaxHourlyFC; i++) {
340 HourlyDT[i] = HourlyForecasts[i].dt = hourly[i]["dt"].as<int>();
341 HourlyTemp[i] = HourlyForecasts[i].temp = hourly[i]["temp"].as<float>();
342 HourlyFeelsLike[i] = HourlyForecasts[i].feels_like = hourly[i]["feels_like"].as<float>();
343 HourlyPressure[i] = HourlyForecasts[i].pressure = 0.75 * hourly[i]["pressure"].as<float>();
344 HourlyHumidity[i] = HourlyForecasts[i].humidity = hourly[i]["humidity"].as<float>();
345 HourlyClouds[i] = HourlyForecasts[i].clouds = hourly[i]["clouds"].as<float>();
346 HourlyWindSpeed[i] = HourlyForecasts[i].wind_speed = hourly[i]["wind_speed"].as<float>();
347 HourlyForecasts[i].wind_deg = hourly[i]["wind_deg"].as<float>();
348 HourlyRain[i] = HourlyForecasts[i].rain = hourly[i]["rain"]["1h"].as<float>();
349 HourlySnow[i] = HourlyForecasts[i].snow = hourly[i]["snow"]["1h"].as<float>();
350 HourlyPrecip[i] = HourlyRain[i] + HourlySnow[i];
351 HourlyForecasts[i].description = hourly[i]["weather"][0]["description"].as<char*>();
352 HourlyForecasts[i].icon = hourly[i]["weather"][0]["icon"].as<char*>();
355 JsonArray daily = root["daily"];
356 for (byte i=0; i<MaxDailyFC; i++) {
357 DailyDT[i] = DailyForecasts[i].dt = daily[i]["dt"].as<int>();
358 DailyTempMin[i] = DailyForecasts[i].temp_min = daily[i]["temp"]["min"].as<float>();
359 DailyFeelsLikeMin[i] = DailyForecasts[i].feels_like_min = daily[i]["feels_like"]["min"].as<float>();
360 DailyTempMax[i] = DailyForecasts[i].temp_max = daily[i]["temp"]["max"].as<float>();
361 DailyFeelsLikeMax[i] = DailyForecasts[i].feels_like_max = daily[i]["feels_like"]["max"].as<float>();
362 DailyPressure[i] = DailyForecasts[i].pressure = 0.75 * daily[i]["pressure"].as<float>();
363 DailyHumidity[i] = DailyForecasts[i].humidity = daily[i]["humidity"].as<float>();
364 DailyClouds[i] = DailyForecasts[i].clouds = daily[i]["clouds"].as<float>();
365 DailyWindSpeed[i] = DailyForecasts[i].wind_speed = daily[i]["wind_speed"].as<float>();
366 DailyForecasts[i].wind_deg = daily[i]["wind_deg"].as<float>();
367 DailyRain[i] = DailyForecasts[i].rain = daily[i]["rain"].as<float>();
368 DailySnow[i] = DailyForecasts[i].snow = daily[i]["snow"].as<float>();
369 DailyPrecip[i] = DailyRain[i] + DailySnow[i];
370 DailyForecasts[i].description = daily[i]["weather"][0]["description"].as<char*>();
371 DailyForecasts[i].icon = daily[i]["weather"][0]["icon"].as<char*>();
377 bool GetWeather(WiFiClient& client) {
378 client.stop(); // close connection before sending a new request
381 sprintf(uri,"/data/2.5/onecall?lat=%s&lon=%s&appid=%s&lang=%s&units=metric",latitude,longitude,api_key,language);
382 http.useHTTP10(true);
383 Serial.println("Connecting...");
384 http.begin(client, server, 80, uri);
385 int httpCode = http.GET();
386 if(httpCode == HTTP_CODE_OK) {
387 if (!DecodeWeather(http.getStream())) return false;
392 Serial.printf("connection failed, error: %s\n", http.errorToString(httpCode).c_str());
401 bool DecodeCurrentWeather(WiFiClient& json) {
402 DynamicJsonDocument doc(35 * 1024);
403 // Deserialize the JSON document
404 DeserializationError error = deserializeJson(doc, json);
405 // Test if parsing succeeds.
407 Serial.print(F("deserializeJson() failed: "));
408 Serial.println(error.c_str());
411 // convert it to a JsonObject
412 JsonObject root = doc.as<JsonObject>();
414 if (current_weather_pressure[0] && root.containsKey(current_weather_pressure)) {
415 CurrentWeather.pressure = root[current_weather_pressure].as<float>();
417 if (current_weather_temperature[0] && root.containsKey(current_weather_temperature)) {
418 CurrentWeather.temp = root[current_weather_temperature].as<float>();
420 if (current_weather_humidity[0] && root.containsKey(current_weather_humidity)) {
421 CurrentWeather.humidity = root[current_weather_humidity].as<float>();
423 if (current_weather_windspeed[0] && root.containsKey(current_weather_windspeed)) {
424 CurrentWeather.wind_speed = root[current_weather_windspeed].as<float>();
427 float Ro = (CurrentWeather.humidity/100) * 6.105 * pow(2.712828, 17.27 * CurrentWeather.temp/(237.7+CurrentWeather.temp));
428 CurrentWeather.feels_like = CurrentWeather.temp + 0.348*Ro - 0.70*CurrentWeather.wind_speed - 4.25;
429 if (current_weather_lux[0] && root.containsKey(current_weather_lux)) {
430 float wm2 = root[current_weather_lux].as<float>()/685;
431 CurrentWeather.feels_like = CurrentWeather.feels_like + 0.70*wm2/(CurrentWeather.wind_speed + 10);
433 Serial.print("Calculated feels like: ");
434 Serial.println(CurrentWeather.feels_like);
440 bool GetCurrentWeather(WiFiClient& client) {
441 if (current_weather_server) {
442 client.stop(); // close connection before sending a new request
444 http.useHTTP10(true);
445 Serial.println("Connecting...");
446 http.begin(client, current_weather_server, 80, current_weather_uri);
447 int httpCode = http.GET();
448 if(httpCode == HTTP_CODE_OK) {
449 if (!DecodeCurrentWeather(http.getStream())) return false;
454 Serial.printf("connection failed, error: %s\n", http.errorToString(httpCode).c_str());
464 void arrow(int x, int y, int asize, float aangle, int pwidth, int plength) {
465 float dx = (asize) * cos((aangle - 90) * PI / 180) + x; // calculate X position
466 float dy = (asize) * sin((aangle - 90) * PI / 180) + y; // calculate Y position
467 float x1 = 0; float y1 = plength;
468 float x2 = pwidth / 2; float y2 = pwidth / 2;
469 float x3 = -pwidth / 2; float y3 = pwidth / 2;
470 float angle = aangle * PI / 180 - 135;
471 float xx1 = x1 * cos(angle) - y1 * sin(angle) + dx;
472 float yy1 = y1 * cos(angle) + x1 * sin(angle) + dy;
473 float xx2 = x2 * cos(angle) - y2 * sin(angle) + dx;
474 float yy2 = y2 * cos(angle) + x2 * sin(angle) + dy;
475 float xx3 = x3 * cos(angle) - y3 * sin(angle) + dx;
476 float yy3 = y3 * cos(angle) + x3 * sin(angle) + dy;
477 display.fillTriangle(xx1, yy1, xx3, yy3, xx2, yy2, GxEPD_BLACK);
480 void drawString(int x, int y, String text, alignment align) {
482 display.setTextWrap(false);
483 w = u8g2Fonts.getUTF8Width(text.c_str());
484 h = u8g2Fonts.getFontAscent();
485 if (align == RIGHT) x = x - w;
486 if (align == CENTER) x = x - w / 2;
487 u8g2Fonts.setCursor(x, y + h / 2);
488 u8g2Fonts.print(text);
491 String WindDegToDirection(float winddirection) {
492 if (winddirection >= 337.5 || winddirection < 22.5) return "С";
493 if (winddirection >= 22.5 && winddirection < 67.5) return "СВ";
494 if (winddirection >= 67.5 && winddirection < 112.5) return "В";
495 if (winddirection >= 112.5 && winddirection < 157.5) return "ЮВ";
496 if (winddirection >= 157.5 && winddirection < 202.5) return "Ю";
497 if (winddirection >= 202.5 && winddirection < 247.5) return "ЮЗ";
498 if (winddirection >= 247.5 && winddirection < 292.5) return "З";
499 if (winddirection >= 292.5 && winddirection < 337.5) return "СЗ";
503 void DrawBlock(int x1, int x2, int y1, int y2) {
504 display.fillRect(x1+3, y1+3, x2-x1, y2-y1, GxEPD_BLACK);
505 display.fillRect(x1, y1, x2-x1, y2-y1, GxEPD_WHITE);
506 display.drawRect(x1, y1, x2-x1, y2-y1, GxEPD_BLACK);
509 void DisplayCurrent(int x1, int x2, int y1, int y2, WeatherRecord weather) {
511 DrawBlock(x1,x2,y1,y2);
519 drawString(x, y1+w*2/3, String(weather.temp,1) + "°", CENTER);
521 drawString(x, y1+w*3/2, "ощущается как", CENTER);
523 drawString(x, y1+w*2, String(weather.feels_like,1) + "°", CENTER);
525 drawString(x, y2-w/3, String(weather.humidity,0) + "% " + String(weather.pressure,0) + "мм", CENTER);
528 void DisplayWind(int x1, int x2, int y1, int y2, WeatherRecord weather) {
530 DrawBlock(x1,x2,y1,y2);
532 int r, dxo, dyo, dxi, dyi;
535 int r1 = (x2 - x1)/2;
536 int r2 = (y2 - y1)/2;
537 if (r1>r2) { r = r2; } else { r = r1; }
539 if (w>40) { w = 40; }
542 String speedstr = String(weather.wind_speed,1);
545 arrow (x, y, r-w, weather.wind_deg, w/2, w*7/8);
547 display.drawCircle(x, y, r, GxEPD_BLACK); // Draw compass circle
548 display.drawCircle(x, y, r + 1, GxEPD_BLACK); // Draw compass circle
549 display.drawCircle(x, y, r - w, GxEPD_BLACK); // Draw compass inner circle
550 display.drawCircle(x, y, r - w + 1, GxEPD_BLACK); // Draw compass inner circle
551 for (float a = 0; a < 360; a = a + 30) {
552 dxo = r * cos((a - 90) * PI / 180);
553 dyo = r * sin((a - 90) * PI / 180);
556 display.drawLine(dxo + x, dyo + y, dxi + x, dyi + y, GxEPD_BLACK);
560 drawString(x, y - r - w/2, "С", CENTER);
561 drawString(x, y + r + w/2, "Ю", CENTER);
562 drawString(x - r - w/2, y, "З", CENTER);
563 drawString(x + r + w/2, y, "В", CENTER);
564 drawString(x - (r + w/2)*10/14, y - (r + w/2)*10/14, "сз", CENTER);
565 drawString(x - (r + w/2)*10/14, y + (r + w/2)*10/14, "юз", CENTER);
566 drawString(x + (r + w/2)*10/14, y + (r + w/2)*10/14, "юв", CENTER);
567 drawString(x + (r + w/2)*10/14, y - (r + w/2)*10/14, "св", CENTER);
569 drawString(x, y-r/8, speedstr, CENTER);
571 drawString(x, y+r*3/8, "м/с", CENTER);
575 void drawsun(int x, int y, int scale, int linesize) {
576 display.fillRect(x - scale * 2, y, scale * 4, linesize, GxEPD_BLACK);
577 display.fillRect(x, y - scale * 2, linesize, scale * 4, GxEPD_BLACK);
578 display.drawLine(x - scale * 1.3, y - scale * 1.3, x + scale * 1.3, y + scale * 1.3, GxEPD_BLACK);
579 display.drawLine(x - scale * 1.3, y + scale * 1.3, x + scale * 1.3, y - scale * 1.3, GxEPD_BLACK);
581 display.drawLine(1 + x - scale * 1.3, y - scale * 1.3, 1 + x + scale * 1.3, y + scale * 1.3, GxEPD_BLACK);
582 display.drawLine(2 + x - scale * 1.3, y - scale * 1.3, 2 + x + scale * 1.3, y + scale * 1.3, GxEPD_BLACK);
583 display.drawLine(3 + x - scale * 1.3, y - scale * 1.3, 3 + x + scale * 1.3, y + scale * 1.3, GxEPD_BLACK);
584 display.drawLine(1 + x - scale * 1.3, y + scale * 1.3, 1 + x + scale * 1.3, y - scale * 1.3, GxEPD_BLACK);
585 display.drawLine(2 + x - scale * 1.3, y + scale * 1.3, 2 + x + scale * 1.3, y - scale * 1.3, GxEPD_BLACK);
586 display.drawLine(3 + x - scale * 1.3, y + scale * 1.3, 3 + x + scale * 1.3, y - scale * 1.3, GxEPD_BLACK);
588 display.fillCircle(x, y, scale * 1.3, GxEPD_WHITE);
589 display.fillCircle(x, y, scale, GxEPD_BLACK);
590 display.fillCircle(x, y, scale - linesize, GxEPD_WHITE);
593 void drawcloud(int x, int y, int scale, int linesize) {
595 display.fillCircle(x - scale * 3, y, scale, GxEPD_BLACK); // Left most circle
596 display.fillCircle(x + scale * 3, y, scale, GxEPD_BLACK); // Right most circle
597 display.fillCircle(x - scale, y - scale, scale * 1.4, GxEPD_BLACK); // left middle upper circle
598 display.fillCircle(x + scale * 1.5, y - scale * 1.3, scale * 1.75, GxEPD_BLACK); // Right middle upper circle
599 display.fillRect(x - scale * 3 - 1, y - scale, scale * 6, scale * 2 + 1, GxEPD_BLACK); // Upper and lower lines
601 display.fillCircle(x - scale * 3, y, scale - linesize, GxEPD_WHITE); // Clear left most circle
602 display.fillCircle(x + scale * 3, y, scale - linesize, GxEPD_WHITE); // Clear right most circle
603 display.fillCircle(x - scale, y - scale, scale * 1.4 - linesize, GxEPD_WHITE); // left middle upper circle
604 display.fillCircle(x + scale * 1.5, y - scale * 1.3, scale * 1.75 - linesize, GxEPD_WHITE); // Right middle upper circle
605 display.fillRect(x - scale * 3 + 2, y - scale + linesize - 1, scale * 5.9, scale * 2 - linesize * 2 + 2, GxEPD_WHITE); // Upper and lower lines
608 void drawraindrop(int x, int y, int scale) {
609 display.fillCircle(x, y, scale / 4, GxEPD_BLACK);
610 display.fillTriangle(x - scale / 4, y, x, y - scale, x + scale / 4, y , GxEPD_BLACK);
611 x = x + scale * 1.5; y = y + scale / 2;
612 display.fillCircle(x, y, scale / 4, GxEPD_BLACK);
613 display.fillTriangle(x - scale / 4, y, x, y - scale, x + scale / 4, y , GxEPD_BLACK);
616 void drawrain(int x, int y, int scale) {
617 for (int d = 0; d < 4; d++) {
618 drawraindrop(x + scale * (7.8 - d * 1.85) - scale * 5.2, y + scale * 2 - scale / 6, scale / 1.6);
622 void drawsnow(int x, int y, int scale) {
623 int dxo, dyo, dxi, dyi;
624 int delta = -scale/5;
625 for (int flakes = 0; flakes < 5; flakes++) {
626 for (int i = 0; i < 360; i = i + 60) {
627 dxo = 0.5 * scale * cos((i - 90) * 3.14 / 180); dxi = dxo * 0.1;
628 dyo = 0.5 * scale * sin((i - 90) * 3.14 / 180); dyi = dyo * 0.1;
629 display.drawLine(dxo + x + flakes * 1.5 * scale - scale * 3, dyo + y + delta + scale * 1.8, dxi + x + 0 + flakes * 1.5 * scale - scale * 3, dyi + y + delta + scale * 1.8, GxEPD_BLACK);
635 void drawtstorm(int x, int y, int scale) {
637 int delta = -scale/5;
638 for (int i = 0; i < 4; i++) {
640 display.drawLine(x - scale * 3 + scale * i * 1.5 + 0, y + delta + scale * 1.4, x - scale * 2.5 + scale * i * 1.5 + 0, y + delta + scale * 0.9, GxEPD_BLACK);
642 display.drawLine(x - scale * 3 + scale * i * 1.5 + 1, y + delta + scale * 1.4, x - scale * 2.5 + scale * i * 1.5 + 1, y + delta + scale * 0.9, GxEPD_BLACK);
643 display.drawLine(x - scale * 3 + scale * i * 1.5 + 2, y + delta + scale * 1.4, x - scale * 2.5 + scale * i * 1.5 + 2, y + delta + scale * 0.9, GxEPD_BLACK);
645 display.drawLine(x - scale * 3 + scale * i * 1.5, y + delta + scale * 1.4 + 0, x - scale * 2 + scale * i * 1.5 + 0, y + delta + scale * 1.4 + 0, GxEPD_BLACK);
647 display.drawLine(x - scale * 3 + scale * i * 1.5, y + delta + scale * 1.4 + 1, x - scale * 2 + scale * i * 1.5 + 0, y + delta + scale * 1.4 + 1, GxEPD_BLACK);
648 display.drawLine(x - scale * 3 + scale * i * 1.5, y + delta + scale * 1.4 + 2, x - scale * 2 + scale * i * 1.5 + 0, y + delta + scale * 1.4 + 2, GxEPD_BLACK);
650 display.drawLine(x - scale * 2.5 + scale * i * 1.4 + 0, y + delta + scale * 2.4, x - scale * 2 + scale * i * 1.5 + 0, y + delta + scale * 1.4, GxEPD_BLACK);
652 display.drawLine(x - scale * 2.5 + scale * i * 1.4 + 1, y + delta + scale * 2.4, x - scale * 2 + scale * i * 1.5 + 1, y + delta + scale * 1.4, GxEPD_BLACK);
653 display.drawLine(x - scale * 2.5 + scale * i * 1.4 + 2, y + delta + scale * 2.4, x - scale * 2 + scale * i * 1.5 + 2, y + delta + scale * 1.4, GxEPD_BLACK);
658 void drawfog(int x, int y, int scale, int linesize) {
663 for (int i = 0; i < 6; i++) {
664 display.fillRect(x - scale * 3, y + scale * 1.5, scale * 6, linesize, GxEPD_BLACK);
665 display.fillRect(x - scale * 3, y + scale * 2.0, scale * 6, linesize, GxEPD_BLACK);
666 display.fillRect(x - scale * 3, y + scale * 2.5, scale * 6, linesize, GxEPD_BLACK);
670 void Sunny(int x, int y, int scale) {
672 if (scale>15) linesize=3;
673 drawsun(x, y, scale*6/5, linesize);
676 void MostlySunny(int x, int y, int scale) {
677 int linesize = 1, offset = 5;
682 drawcloud(x, y + offset, scale, linesize);
683 drawsun(x - scale * 1.8, y - scale * 1.8 + offset, scale, linesize);
686 void MostlyCloudy(int x, int y, int scale) {
687 int linesize = 1, offset = 5;
692 drawsun(x - scale * 1.8, y - scale * 1.8 + offset, scale, linesize);
693 drawcloud(x, y + offset, scale, linesize);
696 void Cloudy(int x, int y, int scale) {
699 drawcloud(x, y+5, scale, linesize);
704 drawcloud(x + scale * 2.5, y - scale * 1.8, scale/1.6, linesize); // Cloud top right
705 drawcloud(x - scale * 2, y - scale * 2, scale/2, linesize); // Cloud top left
706 drawcloud(x, y, scale, linesize); // Main cloud
710 void Rain(int x, int y, int scale) {
715 drawcloud(x, y-scale/3, scale, linesize);
716 drawrain(x, y-scale/3, scale);
719 void ExpectRain(int x, int y, int scale) {
724 drawsun(x - scale * 1.8, y - scale * 1.6, scale, linesize);
725 drawcloud(x, y+scale/5, scale, linesize);
726 drawrain(x, y, scale);
729 void Tstorms(int x, int y, int scale) {
734 drawcloud(x, y, scale, linesize);
735 drawtstorm(x, y, scale);
738 void Snow(int x, int y, int scale) {
743 drawcloud(x, y, scale, linesize);
744 drawsnow(x, y, scale);
747 void Fog(int x, int y, int scale) {
752 drawcloud(x, y - 5, scale, linesize);
753 drawfog(x, y - 5, scale, linesize);
756 void Haze(int x, int y, int scale) {
761 drawsun(x, y - 5, scale*6/5, linesize);
762 drawfog(x, y - 5, scale, linesize);
765 void DisplayIcon(int x1, int x2, int y1, int y2, WeatherRecord weather) {
767 DrawBlock(x1,x2,y1,y2);
777 String icon = weather.icon;
778 String description = weather.description;
780 if (icon == "01d" || icon == "01n")
782 else if (icon == "02d" || icon == "02n")
783 MostlySunny(x, y, scale);
784 else if (icon == "03d" || icon == "03n")
785 MostlyCloudy(x, y, scale);
786 else if (icon == "04d" || icon == "04n")
788 else if (icon == "09d" || icon == "09n")
790 else if (icon == "10d" || icon == "10n")
791 ExpectRain(x, y, scale);
792 else if (icon == "11d" || icon == "11n")
793 Tstorms(x, y, scale);
794 else if (icon == "13d" || icon == "13n")
796 else if (icon == "50d")
798 else if (icon == "50n")
802 drawString(x, y2-fscale*2/3, description, CENTER);
806 void DisplayUpdate(int x1, int x2, int y1, int y2, WeatherRecord weather) {
808 DrawBlock(x1,x2,y1,y2);
813 int w = (x2 - x1)/16;
816 drawString(x, y1+w, "Прогноз от " + UnixTime(weather.dt + TimeZoneOffset), CENTER);
820 void DisplayForecast(int x1, int x2, int y1, int y2, WeatherRecord weather) {
822 DrawBlock(x1,x2,y1,y2);
831 if (fscale<6) fscale = 6;
833 String icon = weather.icon;
834 String temperature = String(weather.temp,1);
836 if (icon == "01d" || icon == "01n")
838 else if (icon == "02d" || icon == "02n")
839 MostlySunny(x, y, scale);
840 else if (icon == "03d" || icon == "03n")
841 MostlyCloudy(x, y, scale);
842 else if (icon == "04d" || icon == "04n")
844 else if (icon == "09d" || icon == "09n")
846 else if (icon == "10d" || icon == "10n")
847 ExpectRain(x, y, scale);
848 else if (icon == "11d" || icon == "11n")
849 Tstorms(x, y, scale);
850 else if (icon == "13d" || icon == "13n")
852 else if (icon == "50d")
854 else if (icon == "50n")
858 drawString(x, y1+fscale*2/3, UnixTimeOnly(weather.dt+ TimeZoneOffset), CENTER);
859 drawString(x, y2-fscale*2/3, temperature, CENTER);
863 void DisplayGraph(int x1, int x2, int y1, int y2, int TimeArray[], float DataArray[], int readings, boolean barchart_mode, String label) {
865 DrawBlock(x1,x2,y1,y2);
872 int scale = (y2 - y1)/10;
875 drawString((x1+x2)/2, y1+scale*2/3, label, CENTER);
877 int x_pos = x1 + 3 + scale*2;
880 int h = y2 - y1 - 6 - scale;
881 int w = x2 - x1 - 8 - scale*2;
885 for (int i = 1; i < readings; i++ ) {
886 if (DataArray[i] >= maxY) maxY = DataArray[i];
887 if (DataArray[i] <= minY) minY = DataArray[i];
889 maxY = round(maxY + 0.5);
893 last_y = y_pos - (constrain(DataArray[0], minY, maxY) - minY) / (maxY - minY) * h -1;
895 display.drawRect(x_pos, y_pos-h, w, h, GxEPD_BLACK);
897 for (int gx = 1; gx < readings; gx++) {
898 x_new = x_pos + gx * w / (readings - 1) - 1 ;
899 y_new = y_pos - (constrain(DataArray[gx], minY, maxY) - minY) / (maxY - minY) * h -1;
901 if (DataArray[gx]!=0) {
902 display.fillRect(x_new, y_new, (w / readings) - 1, y_pos - y_new, GxEPD_BLACK);
905 display.drawLine(last_x, last_y, x_new, y_new, GxEPD_BLACK);
911 #define y_minor_axis 5
912 #define number_of_dashes 20
913 SetNumFont(scale/1.3);
914 for (int spacing = 1; spacing < y_minor_axis; spacing++) {
915 for (int j = 0; j < number_of_dashes; j++) {
916 if (spacing < y_minor_axis) display.drawFastHLine((x_pos + 3 + j * w / number_of_dashes), y_pos - (h * spacing / y_minor_axis), w / (2 * number_of_dashes), GxEPD_BLACK);
918 drawString(x_pos - 1, y_pos - h * spacing / y_minor_axis, String((minY + (float)(maxY - minY) / y_minor_axis * spacing + 0.01), 1), RIGHT);
922 void DisplayWeather() {
923 Serial.println("Displaying...");
924 DisplayWind(20,235,20,225, CurrentWeather);
925 DisplayCurrent(240,515,20,225,CurrentWeather);
926 DisplayIcon(520,775,60,225,CurrentWeather);
927 DisplayUpdate(520,775,20,55,CurrentWeather);
929 for (int i=0; i<MaxHourlyFC; i+=3) {
930 DisplayForecast(x,x+90,235,305,HourlyForecasts[i]);
934 DisplayGraph(20,268,315,460,HourlyDT,HourlyTemp,MaxHourlyFC,false,"Температура");
935 DisplayGraph(273,522,315,460,HourlyDT,HourlyPressure,MaxHourlyFC,false,"Давление");
936 DisplayGraph(527,775,315,460,HourlyDT,HourlyPrecip,MaxHourlyFC,true,"Осадки");
943 WiFiManager wifiManager;
944 wifiManager.setTimeout(10);
945 if(!wifiManager.autoConnect()) {
947 Serial.println("failed to connect and hit timeout");
951 if (Serial.available() && Serial.read() == 'c') {
955 // put your main code here, to run repeatedly:
957 WiFiClient client; // wifi client object
958 for (byte i=1; i<=3; i++) {
959 if (GetWeather(client)) {
960 GetCurrentWeather(client);
965 int CurrentMin, CurrentSec;
967 getLocalTime(&timeinfo, 10000);
968 CurrentMin = timeinfo.tm_min;
969 CurrentSec = timeinfo.tm_sec;
970 long SleepTimer = (SleepDuration * 60 - ((CurrentMin % SleepDuration) * 60 + CurrentSec));
971 Serial.print("Going to long sleep for ");
972 Serial.print(SleepTimer);
973 Serial.println(" seconds now");
977 esp_sleep_enable_timer_wakeup((SleepTimer+5)*1000*1000);
978 esp_deep_sleep_start();
984 Serial.println("Going to short sleep for 120 seconds now ");
987 esp_sleep_enable_timer_wakeup(120*1000*1000);
988 esp_deep_sleep_start();