1
1
#include " main.hpp"
2
2
3
3
HomieBootMode bootMode = HomieBootMode::UNDEFINED;
4
+ bool otaInitialDrawDone = false ;
4
5
uint8_t otaState = 0 ;
5
6
uint8_t otaProgress = 0 ;
6
- bool canUpdate = false ;
7
- int lastUpdate = 0 ;
7
+
8
+ uint32_t currTempRotateTime = 0 ;
9
+
10
+ bool initialUpdate = false ;
11
+ bool currentUpdate = false ;
12
+ bool forecastUpdate = false ;
13
+ bool astronomyUpdate = false ;
14
+
8
15
time_t dstOffset = 0 ;
9
16
uint8_t moonAge = 0 ;
10
17
String moonAgeImage = " " ;
18
+ uint32_t lastTemperatureSent = 0 ;
11
19
20
+ HomieNode temperatureNode (" temperature" , " temperature" );
12
21
HomieSetting<const char *> owApiKey (" ow_api_key" , " Open Weather API Key" );
13
22
23
+ void initialize () {
24
+ currentUpdate = true ;
25
+ forecastUpdate = true ;
26
+ astronomyUpdate = true ;
27
+ sensors.begin ();
28
+ temperatureNode.setProperty (" unit" ).send (" c" );
29
+ }
30
+
31
+ void temperatureLoop () {
32
+ if (millis () - lastTemperatureSent >= TEMPERATURE_UPDATE * 1000 || lastTemperatureSent == 0 ) {
33
+ sensors.requestTemperatures ();
34
+ float temp = sensors.getTempCByIndex (0 );
35
+ Homie.getLogger () << F (" Temperature: " ) << temp << endl;
36
+ temperatureNode.setProperty (" degrees" ).send (String (temp));
37
+ lastTemperatureSent = millis ();
38
+ }
39
+ }
40
+
14
41
void setup () {
15
42
Serial.begin (115200 );
16
43
17
44
pinMode (TFT_LED, OUTPUT);
18
45
digitalWrite (TFT_LED, HIGH);
46
+ pinMode (TEMP_PIN, INPUT);
19
47
20
48
gfx.init ();
21
49
gfx.fillBuffer (MINI_BLACK);
22
50
gfx.commit ();
23
51
52
+ updateCurrentTicker.attach (5 * 60 * 1000 , []() {if (WiFi.status () == WL_CONNECTED) currentUpdate = true ;});
53
+ updateForecastTicker.attach (20 * 60 * 1000 , []() {if (WiFi.status () == WL_CONNECTED) forecastUpdate = true ;});
54
+ updateAstronomyTicker.attach (60 * 60 * 1000 , []() {if (WiFi.status () == WL_CONNECTED) astronomyUpdate = true ;});
55
+
24
56
carousel.setFrames (frames, frameCount);
25
57
carousel.disableAllIndicators ();
26
58
carousel.setTargetFPS (3 );
@@ -35,6 +67,8 @@ void setup() {
35
67
Homie_setFirmware (" weather-station" , " 0.0.1" );
36
68
Homie_setBrand (" IoT" );
37
69
Homie.onEvent (onHomieEvent);
70
+ Homie.setSetupFunction (initialize);
71
+ Homie.setLoopFunction (temperatureLoop);
38
72
Homie.setup ();
39
73
}
40
74
@@ -52,12 +86,6 @@ void onHomieEvent(const HomieEvent &event) {
52
86
case HomieEventType::OTA_FAILED:
53
87
otaState = 3 ;
54
88
break ;
55
- case HomieEventType::WIFI_CONNECTED:
56
- canUpdate = true ;
57
- break ;
58
- case HomieEventType::WIFI_DISCONNECTED:
59
- canUpdate = false ;
60
- break ;
61
89
case HomieEventType::OTA_PROGRESS:
62
90
otaProgress = ((float )event.sizeDone / (float )event.sizeTotal ) * 100 ;
63
91
break ;
@@ -70,8 +98,9 @@ void loop() {
70
98
// Handle OTA display first to ensure it is displaued before restarts
71
99
switch (otaState) {
72
100
case 1 : // started
73
- if (otaProgress < 4 || otaProgress % 10 == 0 || (otaProgress > 96 ) ) {
101
+ if (!otaInitialDrawDone || otaProgress % 10 == 0 ) {
74
102
drawProgress (otaProgress, F (" Updating..." ));
103
+ otaInitialDrawDone = true ;
75
104
}
76
105
return ;
77
106
case 2 : // success
@@ -92,38 +121,31 @@ void loop() {
92
121
switch (bootMode) {
93
122
case HomieBootMode::NORMAL:
94
123
// Only update data if WiFi connected and interval passed
95
- if (canUpdate &&
96
- (lastUpdate == 0 || millis () - lastUpdate > UPDATE_INTERVAL * 1000 )) {
124
+ if (currentUpdate || forecastUpdate || astronomyUpdate) {
97
125
updateData ();
98
- lastUpdate = millis () ;
126
+ return ;
99
127
}
100
128
101
129
// To avoid showing unix time zero dates/temps wait for initial update to
102
130
// run
103
- if (lastUpdate != 0 ) {
131
+ if (initialUpdate ) {
104
132
gfx.fillBuffer (MINI_BLACK);
105
133
drawTime ();
106
134
drawWifiQuality ();
107
135
carousel.update ();
108
136
drawCurrentWeather ();
109
137
drawAstronomy ();
110
- drawNextUpdate ();
111
138
gfx.commit ();
112
139
} else {
113
- drawProgress (millis () / 1000 , F (" Initializing..." ));
140
+ // throttle drawing while system is getting started
141
+ if ((millis () / 1000 ) % 5 == 0 )
142
+ drawProgress (millis () / 1000 , F (" Initializing..." ));
114
143
}
115
144
default :
116
145
break ;
117
146
}
118
147
}
119
148
120
- void drawNextUpdate () {
121
- float percentAlong =
122
- ((float )(millis () - lastUpdate) / (float )(UPDATE_INTERVAL * 1000 ));
123
- int progressLength = SCREEN_WIDTH * percentAlong;
124
- gfx.drawHorizontalLine (0 , 317 , progressLength);
125
- }
126
-
127
149
void drawWifiQuality () {
128
150
int8_t quality = getWifiQuality ();
129
151
gfx.setColor (MINI_WHITE);
@@ -318,22 +340,32 @@ void updateData() {
318
340
// calculate for time calculation how much the dst class adds.
319
341
dstOffset = UTC_OFFSET * 3600 + dstAdjusted.time (nullptr ) - time (nullptr );
320
342
321
- drawProgress (50 , F (" Updating conditions..." ));
322
- currentWeatherClient.updateCurrentById (
323
- ¤tWeather, owApiKey.get (), OPEN_WEATHER_MAP_LOCATION_ID);
324
-
325
- drawProgress (70 , F (" Updating forecasts..." ));
326
- forecastClient.updateForecastsById (forecasts, owApiKey.get (),
327
- OPEN_WEATHER_MAP_LOCATION_ID,
328
- MAX_FORECASTS);
329
-
330
- drawProgress (80 , F (" Updating astronomy..." ));
331
- moonData = astronomy.calculateMoonData (time (nullptr ));
332
- float lunarMonth = 29.53 ;
333
- moonAge = moonData.phase <= 4
334
- ? lunarMonth * moonData.illumination / 2
335
- : lunarMonth - moonData.illumination * lunarMonth / 2 ;
336
- moonAgeImage = String ((char )(65 + ((uint8_t )((26 * moonAge / 30 ) % 26 ))));
343
+ if (currentUpdate) {
344
+ drawProgress (50 , F (" Updating conditions..." ));
345
+ currentWeatherClient.updateCurrentById (
346
+ ¤tWeather, owApiKey.get (), OPEN_WEATHER_MAP_LOCATION_ID);
347
+ currentUpdate = false ;
348
+ }
349
+
350
+ if (forecastUpdate) {
351
+ drawProgress (70 , F (" Updating forecasts..." ));
352
+ forecastClient.updateForecastsById (forecasts, owApiKey.get (),
353
+ OPEN_WEATHER_MAP_LOCATION_ID,
354
+ MAX_FORECASTS);
355
+ forecastUpdate = false ;
356
+ }
357
+
358
+ if (astronomyUpdate) {
359
+ drawProgress (80 , F (" Updating astronomy..." ));
360
+ moonData = astronomy.calculateMoonData (time (nullptr ));
361
+ float lunarMonth = 29.53 ;
362
+ moonAge = moonData.phase <= 4
363
+ ? lunarMonth * moonData.illumination / 2
364
+ : lunarMonth - moonData.illumination * lunarMonth / 2 ;
365
+ moonAgeImage = String ((char )(65 + ((uint8_t )((26 * moonAge / 30 ) % 26 ))));
366
+ astronomyUpdate = false ;
367
+ }
368
+ initialUpdate = true ;
337
369
}
338
370
339
371
String getTime (time_t *timestamp) {
0 commit comments