Browse Source

date time printing on lorarx and auto reset

Thomas Buck 2 months ago
parent
commit
3777cef70d
8 changed files with 90 additions and 43 deletions
  1. 1
    1
      compile_commands.json
  2. 6
    0
      include/config.h
  3. 10
    0
      include/ui.h
  4. 2
    0
      platformio.ini
  5. 9
    0
      src/influx.cpp
  6. 10
    1
      src/lora.cpp
  7. 5
    0
      src/main.cpp
  8. 47
    41
      src/ui.cpp

+ 1
- 1
compile_commands.json View File

@@ -1 +1 @@
1
-.pio/build/loratx/compile_commands.json
1
+.pio/build/lorarx/compile_commands.json

+ 6
- 0
include/config.h View File

@@ -57,6 +57,12 @@
57 57
 #define LED_ERROR_BLINK_INTERVAL 100
58 58
 #define MQTT_RECONNECT_INTERVAL (5 * 1000)
59 59
 
60
+#define NTP_SERVER "pool.ntp.org"
61
+// TODO auto-detect?!
62
+//#warning hard-coded timezone and daylight savings offset
63
+#define gmtOffset_sec (60 * 60)
64
+#define daylightOffset_sec (60 * 60)
65
+
60 66
 #if defined(SENSOR_LOCATION_LIVINGROOM)
61 67
 #define SENSOR_LOCATION "livingroom"
62 68
 #define SENSOR_ID SENSOR_LOCATION

+ 10
- 0
include/ui.h View File

@@ -14,6 +14,14 @@
14 14
 #ifndef __UI_H__
15 15
 #define __UI_H__
16 16
 
