GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — vzero/src/vzero.ino at master · andig/vzero

Expand file tree

/

vzero.ino

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
319 lines (274 loc) · 7.25 KB
/

vzero.ino

Copy path

File metadata and controls

executable file
·
319 lines (274 loc) · 7.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* VZero - Zero Config Volkszaehler Controller
*
* @author Andreas Goetz <[email protected]>
*/
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#include <SPIFFS.h>
#include <ESPmDNS.h>
#endif
#include <FS.h>
#include "config.h"
#include "webserver.h"
#include "plugins/Plugin.h"
#ifdef OTA_SERVER
#include <ArduinoOTA.h>
#endif
#ifdef CAPTIVE_PORTAL
#include <DNSServer.h>
const byte DNS_PORT = 53;
DNSServer dnsServer;
#endif
enum operation_t {
OPERATION_NORMAL = 0, // deep sleep forbidden
OPERATION_SLEEP = 1 // deep sleep allowed
};
/**
* Get operation mode
*
* Return NORMAL if:
* - deep sleep disabled
* - not waking up from deep sleep
* - not running as access point
*/
operation_t getOperationMode()
{
#ifndef DEEP_SLEEP
return OPERATION_NORMAL;
#endif
if (getResetReason(0) != REASON_DEEP_SLEEP_AWAKE)
return OPERATION_NORMAL;
if ((WiFi.getMode() & WIFI_STA) == 0)
return OPERATION_NORMAL;
return OPERATION_SLEEP;
}
/**
* (Re)connect WiFi - give ESP 10 seconds to connect to station
*/
wl_status_t wifiConnect() {
DEBUG_MSG("wifi", "waiting for connection");
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < WIFI_CONNECT_TIMEOUT) {
DEBUG_PLAIN(".");
delay(100);
}
DEBUG_PLAIN("\n");
return WiFi.status();
}
/**
* Get max deep sleep window in ms
*/
uint32_t getDeepSleepDurationMs()
{
#ifndef DEEP_SLEEP
return 0;
#else
// don't sleep if access point
if ((WiFi.getMode() & WIFI_STA) == 0)
return 0;
// don't sleep during initial startup
if (g_resetInfo->reason != 5 && millis() < STARTUP_ONLINE_DURATION_MS)
return 0;
// don't sleep if client connected
if (millis() - g_lastAccessTime < WIFI_CLIENT_TIMEOUT)
return 0;
// check if deep sleep possible
uint32_t maxSleep = -1; // max uint32_t
Plugin::each([&maxSleep](Plugin* plugin) {
uint32_t sleep = plugin->getMaxSleepDuration();
// DEBUG_MSG(CORE, "sleep window is %u for %s\n", sleep, Plugin::get(pluginIndex)->getName().c_str());
if (sleep < maxSleep)
maxSleep = sleep;
});
// valid sleep window found?
if (maxSleep == -1 || maxSleep < MIN_SLEEP_DURATION_MS)
return 0;
return maxSleep - SLEEP_SAFETY_MARGIN;
#endif
}
/**
* Start ota server
*/
void start_ota() {
#ifdef OTA_SERVER
if (MDNS.begin(net_hostname.c_str())) {
DEBUG_MSG(CORE, "mDNS responder started at %s.local\n", net_hostname.c_str());
}
else {
DEBUG_MSG(CORE, "error setting up mDNS responder\n");
}
// start OTA server
DEBUG_MSG(CORE, "starting OTA server\n");
ArduinoOTA.setHostname(net_hostname.c_str());
ArduinoOTA.onStart([]() {
DEBUG_MSG(CORE, "OTA start\n");
});
ArduinoOTA.onEnd([]() {
DEBUG_MSG(CORE, "OTA end\n");
// save config after OTA Update
if (SPIFFS.begin()) {
File configFile = SPIFFS.open(F("/config.json"), "r");
if (!configFile) {
DEBUG_MSG(CORE, "config wiped by OTA - saving\n");
saveConfig();
}
else {
configFile.close();
}
SPIFFS.end();
}
});
ArduinoOTA.onError([](ota_error_t error) {
DEBUG_MSG(CORE, "OTA error [%u]\n", error);
});
ArduinoOTA.begin();
#endif
}
#ifndef ESP32
// use the internal hardware buffer
static void _u0_putc(char c) {
while(((U0S >> USTXC) & 0x7F) == 0x7F);
U0F = c;
}
#endif
/**
* Setup
*/
void setup()
{
// hardware serial
Serial.begin(115200);
#ifndef ESP32
ets_install_putc1((void *) &_u0_putc);
system_set_os_print(1);
g_resetInfo = ESP.getResetInfoPtr();
#endif
DEBUG_PLAIN("\n");
DEBUG_MSG(CORE, "Booting...\n");
DEBUG_MSG(CORE, "Cause %d: %s\n", getResetReason(0), getResetReasonStr(0));
DEBUG_MSG(CORE, "Chip ID: %05X\n", getChipId());
#ifndef ESP32
// set hostname
net_hostname += "-" + String(getChipId(), HEX);
WiFi.hostname(net_hostname);
DEBUG_MSG(CORE, "Hostname: %s\n", net_hostname.c_str());
#endif
// initialize file system
if (!SPIFFS.begin()) {
DEBUG_MSG(CORE, "failed mounting file system\n");
return;
}
// check WiFi connection
if (WiFi.getMode() != WIFI_STA) {
WiFi.mode(WIFI_STA);
delay(10);
}
// configuration changed - set new credentials
if (loadConfig() && g_ssid != "" && (String(WiFi.SSID()) != g_ssid || String(WiFi.psk()) != g_pass)) {
DEBUG_MSG("wifi", "connect: %s\n", WiFi.SSID().c_str());
WiFi.begin(g_ssid.c_str(), g_pass.c_str());
}
else {
// reconnect to sdk-configured station
DEBUG_MSG("wifi", "reconnect: %s\n", WiFi.SSID().c_str());
WiFi.begin();
}
// Check connection
if (wifiConnect() == WL_CONNECTED) {
DEBUG_MSG("wifi", "IP address: %d.%d.%d.%d\n", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
}
else {
// go into AP mode
DEBUG_MSG("wifi", "could not connect to WiFi - going into AP mode\n");
WiFi.mode(WIFI_AP); // WIFI_AP_STA
delay(10);
WiFi.softAP(ap_default_ssid);
DEBUG_MSG("wifi", "IP address: %d.%d.%d.%d\n", WiFi.softAPIP()[0], WiFi.softAPIP()[1], WiFi.softAPIP()[2], WiFi.softAPIP()[3]);
#ifdef CAPTIVE_PORTAL
// start DNS server for any domain
DEBUG_MSG("wifi", "starting captive DNS server\n");
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
#endif
}
// start OTA - both in AP as in STA mode
if (getOperationMode() == OPERATION_NORMAL) {
start_ota();
}
// start plugins (before web server)
startPlugins();
// start web server if not in battery mode
if (getOperationMode() == OPERATION_NORMAL) {
webserver_start();
}
}
uint32_t _minFreeHeap = 0;
uint32_t _freeHeap = 0;
long _tsMillis = 0;
long _loopMillis = 0;
/**
* Loop
*/
void loop()
{
// loop duration
_tsMillis = millis();
#ifdef CAPTIVE_PORTAL
dnsServer.processNextRequest();
#endif
#ifdef OTA_SERVER
if (getOperationMode() == OPERATION_NORMAL) {
ArduinoOTA.handle();
}
#endif
// call plugin's loop method
Plugin::each([](Plugin* plugin) {
plugin->loop();
yield();
});
// check if deep sleep possible
uint32_t sleep = getDeepSleepDurationMs();
if (sleep > 0) {
DEBUG_MSG(CORE, "going to deep sleep for %ums\n", sleep);
ESP.deepSleep(sleep * 1000);
}
// trigger restart?
if (g_restartTime > 0 && millis() >= g_restartTime) {
DEBUG_MSG(CORE, "restarting...\n");
g_restartTime = 0;
ESP.restart();
}
// check WLAN if not AP
if ((WiFi.getMode() & WIFI_AP) == 0) {
if (WiFi.status() != WL_CONNECTED) {
DEBUG_MSG(CORE, "wifi connection lost\n");
WiFi.reconnect();
if (wifiConnect() != WL_CONNECTED) {
DEBUG_MSG(CORE, "could not reconnect wifi - restarting\n");
ESP.restart();
}
}
}
// loop duration without debug
_loopMillis = millis() - _tsMillis;
if (g_minFreeHeap != _minFreeHeap || ESP.getFreeHeap() != _freeHeap) {
_freeHeap = ESP.getFreeHeap();
if (_freeHeap < g_minFreeHeap)
g_minFreeHeap = _freeHeap;
_minFreeHeap = g_minFreeHeap;
#ifdef ESP8266
umm_info(NULL, 0);
DEBUG_MSG(CORE, "heap min: %d (%d blk, %d tot)\n", g_minFreeHeap, ummHeapInfo.maxFreeContiguousBlocks * 8, _freeHeap);
#endif
#ifdef ESP32
DEBUG_MSG(CORE, "heap min: %d (%d tot)\n", g_minFreeHeap, _freeHeap);
#endif
// loop duration
DEBUG_MSG(CORE, "loop %ums\n", _loopMillis);
}
delay(1000);
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.