1
- // #define SH1106_OLED /* uncomment to compile for SH1106 instead of SSD1306 */
1
+ #define SH1106_OLED /* uncomment to compile for SH1106 instead of SSD1306 */
2
2
3
3
#include < SD.h>
4
4
#include < FS.h>
18
18
#include < SSD1306.h> /* In same library as SH1106 */
19
19
#endif
20
20
21
- #define USE_WS_BRIDGE true /* true = use a dsmr websocket bridge - false = use a dsmr smartmeter */
21
+ #define USE_WS_BRIDGE true /* true = connect to a dsmr websocket bridge - false = connect to a dsmr smartmeter */
22
22
23
23
const char * WS_BRIDGE_HOST = " 192.168.0.177" ; /* bridge adress */
24
24
const uint16_t WS_BRIDGE_PORT = 80 ; /* bridge port */
25
25
const char * WS_BRIDGE_URL = " /raw" ; /* bridge url */
26
26
27
- #define SAVE_TIME_MIN (1 ) /* data save interval in minutes */
27
+ #define SAVE_TIME_MIN (1 ) /* data save interval in minutes */
28
28
29
29
/* settings for smartMeter */
30
30
#define RXD_PIN (36 )
@@ -69,8 +69,8 @@ struct {
69
69
uint32_t gas;
70
70
} current;
71
71
72
- time_t bootTime;
73
- bool oledFound{false };
72
+ time_t bootTime;
73
+ bool oledFound{false };
74
74
75
75
const char * HEADER_MODIFIED_SINCE = " If-Modified-Since" ;
76
76
@@ -83,6 +83,24 @@ void connectToWebSocketBridge() {
83
83
ws_bridge.begin (WS_BRIDGE_HOST, WS_BRIDGE_PORT, WS_BRIDGE_URL);
84
84
}
85
85
86
+ void updateFileHandlers (const tm& now) {
87
+ static char path[16 ];
88
+ snprintf (path, sizeof (path), " /%i/%i/%i.log" , now.tm_year + 1900 , now.tm_mon + 1 , now.tm_mday );
89
+
90
+ static AsyncCallbackWebHandler* currentLogFileHandler;
91
+ http_server.removeHandler (currentLogFileHandler);
92
+ currentLogFileHandler = &http_server.on (path, HTTP_GET, [] (AsyncWebServerRequest * request) {
93
+ if (!SD.exists (path)) return request->send (404 );
94
+ AsyncWebServerResponse *response = request->beginResponse (SD, path);
95
+ response->addHeader (" Cache-Control" , " no-store, max-age=0" );
96
+ request->send (response);
97
+ });
98
+
99
+ static AsyncStaticWebHandler* staticFilesHandler;
100
+ http_server.removeHandler (staticFilesHandler);
101
+ staticFilesHandler = &http_server.serveStatic (" /" , SD, " /" ).setCacheControl (" public, max-age=604800, immutable" );
102
+ }
103
+
86
104
void setup () {
87
105
Serial.begin (115200 );
88
106
Serial.printf (" \n\n smartMeterLogger-esp32\n\n connecting to %s...\n " , WIFI_NETWORK);
@@ -128,15 +146,13 @@ void setup() {
128
146
/* sync the clock with ntp */
129
147
configTzTime (TIMEZONE, NTP_POOL);
130
148
131
- struct tm timeinfo {
149
+ tm now {
132
150
0
133
151
};
134
152
135
- while (!getLocalTime (&timeinfo , 0 ))
153
+ while (!getLocalTime (&now , 0 ))
136
154
delay (10 );
137
155
138
- time (&bootTime);
139
-
140
156
/* websocket setup */
141
157
ws_server_raw.onEvent (ws_server_onEvent);
142
158
http_server.addHandler (&ws_server_raw);
@@ -145,6 +161,7 @@ void setup() {
145
161
http_server.addHandler (&ws_server_events);
146
162
147
163
/* webserver setup */
164
+ time (&bootTime);
148
165
static char modifiedDate[30 ];
149
166
strftime (modifiedDate, sizeof (modifiedDate), " %a, %d %b %Y %X GMT" , gmtime (&bootTime));
150
167
@@ -201,7 +218,8 @@ void setup() {
201
218
request->send(response);
202
219
});
203
220
*/
204
- http_server.serveStatic (" /" , SD, " /" ).setCacheControl (" no-store, no-cache, must-revalidate, max-age=0" );
221
+
222
+ updateFileHandlers (now);
205
223
206
224
http_server.onNotFound ([](AsyncWebServerRequest * request) {
207
225
request->send (404 );
@@ -274,11 +292,17 @@ void loop() {
274
292
ws_server_raw.cleanupClients ();
275
293
ws_server_events.cleanupClients ();
276
294
277
- static struct tm now;
295
+ static tm now;
278
296
getLocalTime (&now);
279
297
if ((59 == now.tm_sec ) && !(now.tm_min % SAVE_TIME_MIN) && (numberOfSamples > 2 ))
280
298
saveAverage (now);
281
299
300
+ static uint8_t currentMonthDay = now.tm_mday ;
301
+ if (currentMonthDay != now.tm_mday ) {
302
+ updateFileHandlers (now);
303
+ currentMonthDay = now.tm_mday ;
304
+ }
305
+
282
306
if (USE_WS_BRIDGE) {
283
307
ws_bridge.loop ();
284
308
}
0 commit comments