17
+#ifdef FEATURE_NTP
18
+#include <time.h>
19
+String time_to_date_str(struct tm timeinfo);
20
+String time_to_time_str(struct tm timeinfo);
21
+#endif // FEATURE_NTP
22
+
23
+#ifdef FEATURE_UI
24
+
17 25
 enum bathroom_light_states {
18 26
     BATH_LIGHT_OFF,
19 27
     BATH_LIGHT_NONE,
@@ -52,4 +60,6 @@ void ui_run(void);
52 60
 
53 61
 void ui_progress(enum ui_state state);
54 62
 
63
+#endif // FEATURE_UI
64
+
55 65
 #endif // __UI_H__

+ 2
- 0
platformio.ini View File

@@ -28,6 +28,7 @@ build_flags =
28 28
   -DUSER_SETUP_LOADED=1
29 29
   -include include/User_Setup.h
30 30
   -DDISABLE_ALL_LIBRARY_WARNINGS
31
+  -DFEATURE_NTP
31 32
 lib_ldf_mode = deep
32 33
 lib_deps =
33 34
     https://github.com/knolleary/pubsubclient.git#2d228f2f862a95846c65a8518c79f48dfc8f188c
@@ -82,6 +83,7 @@ build_flags =
82 83
   -DFEATURE_LORA
83 84
   -DENABLE_INFLUXDB_LOGGING
84 85
   -DUSE_INFLUXDB_LIB
86
+  -DFEATURE_NTP
85 87
 lib_deps =
86 88
     ESP8266 Influxdb
87 89
     https://github.com/knolleary/pubsubclient.git#2d228f2f862a95846c65a8518c79f48dfc8f188c

+ 9
- 0
src/influx.cpp View File

@@ -18,6 +18,7 @@
18 18
 #include "sensors.h"
19 19
 #include "relais.h"
20 20
 #include "moisture.h"
21
+#include "ui.h"
21 22
 #include "influx.h"
22 23
 
23 24
 #ifdef ENABLE_INFLUXDB_LOGGING
@@ -129,6 +130,14 @@ void writeSensorDatum(String measurement, String sensor, String placement, Strin
129 130
     debug.print(" ");
130 131
     debug.printf("%.2lf\n", value);
131 132
     writeMeasurement(ms);
133
+
134
+#ifdef FEATURE_NTP
135
+    struct tm timeinfo;
136
+    if (getLocalTime(&timeinfo)) {
137
+        debug.print(time_to_time_str(timeinfo) + " ");
138
+    }
139
+#endif // FEATURE_NTP
140
+
132 141
     debug.println(F("Done!"));
133 142
 }
134 143
 

+ 10
- 1
src/lora.cpp View File

@@ -82,7 +82,8 @@
82 82
 
83 83
 static unsigned long last_bat_time = 0;
84 84
 static bool use_lora = true;
85
-static unsigned long last_tx = 0, tx_time = 0, minimum_pause = 0;
85
+static unsigned long last_tx = 0, last_rx = 0;
86
+static unsigned long tx_time = 0, minimum_pause = 0;
86 87
 static volatile bool rx_flag = false;
87 88
 
88 89
 #ifdef FEATURE_SML
@@ -334,12 +335,20 @@ void lora_run(void) {
334 335
     }
335 336
 #endif // DEEP_SLEEP_TIMEOUT_MS && DEEP_SLEEP_DURATION_S
336 337
 
338
+#ifndef FEATURE_SML
339
+    if (time >= (6UL * 60UL * 60UL * 1000UL) // running for at least 6h
340
+        && ((time - last_rx) >= (30UL * 1000UL))) { // and last lora rx at least 30s ago
341
+        heltec_deep_sleep(10); // attempt reset to avoid lorarx hanging
342
+    }
343
+#endif // ! FEATURE_SML
344
+
337 345
     if (!use_lora) {
338 346
         return;
339 347
     }
340 348
 
341 349
     if (rx_flag) {
342 350
         rx_flag = false;
351
+        last_rx = time;
343 352
 
344 353
         bool success = true;
345 354
         uint8_t data[sizeof(struct lora_sml_msg)];

+ 5
- 0
src/main.cpp View File

@@ -224,6 +224,11 @@ void setup() {
224 224
 
225 225
 #endif // ARCH
226 226
 
227
+#ifdef FEATURE_NTP
228
+    // get time via NTP
229
+    configTime(gmtOffset_sec, daylightOffset_sec, NTP_SERVER);
230
+#endif
231
+
227 232
     debug.println(F("Seeding"));
228 233
     randomSeed(micros());
229 234
 

+ 47
- 41
src/ui.cpp View File

@@ -20,7 +20,6 @@
20 20
 
21 21
 #include <Arduino.h>
22 22
 #include <WiFi.h>
23
-#include <time.h>
24 23
 
25 24
 #include "config.h"
26 25
 #include "DebugLog.h"
@@ -72,7 +71,6 @@
72 71
 #define FULL_BRIGHT_MS (1000 * 30)
73 72
 #define NO_BRIGHT_MS (1000 * 2)
74 73
 
75
-#define NTP_SERVER "pool.ntp.org"
76 74
 #define STANDBY_REDRAW_MS 500
77 75
 
78 76
 #define CALIB_1_X 42
@@ -80,11 +78,6 @@
80 78
 #define CALIB_2_X LCD_WIDTH - 1 - CALIB_1_X
81 79
 #define CALIB_2_Y LCD_HEIGHT - 1 - CALIB_1_Y
82 80
 
83
-// TODO auto-detect?!
84
-#warning hard-coded timezone and daylight savings offset
85
-#define gmtOffset_sec (60 * 60)
86
-#define daylightOffset_sec (60 * 60)
87
-
88 81
 #if (LCD_MIN_BRIGHTNESS <= STANDBY_BRIGHTNESS)
89 82
 #error STANDBY_BRIGHTNESS needs to be bigger than LCD_MIN_BRIGHTNESS
90 83
 #endif
@@ -259,38 +252,12 @@ static void draw_standby(void) {
259 252
     tft.drawString(ESP_PLATFORM_NAME " " NAME_OF_FEATURE " V" ESP_ENV_VERSION, LCD_WIDTH / 2, 0, 2);
260 253
     tft.drawString("by xythobuz.de", LCD_WIDTH / 2, 16, 2);
261 254
 
255
+    String date = "?";
256
+    String time = "?";
262 257
     struct tm timeinfo;
263
-    String date, time;
264
-    String weekday[7] = { "So.", "Mo.", "Di.", "Mi.", "Do.", "Fr.", "Sa." };
265
-    if(getLocalTime(&timeinfo)) {
266
-        date += weekday[timeinfo.tm_wday % 7];
267
-        date += " ";
268
-        if (timeinfo.tm_mday < 10) {
269
-            date += "0";
270
-        }
271
-        date += String(timeinfo.tm_mday);
272
-        date += ".";
273
-        if ((timeinfo.tm_mon + 1) < 10) {
274
-            date += "0";
275
-        }
276
-        date += String(timeinfo.tm_mon + 1);
277
-        date += ".";
278
-        date += String(timeinfo.tm_year + 1900);
279
-
280
-        if (timeinfo.tm_hour < 10) {
281
-            time += "0";
282
-        }
283
-        time += String(timeinfo.tm_hour);
284
-        time += ":";
285
-        if (timeinfo.tm_min < 10) {
286
-            time += "0";
287
-        }
288
-        time += String(timeinfo.tm_min);
289
-        time += ":";
290
-        if (timeinfo.tm_sec < 10) {
291
-            time += "0";
292
-        }
293
-        time += String(timeinfo.tm_sec);
258
+    if (getLocalTime(&timeinfo)) {
259
+        date = time_to_date_str(timeinfo);
260
+        time = time_to_time_str(timeinfo);
294 261
     }
295 262
 
296 263
     tft.setTextDatum(MC_DATUM); // middle center
@@ -474,9 +441,6 @@ void ui_progress(enum ui_state state) {
474 441
         } break;
475 442
 
476 443
         case UI_READY: {
477
-            // get time via NTP
478
-            configTime(gmtOffset_sec, daylightOffset_sec, NTP_SERVER);
479
-
480 444
             ui_page = UI_START;
481 445
             ui_draw_menu();
482 446
         } break;
@@ -648,3 +612,45 @@ void ui_run(void) {
648 612
 }
649 613
 
650 614
 #endif // FEATURE_UI
615
+
616
+#ifdef FEATURE_NTP
617
+
618
+String time_to_date_str(struct tm timeinfo) {
619
+    static String weekday[7] = { "So.", "Mo.", "Di.", "Mi.", "Do.", "Fr.", "Sa." };
620
+    String date;
621
+    date += weekday[timeinfo.tm_wday % 7];
622
+    date += " ";
623
+    if (timeinfo.tm_mday < 10) {
624
+        date += "0";
625
+    }
626
+    date += String(timeinfo.tm_mday);
627
+    date += ".";
628
+    if ((timeinfo.tm_mon + 1) < 10) {
629
+        date += "0";
630
+    }
631
+    date += String(timeinfo.tm_mon + 1);
632
+    date += ".";
633
+    date += String(timeinfo.tm_year + 1900);
634
+    return date;
635
+}
636
+
637
+String time_to_time_str(struct tm timeinfo) {
638
+    String time;
639
+    if (timeinfo.tm_hour < 10) {
640
+        time += "0";
641
+    }
642
+    time += String(timeinfo.tm_hour);
643
+    time += ":";
644
+    if (timeinfo.tm_min < 10) {
645
+        time += "0";
646
+    }
647
+    time += String(timeinfo.tm_min);
648
+    time += ":";
649
+    if (timeinfo.tm_sec < 10) {
650
+        time += "0";
651
+    }
652
+    time += String(timeinfo.tm_sec);
653
+    return time;
654
+}
655
+
656
+#endif // FEATURE_NTP

Loading…
Cancel
Save