From 79d931bf50b63add723d911d5c58355d5cb352c5 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Wed, 6 Mar 2019 23:21:46 +0100 Subject: [PATCH] DBtimesync (experimental) --- include/lorawan.h | 1 + include/rcommand.h | 2 ++ include/timekeeper.h | 2 ++ src/gpsread.cpp | 6 ++++-- src/hal/heltec.h | 12 +++++++----- src/hal/heltecv2.h | 8 ++++---- src/lorawan.cpp | 6 ++++++ src/main.cpp | 2 ++ src/paxcounter.conf | 5 +++-- src/rcommand.cpp | 42 +++++++++++++++++++++++++++++------------- src/timekeeper.cpp | 5 ++++- 11 files changed, 64 insertions(+), 27 deletions(-) diff --git a/include/lorawan.h b/include/lorawan.h index ddc2fc41..86f8e852 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -4,6 +4,7 @@ #include "globals.h" #include "rcommand.h" #include "timekeeper.h" +#include "DBtimesync.h" // LMIC-Arduino LoRaWAN Stack #include diff --git a/include/rcommand.h b/include/rcommand.h index 269caf91..dea6ce99 100644 --- a/include/rcommand.h +++ b/include/rcommand.h @@ -8,6 +8,8 @@ #include "macsniff.h" #include #include "cyclic.h" +#include "timekeeper.h" +#include "DBtimesync.h" // table of remote commands and assigned functions typedef struct { diff --git a/include/timekeeper.h b/include/timekeeper.h index 4714456a..ef83b11b 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -31,5 +31,7 @@ time_t tmConvert(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, uint8_t mm, uint8_t ss); TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPins); +time_t TimeSyncAns(uint8_t seqNo, uint64_t unixTime); +void TimeSyncReq(uint8_t seqNo); #endif // _timekeeper_H \ No newline at end of file diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 2036b611..59739bf4 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -81,8 +81,10 @@ time_t get_gpstime(void) { time_t t = 0; - if ((gps.time.age() < gpsDelay_ms) && (gps.time.isValid()) && - (gps.date.isValid())) { + if ((gps.time.age() < gpsDelay_ms) && gps.time.isValid() && + gps.date.isValid() && gps.time.isUpdated()) { + + gps.time.value(); // trigger isUpdated() ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d", gps.time.age(), diff --git a/src/hal/heltec.h b/src/hal/heltec.h index 3b1e9e5a..f8ab5b8e 100644 --- a/src/hal/heltec.h +++ b/src/hal/heltec.h @@ -5,19 +5,21 @@ #include -//#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out -//if not present +// Hardware related definitions for Heltec V2 LoRa-32 Board + +//#define HAS_BME GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL +//#define BME_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND -// Hardware related definitions for Heltec LoRa-32 Board #define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1276_radio 1 + #define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C // OLED-Display on board #define HAS_LED LED_BUILTIN // white LED on board #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board // Pins for I2C interface of OLED Display -#define MY_OLED_SDA (4) -#define MY_OLED_SCL (15) +#define MY_OLED_SDA (21) +#define MY_OLED_SCL (22) #define MY_OLED_RST (16) // Pins for LORA chip SPI interface come from board file, we need some diff --git a/src/hal/heltecv2.h b/src/hal/heltecv2.h index ad34f68c..efa9af4d 100644 --- a/src/hal/heltecv2.h +++ b/src/hal/heltecv2.h @@ -7,8 +7,8 @@ // Hardware related definitions for Heltec V2 LoRa-32 Board -//#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out -//if not present +//#define HAS_BME GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL +//#define BME_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND #define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1276_radio 1 @@ -18,8 +18,8 @@ #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board // Pins for I2C interface of OLED Display -#define MY_OLED_SDA (21) -#define MY_OLED_SCL (22) +#define MY_OLED_SDA (4) +#define MY_OLED_SCL (15) #define MY_OLED_RST (16) // Pins for LORA chip SPI interface come from board file, we need some diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 2c0fec3b..0de146d8 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -224,6 +224,12 @@ void onEvent(ev_t ev) { break; case EV_TXCOMPLETE: + +#ifdef DBTIMESYNC + if (!(LMIC.txrxFlags & TXRX_ACK) && time_sync_seqNo) + time_sync_messages[time_sync_seqNo - 1] = LMIC.txend; +#endif + strcpy_P(buff, (LMIC.txrxFlags & TXRX_ACK) ? PSTR("RECEIVED_ACK") : PSTR("TX_COMPLETE")); sprintf(display_line6, " "); // clear previous lmic status diff --git a/src/main.cpp b/src/main.cpp index ff0d5e4b..8c9bb371 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,8 @@ looptask 1 1 arduino core -> runs the LMIC LoRa stack irqhandler 1 1 executes tasks triggered by timer irq gpsloop 1 2 reads data from GPS via serial or i2c bmeloop 1 1 reads data from BME sensor via i2c +timesync_ans 1 0 temporary task for receiving time sync requests +timesync_req 1 0 temporary task for sending time sync requests IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator Low priority numbers denote low priority tasks. diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 6d47a438..e0660c3c 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -66,8 +66,9 @@ #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] // settings for syncing time of node with external time source -#define TIME_SYNC_INTERVAL 2 // sync time attempt each .. minutes from time source (GPS/LORA/RTC) [default = 60], comment out means off -#define TIME_SYNC_LORA 1 // use LORA network as time source, comment out means off [default = off] +#define TIME_SYNC_INTERVAL 2 // sync time attempt each .. minutes from time source (GPS/LORA/RTC) [default = 60], comment out means off +//#define TIME_SYNC_LORA 1 // use LORA network as time source, comment out means off [default = off] +#define DBTIMESYNC 1 // use DB LORA timeserver with patented sync algorithm [default = off] // time zone, see https://github.com/JChristensen/Timezone/blob/master/examples/WorldClock/WorldClock.ino #define DAYLIGHT_TIME {"CEST", Last, Sun, Mar, 2, 120} // Central European Summer Time diff --git a/src/rcommand.cpp b/src/rcommand.cpp index a49dc1e8..5a4718cf 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -282,19 +282,35 @@ void get_time(uint8_t val[]) { // format: opcode, function, #bytes params, // flag (true = do make settings persistent / false = don't) // -cmd_t table[] = { - {0x01, set_rssi, 1, true}, {0x02, set_countmode, 1, true}, - {0x03, set_gps, 1, true}, {0x04, set_display, 1, true}, - {0x05, set_lorasf, 1, true}, {0x06, set_lorapower, 1, true}, - {0x07, set_loraadr, 1, true}, {0x08, set_screensaver, 1, true}, - {0x09, set_reset, 1, true}, {0x0a, set_sendcycle, 1, true}, - {0x0b, set_wifichancycle, 1, true}, {0x0c, set_blescantime, 1, true}, - {0x0d, set_vendorfilter, 1, false}, {0x0e, set_blescan, 1, true}, - {0x0f, set_wifiant, 1, true}, {0x10, set_rgblum, 1, true}, - {0x11, set_monitor, 1, true}, {0x12, set_beacon, 7, false}, - {0x13, set_sensor, 2, true}, {0x80, get_config, 0, false}, - {0x81, get_status, 0, false}, {0x84, get_gps, 0, false}, - {0x85, get_bme, 0, false}, {0x86, get_time, 0, false}, +cmd_t table[] = {{0x01, set_rssi, 1, true}, + {0x02, set_countmode, 1, true}, + {0x03, set_gps, 1, true}, + {0x04, set_display, 1, true}, + {0x05, set_lorasf, 1, true}, + {0x06, set_lorapower, 1, true}, + {0x07, set_loraadr, 1, true}, + {0x08, set_screensaver, 1, true}, + {0x09, set_reset, 1, true}, + {0x0a, set_sendcycle, 1, true}, + {0x0b, set_wifichancycle, 1, true}, + {0x0c, set_blescantime, 1, true}, + {0x0d, set_vendorfilter, 1, false}, + {0x0e, set_blescan, 1, true}, + {0x0f, set_wifiant, 1, true}, + {0x10, set_rgblum, 1, true}, + {0x11, set_monitor, 1, true}, + {0x12, set_beacon, 7, false}, + {0x13, set_sensor, 2, true}, + {0x80, get_config, 0, false}, + {0x81, get_status, 0, false}, + {0x84, get_gps, 0, false}, + {0x85, get_bme, 0, false}, + {0x86, get_time, 0, false} +#ifdef DBTIMESYNC + , + {TIME_ANS_OPCODE, recv_DBtime_ans, 0, false}, + {TIME_SYNC_OPCODE, force_DBtime_sync, 0, false} +#endif }; const uint8_t cmdtablesize = diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 3575df28..5b2da09c 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -35,8 +35,11 @@ time_t timeProvider(void) { } #endif +// kick off asychronous DB timesync if we have +#ifdef DBTIMESYNC + send_DBtime_req(); // kick off asychronous lora sync if we have -#if defined HAS_LORA && defined TIME_SYNC_LORA +#elif defined HAS_LORA && defined TIME_SYNC_LORA LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime); #endif