-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pool_display.ino
205 lines (172 loc) · 4.62 KB
/
Pool_display.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// configs
#include <WifiConfig.h>
#include "Config.h"
#include "Icons.h"
// libraries
#include <WiFi.h>
#include "time.h" // NTP
#include "Button2.h" // debounce switch
#include "Wire.h" // Display
#include "SSD1306.h" // Display
#include <HTTPClient.h> // OTA
#include <AsyncTCP.h> // OTA
#include <ESPAsyncWebServer.h> // OTA
#include <AsyncElegantOTA.h> // OTA
// virtual objects
HTTPClient http;
AsyncWebServer server(80);
// physical objects
Button2 leftButton;
Button2 rightButton;
SSD1306 display(0x3c, SDA, SCL);
// state variables
int currentLocation = 4; // State, which temperature is currently shown on the display
unsigned long lastUpdate = 0; // counter: refresh displayed values every x ms
void setup()
{
// pinMode(BATTERY_PIN, INPUT); // TODO this might fix the wrong voltage on the voltage divider
display.init();
display.flipScreenVertically();
WiFi.mode(WIFI_STA);
WiFi.config(LOCAL_IP, GATEWAY, SUBNET, PRIMARY_DNS, SECONDARY_DNS); // use static IP --> no DHCP --> connection is established faster
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// printBatteryState();
printConnectionState();
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); // NTP init
printTime();
leftButton.begin(LEFT_BUTTON_PIN);
leftButton.setClickHandler(leftButtonClick);
leftButton.setLongClickTime(400);
rightButton.begin(RIGHT_BUTTON_PIN);
rightButton.setClickHandler(rightButtonClick);
rightButton.setLongClickTime(400);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/plain", MQTT_CLIENT_NAME); });
AsyncElegantOTA.begin(&server);
server.begin();
}
void loop()
{
leftButton.loop();
rightButton.loop();
delay(1); // Sometimes this prevents the ESP from heating up
if (millis() >= lastUpdate + 5000)
{
if (currentLocation == numberOfStations)
{
printTime();
}
else
{
printTemperature(currentLocation);
}
lastUpdate = millis();
}
}
void rightButtonClick(Button2 &btn)
{
currentLocation = (currentLocation + 1) % 4;
printTemperature(currentLocation);
}
void leftButtonClick(Button2 &btn)
{
setFilterState(!getFilterState());
}
void printBatteryState()
{
int average = 0;
for (int i = 0; i < 500; i++) // 500 iterations because ESP32 ADCs are bad but also quit fast
{
average = average + analogRead(BATTERY_PIN);
}
average = average / 500;
if (average <= BATTERY_SHUTDOWN_VALUE)
{
display.clear();
display.drawXbm(0, 0, 128, 64, icon_deadBattery);
display.display();
delay(1500); // go to deep sleep instead
}
else if (average <= BATTERY_WARNING_VALUE)
{
display.clear();
display.drawXbm(0, 0, 128, 64, icon_lowBattery);
display.display();
delay(1000);
}
}
void printConnectionState()
{
display.clear();
display.drawXbm(0, 0, 128, 64, icon_wifi);
display.display();
while (WiFi.status() != WL_CONNECTED)
{
delay(10);
}
}
void printTime()
{
struct tm timeInfo;
if (!getLocalTime(&timeInfo))
{
return;
}
char timeHour[6];
strftime(timeHour, 6, "%H:%M", &timeInfo); // 5 digits + 1 end of line ; %H:%M = 24h:minutes
display.clear();
display.setFont(Open_Sans_Hebrew_10);
display.drawString(0, 0, "Zeit");
display.setFont(Open_Sans_Hebrew_35);
display.drawString(15, 12, timeHour);
display.display();
}
void printTemperature(int stationNumber)
{
http.begin(stationApiUrl[stationNumber]);
int httpCode = http.GET();
if (httpCode > 0)
{
String payload = http.getString();
char charBuf[5];
payload.toCharArray(charBuf, 5);
display.clear();
display.setFont(Open_Sans_Hebrew_10);
display.drawString(0, 0, stationLabel[stationNumber]);
display.drawString(110, 0, getFilterState() ? "ON" : "OFF");
display.setFont(Open_Sans_Hebrew_35);
display.drawString(5, 12, charBuf);
display.drawString(85, 12, "°C");
display.display();
}
http.end();
}
bool getFilterState()
{
http.begin(filterGetApiUrl);
int httpCode = http.GET();
if (httpCode > 0)
{
String payload = http.getString();
return payload.equals("ON");
}
http.end();
}
void setFilterState(bool state)
{
if (http.begin(filterSetApiUrl))
{
http.addHeader("Content-Type", "text/plain");
http.addHeader("Accept", "application/json");
int httpCode = http.POST(state ? "ON" : "OFF");
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
String payload = http.getString(); // TODO remove?
}
}
http.end();
}
printTemperature(currentLocation % numberOfStations);
}