DBtimesync (experimental)

This commit is contained in:
Verkehrsrot 2019-03-06 23:21:46 +01:00
parent bc4b4184a9
commit 79d931bf50
11 changed files with 64 additions and 27 deletions

View File

@ -4,6 +4,7 @@
#include "globals.h" #include "globals.h"
#include "rcommand.h" #include "rcommand.h"
#include "timekeeper.h" #include "timekeeper.h"
#include "DBtimesync.h"
// LMIC-Arduino LoRaWAN Stack // LMIC-Arduino LoRaWAN Stack
#include <lmic.h> #include <lmic.h>

View File

@ -8,6 +8,8 @@
#include "macsniff.h" #include "macsniff.h"
#include <rom/rtc.h> #include <rom/rtc.h>
#include "cyclic.h" #include "cyclic.h"
#include "timekeeper.h"
#include "DBtimesync.h"
// table of remote commands and assigned functions // table of remote commands and assigned functions
typedef struct { typedef struct {

View File

@ -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); uint8_t ss);
TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config, TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,
int8_t rxPin, int8_t txPins); int8_t rxPin, int8_t txPins);
time_t TimeSyncAns(uint8_t seqNo, uint64_t unixTime);
void TimeSyncReq(uint8_t seqNo);
#endif // _timekeeper_H #endif // _timekeeper_H

View File

@ -81,8 +81,10 @@ time_t get_gpstime(void) {
time_t t = 0; time_t t = 0;
if ((gps.time.age() < gpsDelay_ms) && (gps.time.isValid()) && if ((gps.time.age() < gpsDelay_ms) && gps.time.isValid() &&
(gps.date.isValid())) { gps.date.isValid() && gps.time.isUpdated()) {
gps.time.value(); // trigger isUpdated()
ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d", ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d",
gps.time.age(), gps.time.age(),

View File

@ -5,19 +5,21 @@
#include <stdint.h> #include <stdint.h>
//#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out // Hardware related definitions for Heltec V2 LoRa-32 Board
//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
// Hardware related definitions for Heltec LoRa-32 Board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa #define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1 #define CFG_sx1276_radio 1
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C // OLED-Display on board #define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C // OLED-Display on board
#define HAS_LED LED_BUILTIN // white LED on board #define HAS_LED LED_BUILTIN // white LED on board
#define HAS_BUTTON KEY_BUILTIN // button "PROG" on board #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board
// Pins for I2C interface of OLED Display // Pins for I2C interface of OLED Display
#define MY_OLED_SDA (4) #define MY_OLED_SDA (21)
#define MY_OLED_SCL (15) #define MY_OLED_SCL (22)
#define MY_OLED_RST (16) #define MY_OLED_RST (16)
// Pins for LORA chip SPI interface come from board file, we need some // Pins for LORA chip SPI interface come from board file, we need some

View File

@ -7,8 +7,8 @@
// Hardware related definitions for Heltec V2 LoRa-32 Board // Hardware related definitions for Heltec V2 LoRa-32 Board
//#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out //#define HAS_BME GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
//if not present //#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 HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1 #define CFG_sx1276_radio 1
@ -18,8 +18,8 @@
#define HAS_BUTTON KEY_BUILTIN // button "PROG" on board #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board
// Pins for I2C interface of OLED Display // Pins for I2C interface of OLED Display
#define MY_OLED_SDA (21) #define MY_OLED_SDA (4)
#define MY_OLED_SCL (22) #define MY_OLED_SCL (15)
#define MY_OLED_RST (16) #define MY_OLED_RST (16)
// Pins for LORA chip SPI interface come from board file, we need some // Pins for LORA chip SPI interface come from board file, we need some

View File

@ -224,6 +224,12 @@ void onEvent(ev_t ev) {
break; break;
case EV_TXCOMPLETE: 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") strcpy_P(buff, (LMIC.txrxFlags & TXRX_ACK) ? PSTR("RECEIVED_ACK")
: PSTR("TX_COMPLETE")); : PSTR("TX_COMPLETE"));
sprintf(display_line6, " "); // clear previous lmic status sprintf(display_line6, " "); // clear previous lmic status

View File

@ -36,6 +36,8 @@ looptask 1 1 arduino core -> runs the LMIC LoRa stack
irqhandler 1 1 executes tasks triggered by timer irq irqhandler 1 1 executes tasks triggered by timer irq
gpsloop 1 2 reads data from GPS via serial or i2c gpsloop 1 2 reads data from GPS via serial or i2c
bmeloop 1 1 reads data from BME sensor via 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 IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator
Low priority numbers denote low priority tasks. Low priority numbers denote low priority tasks.

View File

@ -67,7 +67,8 @@
// settings for syncing time of node with external time source // 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_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_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 // 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 #define DAYLIGHT_TIME {"CEST", Last, Sun, Mar, 2, 120} // Central European Summer Time

View File

@ -282,19 +282,35 @@ void get_time(uint8_t val[]) {
// format: opcode, function, #bytes params, // format: opcode, function, #bytes params,
// flag (true = do make settings persistent / false = don't) // flag (true = do make settings persistent / false = don't)
// //
cmd_t table[] = { cmd_t table[] = {{0x01, set_rssi, 1, true},
{0x01, set_rssi, 1, true}, {0x02, set_countmode, 1, true}, {0x02, set_countmode, 1, true},
{0x03, set_gps, 1, true}, {0x04, set_display, 1, true}, {0x03, set_gps, 1, true},
{0x05, set_lorasf, 1, true}, {0x06, set_lorapower, 1, true}, {0x04, set_display, 1, true},
{0x07, set_loraadr, 1, true}, {0x08, set_screensaver, 1, true}, {0x05, set_lorasf, 1, true},
{0x09, set_reset, 1, true}, {0x0a, set_sendcycle, 1, true}, {0x06, set_lorapower, 1, true},
{0x0b, set_wifichancycle, 1, true}, {0x0c, set_blescantime, 1, true}, {0x07, set_loraadr, 1, true},
{0x0d, set_vendorfilter, 1, false}, {0x0e, set_blescan, 1, true}, {0x08, set_screensaver, 1, true},
{0x0f, set_wifiant, 1, true}, {0x10, set_rgblum, 1, true}, {0x09, set_reset, 1, true},
{0x11, set_monitor, 1, true}, {0x12, set_beacon, 7, false}, {0x0a, set_sendcycle, 1, true},
{0x13, set_sensor, 2, true}, {0x80, get_config, 0, false}, {0x0b, set_wifichancycle, 1, true},
{0x81, get_status, 0, false}, {0x84, get_gps, 0, false}, {0x0c, set_blescantime, 1, true},
{0x85, get_bme, 0, false}, {0x86, get_time, 0, false}, {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 = const uint8_t cmdtablesize =

View File

@ -35,8 +35,11 @@ time_t timeProvider(void) {
} }
#endif #endif
// kick off asychronous DB timesync if we have
#ifdef DBTIMESYNC
send_DBtime_req();
// kick off asychronous lora sync if we have // 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); LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
#endif #endif