-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWifiWeatherGuy.ino
278 lines (242 loc) · 5.87 KB
/
WifiWeatherGuy.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <ArduinoJson.h>
#include <SPI.h>
#include <LittleFS.h>
#include <Adafruit_GFX.h>
#include <TFT_eSPI.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <Timezone.h>
#include "Switch.h"
#include "Configuration.h"
#include "state.h"
#include "display.h"
#include "dbg.h"
#include "providers.h"
#if !defined(TFT_LED)
#define TFT_LED D2
#endif
#if !defined(SWITCH)
#define SWITCH D3
#endif
TFT_eSPI tft;
MDNSResponder mdns;
ESP8266WebServer server(80);
ESP8266HTTPUpdateServer httpUpdater;
DNSServer dnsServer;
uint32_t display_on;
uint8_t fade;
bool connected, debug;
config cfg;
Timezone *tz;
struct Conditions conditions;
struct Forecast forecasts[4];
struct Statistics stats;
#if !defined(PROVIDER)
#define PROVIDER OpenWeatherMap
#endif
PROVIDER provider;
Switch swtch(500);
void IRAM_ATTR swtch_handler() { swtch.on(); }
const char *config_file = "/config.json";
static int screen = 0;
static SimpleTimer timers;
static void update_display() {
if (cfg.dimmable || fade > cfg.dim) {
switch (screen) {
case 0:
display_weather(conditions);
break;
case 1:
display_astronomy(conditions);
break;
case 2:
case 3:
case 4:
case 5:
display_forecast(forecasts[screen - 2]);
break;
case 6:
display_about(stats);
break;
}
}
}
// timer callbacks
static void update_conditions() {
DBG(println(F("Updating conditions...")));
if (provider.fetch_conditions(conditions)) {
update_display();
stats.last_fetch_conditions = millis();
}
}
static void update_forecasts() {
DBG(println(F("Updating forecasts...")));
if (provider.fetch_forecasts(&forecasts[0], sizeof(forecasts)/sizeof(forecasts[0])))
stats.last_fetch_forecasts = millis();
}
static void next_fade() {
analogWrite(TFT_LED, --fade);
if (fade == cfg.dim && screen > 0) {
screen = 0;
update_display();
}
}
static void turn_off() {
if (cfg.dimmable) {
timers.setTimer(25, next_fade, cfg.bright - cfg.dim);
} else {
tft.fillScreen(TFT_BLACK);
fade = cfg.dim;
}
}
void setup() {
Serial.begin(115200);
tft.init();
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(0, 0);
#if defined(FONT)
tft.setTextFont(FONT);
#endif
pinMode(SWITCH, INPUT_PULLUP);
debug = digitalRead(SWITCH) == LOW;
bool result = LittleFS.begin();
if (!result) {
ERR(print(F("LittleFS!")));
return;
}
if (!cfg.read_file(config_file)) {
ERR(print(F("config!")));
return;
}
tz = new Timezone(cfg.summer, cfg.winter);
fade = cfg.bright;
analogWrite(TFT_LED, fade);
tft.fillScreen(TFT_BLACK);
tft.setRotation(cfg.rotate);
tft.println(F("Weather Guy (c)2018-24"));
tft.print(F("ssid: "));
tft.println(cfg.ssid);
tft.print(F("password: "));
tft.println(cfg.password);
tft.print(F("key: "));
tft.println(cfg.key);
tft.print(F("station: "));
if (cfg.nearest)
tft.println(F("nearest"));
else
tft.println(cfg.station);
tft.print(F("hostname: "));
tft.println(cfg.hostname);
tft.print(F("condition...: "));
tft.println(cfg.conditions_interval);
tft.print(F("forecast...: "));
tft.println(cfg.forecasts_interval);
tft.print(F("display: "));
tft.println(cfg.on_time);
tft.print(F("metric: "));
tft.println(cfg.metric);
if (cfg.dimmable) {
tft.print(F("bright: "));
tft.println(cfg.bright);
tft.print(F("dim: "));
tft.println(cfg.dim);
} else
tft.println(F("not dimmable"));
if (debug)
tft.println(F("DEBUG"));
WiFi.mode(WIFI_STA);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.hostname(cfg.hostname);
if (*cfg.ssid) {
WiFi.setAutoReconnect(true);
WiFi.begin(cfg.ssid, cfg.password);
const char busy[] = "|/-\\";
int16_t y = tft.getCursorY();
for (int i = 0; i < 60 && WiFi.status() != WL_CONNECTED; i++) {
delay(500);
char c = busy[i % 4];
tft.print(c);
tft.setCursor(0, y);
}
connected = WiFi.status() == WL_CONNECTED;
}
server.on("/config", HTTP_POST, []() {
if (server.hasArg("plain")) {
String body = server.arg("plain");
File f = LittleFS.open(config_file, "w");
f.print(body);
f.close();
server.send(200);
WiFi.setAutoConnect(false);
ESP.restart();
} else
server.send(400, "text/plain", "No body!");
});
server.serveStatic("/", LittleFS, "/index.html");
server.serveStatic("/config", LittleFS, config_file);
server.serveStatic("/js/transparency.min.js", LittleFS, "/transparency.min.js");
server.serveStatic("/info.png", LittleFS, "/info.png");
httpUpdater.setup(&server);
server.begin();
if (mdns.begin(cfg.hostname, WiFi.localIP())) {
DBG(println(F("mDNS started")));
mdns.addService("http", "tcp", 80);
} else
ERR(println(F("Error starting mDNS")));
if (!connected) {
WiFi.mode(WIFI_AP);
WiFi.softAP(cfg.hostname);
tft.println(F("Connect to SSID"));
tft.println(cfg.hostname);
tft.println(F("to configure WiFi"));
dnsServer.start(53, "*", WiFi.softAPIP());
} else {
DBG(println());
DBG(print(F("Connected to ")));
DBG(println(cfg.ssid));
DBG(println(WiFi.localIP()));
tft.println();
tft.print(F("http://"));
tft.print(WiFi.localIP());
tft.println('/');
provider.begin();
stats.last_fetch_conditions = -cfg.conditions_interval;
stats.last_fetch_forecasts = -cfg.forecasts_interval;
}
attachInterrupt(SWITCH, swtch_handler, FALLING);
timers.setInterval(cfg.conditions_interval, update_conditions);
timers.setInterval(cfg.forecasts_interval, update_forecasts);
timers.setTimeout(cfg.on_time, turn_off);
update_conditions();
update_forecasts();
}
void loop() {
mdns.update();
server.handleClient();
if (!connected) {
dnsServer.processNextRequest();
return;
}
if (swtch) {
if (fade == cfg.dim) {
fade = cfg.bright;
if (cfg.dimmable)
analogWrite(TFT_LED, fade);
else
update_display();
timers.setTimeout(cfg.on_time, turn_off);
} else {
if (screen >= 6)
screen = 0;
else
screen++;
update_display();
}
}
timers.run();
}