Skip to content

Commit 2133809

Browse files
committed
Calibrate NTP time and tune astronomy data
1 parent 850f8fd commit 2133809

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

esp8266-weather-station-color.ino

+28-27
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ See more at https://blog.squix.org
3030

3131
#include <XPT2046_Touchscreen.h>
3232
#include "TouchControllerWS.h"
33+
#include "SunMoonCalc.h"
3334

3435

3536
/***
@@ -95,6 +96,7 @@ OpenWeatherMapCurrentData currentWeather;
9596
OpenWeatherMapForecastData forecasts[MAX_FORECASTS];
9697
simpleDSTadjust dstAdjusted(StartRule, EndRule);
9798
Astronomy::MoonData moonData;
99+
//SunMoonCalc::Moon moonData;
98100

99101
void updateData();
100102
void drawProgress(uint8_t percentage, String text);
@@ -122,8 +124,6 @@ int frameCount = 3;
122124
int screenCount = 5;
123125
long lastDownloadUpdate = millis();
124126

125-
String moonAgeImage = "";
126-
uint8_t moonAge = 0;
127127
uint16_t screen = 0;
128128
long timerPress;
129129
bool canBtnPress;
@@ -180,7 +180,6 @@ void setup() {
180180
SPIFFS.format();
181181
}
182182
drawProgress(100,"Formatting done");
183-
//SPIFFS.remove("/calibration.txt");
184183
boolean isCalibrationAvailable = touchController.loadCalibration();
185184
if (!isCalibrationAvailable) {
186185
Serial.println("Calibration not available");
@@ -269,18 +268,21 @@ void loop() {
269268

270269
// Update the internet based information and update screen
271270
void updateData() {
271+
time_t now;
272272

273273
gfx.fillBuffer(MINI_BLACK);
274274
gfx.setFont(ArialRoundedMTBold_14);
275275

276276
drawProgress(10, "Updating time...");
277277
configTime(UTC_OFFSET * 3600, 0, NTP_SERVERS);
278-
while(!time(nullptr)) {
279-
Serial.print("#");
280-
delay(100);
278+
while((now = time(nullptr)) < NTP_MIN_VALID_EPOCH) {
279+
Serial.print(".");
280+
delay(300);
281281
}
282+
Serial.println();
283+
Serial.printf("Current time: %d\n", now);
282284
// calculate for time calculation how much the dst class adds.
283-
dstOffset = UTC_OFFSET * 3600 + dstAdjusted.time(nullptr) - time(nullptr);
285+
dstOffset = UTC_OFFSET * 3600 + dstAdjusted.time(nullptr) - now;
284286
Serial.printf("Time difference for DST: %d\n", dstOffset);
285287

286288
drawProgress(50, "Updating conditions...");
@@ -303,12 +305,18 @@ void updateData() {
303305

304306
drawProgress(80, "Updating astronomy...");
305307
Astronomy *astronomy = new Astronomy();
306-
moonData = astronomy->calculateMoonData(time(nullptr));
307-
float lunarMonth = 29.53;
308-
moonAge = moonData.phase <= 4 ? lunarMonth * moonData.illumination / 2 : lunarMonth - moonData.illumination * lunarMonth / 2;
309-
moonAgeImage = String((char) (65 + ((uint8_t) ((26 * moonAge / 30) % 26))));
308+
moonData = astronomy->calculateMoonData(now);
309+
moonData.phase = astronomy->calculateMoonPhase(now);
310310
delete astronomy;
311-
astronomy = nullptr;
311+
astronomy = nullptr;
312+
//https://github.com/ThingPulse/esp8266-weather-station/issues/144 prevents using this
313+
// // 'now' has to be UTC, lat/lng in degrees not raadians
314+
// SunMoonCalc *smCalc = new SunMoonCalc(now - dstOffset, currentWeather.lat, currentWeather.lon);
315+
// moonData = smCalc->calculateSunAndMoonData().moon;
316+
// delete smCalc;
317+
// smCalc = nullptr;
318+
Serial.printf("Free mem: %d\n", ESP.getFreeHeap());
319+
312320
delay(1000);
313321
}
314322

@@ -350,22 +358,20 @@ void drawTime() {
350358
if (IS_STYLE_12HR) {
351359
int hour = (timeinfo->tm_hour+11)%12+1; // take care of noon and midnight
352360
sprintf(time_str, "%2d:%02d:%02d\n",hour, timeinfo->tm_min, timeinfo->tm_sec);
353-
gfx.drawString(120, 20, time_str);
354361
} else {
355362
sprintf(time_str, "%02d:%02d:%02d\n",timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
356-
gfx.drawString(120, 20, time_str);
357363
}
364+
gfx.drawString(120, 20, time_str);
358365

359366
gfx.setTextAlignment(TEXT_ALIGN_LEFT);
360367
gfx.setFont(ArialMT_Plain_10);
361368
gfx.setColor(MINI_BLUE);
362369
if (IS_STYLE_12HR) {
363370
sprintf(time_str, "%s\n%s", dstAbbrev, timeinfo->tm_hour>=12?"PM":"AM");
364-
gfx.drawString(195, 27, time_str);
365371
} else {
366372
sprintf(time_str, "%s", dstAbbrev);
367-
gfx.drawString(195, 27, time_str); // Known bug: Cuts off 4th character of timezone abbreviation
368373
}
374+
gfx.drawString(195, 27, time_str); // Known bug: Cuts off 4th character of timezone abbreviation
369375
}
370376

371377
// draws current weather information
@@ -433,7 +439,7 @@ void drawAstronomy() {
433439
gfx.setFont(MoonPhases_Regular_36);
434440
gfx.setColor(MINI_WHITE);
435441
gfx.setTextAlignment(TEXT_ALIGN_CENTER);
436-
gfx.drawString(120, 275, moonAgeImage);
442+
gfx.drawString(120, 275, String((char) (97 + (moonData.illumination * 26))));
437443

438444
gfx.setColor(MINI_WHITE);
439445
gfx.setFont(ArialRoundedMTBold_14);
@@ -456,10 +462,12 @@ void drawAstronomy() {
456462
gfx.setColor(MINI_YELLOW);
457463
gfx.drawString(235, 250, SUN_MOON_TEXT[3]);
458464
gfx.setColor(MINI_WHITE);
459-
gfx.drawString(235, 276, String(moonAge) + "d");
465+
float lunarMonth = 29.53;
466+
// approximate moon age
467+
gfx.drawString(235, 276, String(moonData.phase <= 4 ? lunarMonth * moonData.illumination / 2.0 : lunarMonth - moonData.illumination * lunarMonth / 2.0, 1) + "d");
460468
gfx.drawString(235, 291, String(moonData.illumination * 100, 0) + "%");
461-
gfx.drawString(200, 276, SUN_MOON_TEXT[4] + ":");
462-
gfx.drawString(200, 291, SUN_MOON_TEXT[5] + ":");
469+
gfx.drawString(190, 276, SUN_MOON_TEXT[4] + ":");
470+
gfx.drawString(190, 291, SUN_MOON_TEXT[5] + ":");
463471

464472
}
465473

@@ -485,13 +493,6 @@ void drawCurrentWeatherDetail() {
485493
drawLabelValue(4, "Pressure:", String(currentWeather.pressure) + "hPa");
486494
drawLabelValue(5, "Clouds:", String(currentWeather.clouds) + "%");
487495
drawLabelValue(6, "Visibility:", String(currentWeather.visibility) + "m");
488-
489-
490-
/*gfx.setTextAlignment(TEXT_ALIGN_LEFT);
491-
gfx.setColor(MINI_YELLOW);
492-
gfx.drawString(15, 185, "Description: ");
493-
gfx.setColor(MINI_WHITE);
494-
gfx.drawStringMaxWidth(15, 200, 240 - 2 * 15, forecasts[0].forecastText);*/
495496
}
496497

