Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit 8c9cfcc

Browse files
committed
Add temperature sensor and throttle initial setup steps
1 parent deb89e1 commit 8c9cfcc

File tree

4 files changed

+84
-38
lines changed

4 files changed

+84
-38
lines changed

platformio.ini

+2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ lib_deps =
1717
https://github.com/mchestr/async-mqtt-client
1818
https://github.com/mchestr/homie-esp8266.git
1919
https://github.com/squix78/json-streaming-parser
20+
DallasTemperature
2021
ESP8266 Weather Station
22+
OneWire
2123
Mini Grafx
2224
simpleDSTadjust
2325
build_flags =

src/Settings.h

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define IS_METRIC true
88
#define IS_12H true
99
#define UPDATE_INTERVAL 300
10+
#define TEMPERATURE_UPDATE 10
1011
#define UTC_OFFSET -8
1112
struct dstRule startRule = {"PDT", Last, Sun, Mar, 2, 3600};
1213
struct dstRule endRule = {"PST", Last, Sun, Oct, 2, 0};

src/main.cpp

+70-38
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,58 @@
11
#include "main.hpp"
22

33
HomieBootMode bootMode = HomieBootMode::UNDEFINED;
4+
bool otaInitialDrawDone = false;
45
uint8_t otaState = 0;
56
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+
815
time_t dstOffset = 0;
916
uint8_t moonAge = 0;
1017
String moonAgeImage = "";
18+
uint32_t lastTemperatureSent = 0;
1119

20+
HomieNode temperatureNode("temperature", "temperature");
1221
HomieSetting<const char*> owApiKey("ow_api_key", "Open Weather API Key");
1322

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+
1441
void setup() {
1542
Serial.begin(115200);
1643

1744
pinMode(TFT_LED, OUTPUT);
1845
digitalWrite(TFT_LED, HIGH);
46+
pinMode(TEMP_PIN, INPUT);
1947

2048
gfx.init();
2149
gfx.fillBuffer(MINI_BLACK);
2250
gfx.commit();
2351

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+
2456
carousel.setFrames(frames, frameCount);
2557
carousel.disableAllIndicators();
2658
carousel.setTargetFPS(3);
@@ -35,6 +67,8 @@ void setup() {
3567
Homie_setFirmware("weather-station", "0.0.1");
3668
Homie_setBrand("IoT");
3769
Homie.onEvent(onHomieEvent);
70+
Homie.setSetupFunction(initialize);
71+
Homie.setLoopFunction(temperatureLoop);
3872
Homie.setup();
3973
}
4074

@@ -52,12 +86,6 @@ void onHomieEvent(const HomieEvent &event) {
5286
case HomieEventType::OTA_FAILED:
5387
otaState = 3;
5488
break;
55-
case HomieEventType::WIFI_CONNECTED:
56-
canUpdate = true;
57-
break;
58-
case HomieEventType::WIFI_DISCONNECTED:
59-
canUpdate = false;
60-
break;
6189
case HomieEventType::OTA_PROGRESS:
6290
otaProgress = ((float)event.sizeDone / (float)event.sizeTotal) * 100;
6391
break;
@@ -70,8 +98,9 @@ void loop() {
7098
// Handle OTA display first to ensure it is displaued before restarts
7199
switch (otaState) {
72100
case 1: // started
73-
if (otaProgress < 4 || otaProgress % 10 == 0 || (otaProgress > 96)) {
101+
if (!otaInitialDrawDone || otaProgress % 10 == 0) {
74102
drawProgress(otaProgress, F("Updating..."));
103+
otaInitialDrawDone = true;
75104
}
76105
return;
77106
case 2: // success
@@ -92,38 +121,31 @@ void loop() {
92121
switch (bootMode) {
93122
case HomieBootMode::NORMAL:
94123
// 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) {
97125
updateData();
98-
lastUpdate = millis();
126+
return;
99127
}
100128

101129
// To avoid showing unix time zero dates/temps wait for initial update to
102130
// run
103-
if (lastUpdate != 0) {
131+
if (initialUpdate) {
104132
gfx.fillBuffer(MINI_BLACK);
105133
drawTime();
106134
drawWifiQuality();
107135
carousel.update();
108136
drawCurrentWeather();
109137
drawAstronomy();
110-
drawNextUpdate();
111138
gfx.commit();
112139
} 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..."));
114143
}
115144
default:
116145
break;
117146
}
118147
}
119148

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-
127149
void drawWifiQuality() {
128150
int8_t quality = getWifiQuality();
129151
gfx.setColor(MINI_WHITE);
@@ -318,22 +340,32 @@ void updateData() {
318340
// calculate for time calculation how much the dst class adds.
319341
dstOffset = UTC_OFFSET * 3600 + dstAdjusted.time(nullptr) - time(nullptr);
320342

321-
drawProgress(50, F("Updating conditions..."));
322-
currentWeatherClient.updateCurrentById(
323-
&currentWeather, 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+
&currentWeather, 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;
337369
}
338370

339371
String getTime(time_t *timestamp) {

src/main.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
#include <ILI9341_SPI.h>
55
#include <MiniGrafx.h>
66
#include <SPI.h>
7+
#include <Ticker.h>
78

89
#include <Astronomy.h>
910
#include <OpenWeatherMapCurrent.h>
1011
#include <OpenWeatherMapForecast.h>
1112
#include <simpleDSTadjust.h>
1213

14+
#include <DallasTemperature.h>
15+
#include <OneWire.h>
16+
1317
#include <Homie.h>
1418
#include "ArialRounded.h"
1519
#include "MoonPhases.h"
@@ -22,6 +26,7 @@
2226
#define TFT_DC D2
2327
#define TFT_CS D1
2428
#define TFT_LED D8
29+
#define TEMP_PIN D3
2530

2631
#define NTP_SERVERS \
2732
"0.ch.pool.ntp.org", "1.ch.pool.ntp.org", "2.ch.pool.ntp.org"
@@ -46,6 +51,12 @@ OpenWeatherMapForecast forecastClient;
4651
Astronomy astronomy;
4752
Astronomy::MoonData moonData;
4853

54+
OneWire oneWire(TEMP_PIN);
55+
DallasTemperature sensors(&oneWire);
56+
Ticker updateCurrentTicker;
57+
Ticker updateForecastTicker;
58+
Ticker updateAstronomyTicker;
59+
4960
const char *getMeteoconIconFromProgmem(String iconText);
5061
int8_t getWifiQuality();
5162
String getTime(time_t *timestamp);

0 commit comments

Comments
 (0)