diff --git a/include/globals.h b/include/globals.h index a183c2ce..7b427e5a 100644 --- a/include/globals.h +++ b/include/globals.h @@ -83,7 +83,8 @@ typedef struct { uint8_t satellites; uint16_t hdop; int16_t altitude; - time_t utctime; + uint32_t time_age; + tmElements_t timedate; } gpsStatus_t; typedef struct { diff --git a/include/gpsread.h b/include/gpsread.h index 6c20eef0..d1452b37 100644 --- a/include/gpsread.h +++ b/include/gpsread.h @@ -18,9 +18,10 @@ extern gpsStatus_t extern TaskHandle_t GpsTask; int gps_init(void); -void gps_read(void); +void IRAM_ATTR gps_storetime(gpsStatus_t &gps_store); +void gps_storelocation(gpsStatus_t &gps_store); void gps_loop(void *pvParameters); -time_t get_gpstime(void); +time_t get_gpstime(gpsStatus_t value); int gps_config(); #endif \ No newline at end of file diff --git a/include/timekeeper.h b/include/timekeeper.h index b02fccd2..87f999c0 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -28,8 +28,6 @@ uint8_t timepulse_init(void); time_t timeIsValid(time_t const t); time_t timeProvider(void); time_t compiledUTC(void); -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); diff --git a/platformio.ini b/platformio.ini index b07e0d91..9fc2e6a2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,10 +31,10 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng [common] ; for release_version use max. 10 chars total, use any decimal format like "a.b.c" -release_version = 1.7.5 +release_version = 1.7.541 ; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running! ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose -debug_level = 3 +debug_level = 0 ; UPLOAD MODE: select esptool to flash via USB/UART, select custom to upload to cloud for OTA upload_protocol = esptool ;upload_protocol = custom @@ -45,8 +45,8 @@ platform_espressif32 = espressif32@1.7.0 board_build.partitions = min_spiffs.csv monitor_speed = 115200 lib_deps_lora = - ;MCCI LoRaWAN LMIC library@>=2.3.2 - https://github.com/mcci-catena/arduino-lmic.git#e5503ff + ;MCCI LoRaWAN LMIC library@2.3.2 + https://github.com/mcci-catena/arduino-lmic.git#dc18ee9 lib_deps_display = U8g2@>=2.25.7 lib_deps_rgbled = diff --git a/src/gpsread.cpp b/src/gpsread.cpp index df1b0db7..62aa2da2 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -65,31 +65,48 @@ int gps_config() { return rslt; } -// read GPS data and cast to global struct -void gps_read() { - gps_status.latitude = (int32_t)(gps.location.lat() * 1e6); - gps_status.longitude = (int32_t)(gps.location.lng() * 1e6); - gps_status.satellites = (uint8_t)gps.satellites.value(); - gps_status.hdop = (uint16_t)gps.hdop.value(); - gps_status.altitude = (int16_t)gps.altitude.meters(); - gps_status.utctime = get_gpstime(); - - // show NMEA data in debug mode, useful for debugging GPS - ESP_LOGV(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d", - gps.passedChecksum(), gps.failedChecksum(), gps.sentencesWithFix()); +// store current GPS location data in struct +void gps_storelocation(gpsStatus_t &gps_store) { + gps_store.latitude = (int32_t)(gps.location.lat() * 1e6); + gps_store.longitude = (int32_t)(gps.location.lng() * 1e6); + gps_store.satellites = (uint8_t)gps.satellites.value(); + gps_store.hdop = (uint16_t)gps.hdop.value(); + gps_store.altitude = (int16_t)gps.altitude.meters(); } -// function to fetch current time from gps -time_t get_gpstime(void) { +// store current GPS timedate in struct +void IRAM_ATTR gps_storetime(gpsStatus_t &gps_store) { - time_t t = 0; - uint32_t time_age = gps.time.age(); + gps_store.time_age = gps.time.age(); - if (gps.time.isValid() && gps.date.isValid() && (time_age < 1000)) - t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(), - gps.time.hour(), gps.time.minute(), gps.time.second()); + if (gps.time.isValid() && gps.date.isValid() && (gps_store.time_age < 1000)) { + gps_store.timedate.Year = + CalendarYrToTm(gps.date.year()); // year offset from 1970 in microTime.h + gps_store.timedate.Month = gps.date.month(); + gps_store.timedate.Day = gps.date.day(); + gps_store.timedate.Hour = gps.time.hour(); + gps_store.timedate.Minute = gps.time.minute(); + gps_store.timedate.Second = gps.time.second(); + } else + gps_store.timedate = {0}; +} + +// function to fetch current time from struct; note: this is costly +time_t get_gpstime(gpsStatus_t value) { + + time_t t = timeIsValid(makeTime(value.timedate)); + + // if (t) + // t = value.time_age > nmea_txDelay_ms ? t : t - 1; + + // show NMEA data in verbose mode, useful for debugging GPS + ESP_LOGV( + TAG, + "GPS time: %d | GPS NMEA data: passed %d / failed: %d / with fix: %d", t, + gps.passedChecksum(), gps.failedChecksum(), gps.sentencesWithFix()); + + return t; - return timeIsValid(time_age > nmea_txDelay_ms ? t : t - 1); } // get_gpstime() // GPS serial feed FreeRTos Task diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index 054cbef9..3702634a 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -42,7 +42,7 @@ void irqHandler(void *pvParameters) { // gps refresh buffer? #if (HAS_GPS) if (InterruptStatus & GPS_IRQ) - gps_read(); + gps_storelocation(gps_status); #endif // are cyclic tasks due? diff --git a/src/main.cpp b/src/main.cpp index 50e57c44..4c573e46 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,7 +33,7 @@ IDLE 0 0 ESP32 arduino scheduler -> runs wifi sniffer clockloop 1 4 generates realtime telegrams for external clock timesync_req 1 3 processes realtime time sync requests -irqhandler 1 2 display, timesync, etc. tasks triggered by timer +irqhandler 1 2 display, timesync, gps, etc. triggered by timers gpsloop 1 2 reads data from GPS via serial or i2c bmeloop 1 1 reads data from BME sensor via i2c looptask 1 1 runs the LMIC LoRa stack (arduino loop) diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 5dd42539..ef0b44c4 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -18,6 +18,10 @@ const char timeSetSymbols[] = {'G', 'R', 'L', '?'}; HardwareSerial IF482(2); // use UART #2 (#1 may be in use for serial GPS) #endif +#if (HAS_GPS) +static gpsStatus_t gps_pps_status; +#endif + Ticker timesyncer; void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); } @@ -27,7 +31,8 @@ time_t timeProvider(void) { time_t t = 0; #if (HAS_GPS) - t = gps_pps_time; // fetch recent time from last NEMA record + // fetch recent time from last NEMA record + t = get_gpstime(gps_pps_status); if (t) { #ifdef HAS_RTC set_rtctime(t, do_mutex); // calibrate RTC @@ -116,9 +121,9 @@ void timepulse_start(void) { timerAlarmEnable(ppsIRQ); #endif +// initialize gps time #if (HAS_GPS) - gps_read(); - gps_pps_time = gps_status.utctime; + gps_storetime(gps_pps_status); #endif // start cyclic time sync @@ -134,9 +139,9 @@ void IRAM_ATTR CLOCKIRQ(void) { SyncToPPS(); // advance systime, see microTime.h - // store recent gps time, if we have + // store recent gps time, if we have gps #if (HAS_GPS) - gps_pps_time = gps_status.utctime + 1; + gps_storetime(gps_pps_status); #endif // advance wall clock, if we have @@ -169,19 +174,6 @@ time_t compiledUTC(void) { return t; } -// helper function to convert gps date/time into time_t -time_t tmConvert(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, uint8_t mm, - uint8_t ss) { - tmElements_t tm; - tm.Year = CalendarYrToTm(YYYY); // year offset from 1970 in microTime.h - tm.Month = MM; - tm.Day = DD; - tm.Hour = hh; - tm.Minute = mm; - tm.Second = ss; - return makeTime(tm); -} - // helper function to calculate serial transmit time TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPins) { diff --git a/src/timesync.cpp b/src/timesync.cpp index 8ec0ce54..2a869cb6 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -216,12 +216,12 @@ int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len) { // adjust system time, calibrate RTC and RTC_INT pps void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec) { - time_t time_to_set = (time_t)t_sec; + // advance time 1 sec wait time + time_t time_to_set = (time_t)(t_sec + 1); -// advance time 1 sec wait time if we have no pulse clock -#if (!defined GPS_INT) && (!defined RTC_INT) - time_to_set++; -#endif + //#if (!defined GPS_INT && !defined RTC_INT) + // time_to_set++; + //#endif ESP_LOGD(TAG, "[%0.3f] Calculated UTC epoch time: %d.%03d sec", millis() / 1000.0, time_to_set, t_msec);