497498
void drawLabelValue(uint8_t line, String label, String value) {

moonphases.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Created by http://oleddisplay.squix.ch/ Consider a donation
22
// In case of problems make sure that you are using the font file with the correct version!
3+
4+
// Font face https://www.dafont.com/moon-phases.font
35
const char MoonPhases_Regular_36[] PROGMEM = {
46
0x24, // Width: 36
57
0x25, // Height: 37
@@ -424,4 +426,3 @@ const char MoonPhases_Regular_36[] PROGMEM = {
424426
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0xFF,0x3F,0x00,0xFE,0xFF,0xFF,0x3F,0x00,0xFE,0xFF,0xFF,0x3F,0x00,0xFE,0xFF,0xFF,0x3F,0x00,0x00,0x60,0x00,0x07,0x00,0x00,0x38,0x00,0x0E,0x00,0x00,0x1C,0x00,0x0E,0x00,0x00,0x1C,0x00,0x1C,0x00,0x00,0x0E,0x00,0x1C,0x00,0x00,0x0E,0x00,0x1C,0x00,0x00,0x0E,0x00,0x1C,0x00,0x00,0x0E,0x00,0x1E,0x00,0x00,0x1E,0x00,0x0F,0x00,0x00,0x7C,0xC0,0x0F,0x00,0x00,0xFC,0xFF,0x07,0x00,0x00,0xF8,0xFF,0x03,0x00,0x00,0xF0,0xFF,0x01,0x00,0x00,0xC0,0x3F, // 254
425427
0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0xFE,0x07,0x00,0x00,0x00,0xFC,0x1F,0x00,0x00,0x38,0xE0,0xFF,0x00,0x00,0x38,0x00,0xFF,0x03,0x00,0x38,0x00,0xFC,0x1F,0x00,0x00,0x00,0xE0,0x3F,0x00,0x00,0x00,0x80,0x3F,0x00,0x00,0x00,0x80,0x3F,0x00,0x00,0x00,0xF0,0x0F,0x00,0x38,0x00,0xFC,0x01,0x00,0x38,0x80,0x7F,0x00,0x00,0x38,0xE0,0x1F,0x00,0x00,0x00,0xF8,0x03,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0x06 // 255
426428
};
427-

settings.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ See more at http://blog.squix.ch
2525
#define WIFI_PASS "yourpassw0rd"
2626
#define WIFI_HOSTNAME "ThingPulse-weather-station-color"
2727

28-
const int UPDATE_INTERVAL_SECS = 15 * 60; // Update every 10 minutes
29-
const int SLEEP_INTERVAL_SECS = 0; // Going to Sleep after idle times, set 0 for dont sleep
28+
const int UPDATE_INTERVAL_SECS = 10 * 60; // Update every 10 minutes
29+
const int SLEEP_INTERVAL_SECS = 0; // Going to sleep after idle times, set 0 for insomnia
3030

3131

3232
// OpenWeatherMap Settings
@@ -74,11 +74,12 @@ const boolean IS_METRIC = true;
7474
// Change for 12 Hour/ 24 hour style clock
7575
bool IS_STYLE_12HR = false;
7676

77-
// change for different ntp (time servers)
77+
// change for different NTP (time servers)
7878
#define NTP_SERVERS "0.ch.pool.ntp.org", "1.ch.pool.ntp.org", "2.ch.pool.ntp.org"
7979
// #define NTP_SERVERS "us.pool.ntp.org", "time.nist.gov", "pool.ntp.org"
8080

81-
81+
// August 1st, 2018
82+
#define NTP_MIN_VALID_EPOCH 1533081600
8283

8384
// Pins for the ILI9341
8485
#define TFT_DC D2

0 commit comments

Comments
 (0)