@@ -30,6 +30,7 @@ See more at https://blog.squix.org
30
30
31
31
#include < XPT2046_Touchscreen.h>
32
32
#include " TouchControllerWS.h"
33
+ #include " SunMoonCalc.h"
33
34
34
35
35
36
/* **
@@ -95,6 +96,7 @@ OpenWeatherMapCurrentData currentWeather;
95
96
OpenWeatherMapForecastData forecasts[MAX_FORECASTS];
96
97
simpleDSTadjust dstAdjusted (StartRule, EndRule);
97
98
Astronomy::MoonData moonData;
99
+ // SunMoonCalc::Moon moonData;
98
100
99
101
void updateData ();
100
102
void drawProgress (uint8_t percentage, String text);
@@ -122,8 +124,6 @@ int frameCount = 3;
122
124
int screenCount = 5 ;
123
125
long lastDownloadUpdate = millis();
124
126
125
- String moonAgeImage = " " ;
126
- uint8_t moonAge = 0 ;
127
127
uint16_t screen = 0 ;
128
128
long timerPress;
129
129
bool canBtnPress;
@@ -180,7 +180,6 @@ void setup() {
180
180
SPIFFS.format ();
181
181
}
182
182
drawProgress (100 ," Formatting done" );
183
- // SPIFFS.remove("/calibration.txt");
184
183
boolean isCalibrationAvailable = touchController.loadCalibration ();
185
184
if (!isCalibrationAvailable) {
186
185
Serial.println (" Calibration not available" );
@@ -269,18 +268,21 @@ void loop() {
269
268
270
269
// Update the internet based information and update screen
271
270
void updateData () {
271
+ time_t now;
272
272
273
273
gfx.fillBuffer (MINI_BLACK);
274
274
gfx.setFont (ArialRoundedMTBold_14);
275
275
276
276
drawProgress (10 , " Updating time..." );
277
277
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 );
281
281
}
282
+ Serial.println ();
283
+ Serial.printf (" Current time: %d\n " , now);
282
284
// 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 ;
284
286
Serial.printf (" Time difference for DST: %d\n " , dstOffset);
285
287
286
288
drawProgress (50 , " Updating conditions..." );
@@ -303,12 +305,18 @@ void updateData() {
303
305
304
306
drawProgress (80 , " Updating astronomy..." );
305
307
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);
310
310
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
+
312
320
delay (1000 );
313
321
}
314
322
@@ -350,22 +358,20 @@ void drawTime() {
350
358
if (IS_STYLE_12HR) {
351
359
int hour = (timeinfo->tm_hour +11 )%12 +1 ; // take care of noon and midnight
352
360
sprintf (time_str, " %2d:%02d:%02d\n " ,hour, timeinfo->tm_min , timeinfo->tm_sec );
353
- gfx.drawString (120 , 20 , time_str);
354
361
} else {
355
362
sprintf (time_str, " %02d:%02d:%02d\n " ,timeinfo->tm_hour , timeinfo->tm_min , timeinfo->tm_sec );
356
- gfx.drawString (120 , 20 , time_str);
357
363
}
364
+ gfx.drawString (120 , 20 , time_str);
358
365
359
366
gfx.setTextAlignment (TEXT_ALIGN_LEFT);
360
367
gfx.setFont (ArialMT_Plain_10);
361
368
gfx.setColor (MINI_BLUE);
362
369
if (IS_STYLE_12HR) {
363
370
sprintf (time_str, " %s\n %s" , dstAbbrev, timeinfo->tm_hour >=12 ?" PM" :" AM" );
364
- gfx.drawString (195 , 27 , time_str);
365
371
} else {
366
372
sprintf (time_str, " %s" , dstAbbrev);
367
- gfx.drawString (195 , 27 , time_str); // Known bug: Cuts off 4th character of timezone abbreviation
368
373
}
374
+ gfx.drawString (195 , 27 , time_str); // Known bug: Cuts off 4th character of timezone abbreviation
369
375
}
370
376
371
377
// draws current weather information
@@ -433,7 +439,7 @@ void drawAstronomy() {
433
439
gfx.setFont (MoonPhases_Regular_36);
434
440
gfx.setColor (MINI_WHITE);
435
441
gfx.setTextAlignment (TEXT_ALIGN_CENTER);
436
- gfx.drawString (120 , 275 , moonAgeImage );
442
+ gfx.drawString (120 , 275 , String (( char ) ( 97 + (moonData. illumination * 26 ))) );
437
443
438
444
gfx.setColor (MINI_WHITE);
439
445
gfx.setFont (ArialRoundedMTBold_14);
@@ -456,10 +462,12 @@ void drawAstronomy() {
456
462
gfx.setColor (MINI_YELLOW);
457
463
gfx.drawString (235 , 250 , SUN_MOON_TEXT[3 ]);
458
464
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" );
460
468
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 ] + " :" );
463
471
464
472
}
465
473
@@ -485,13 +493,6 @@ void drawCurrentWeatherDetail() {
485
493
drawLabelValue (4 , " Pressure:" , String (currentWeather.pressure ) + " hPa" );
486
494
drawLabelValue (5 , " Clouds:" , String (currentWeather.clouds ) + " %" );
487
495
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);*/
495
496
}
496
497
497
498
void drawLabelValue (uint8_t line, String label, String value) {
0 commit comments