diff --git a/.gitignore b/.gitignore index da637fe1..fa273fab 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,6 @@ .clang_complete .gcc-flags.json src/loraconf.h -src/ota.conf \ No newline at end of file +src/ota.conf +platformio.ini +src/paxcounter.conf diff --git a/README.md b/README.md index 2d18ce1e..93b11a8d 100644 --- a/README.md +++ b/README.md @@ -85,11 +85,11 @@ By default bluetooth sniffing not installed (#define *BLECOUNTER* 0 in paxcounte Compile time configuration is spread across several files. Before compiling the code, edit or create the following files: -## platformio.ini -Edit `platformio.ini` and select desired hardware target in section boards. To add a new board, create an appropriate hardware abstraction layer file in hal subdirectory, and add a pointer to this file in sections boards. +## platformio_orig.ini +Edit `platformio_orig.ini` and select desired hardware target in section boards. To add a new board, create an appropriate hardware abstraction layer file in hal subdirectory, and add a pointer to this file in sections board. Copy or rename to `platformio.ini`. -## src/paxcounter.conf -Edit `src/paxcounter.conf` and tailor settings in this file according to your needs and use case. Please take care of the duty cycle regulations of the LoRaWAN network you're going to use. +## src/paxcounter_orig.conf +Edit `src/paxcounter_orig.conf` and tailor settings in this file according to your needs and use case. Please take care of the duty cycle regulations of the LoRaWAN network you're going to use. Copy or rename to `src/paxcounter.conf`. If your device has a **real time clock** it can be updated bei either LoRaWAN network or GPS time, according to settings *TIME_SYNC_INTERVAL* and *TIME_SYNC_LORAWAN* in `paxcounter.conf`. @@ -97,11 +97,10 @@ If your device has a **real time clock** it can be updated bei either LoRaWAN ne Edit `src/lmic_config.h` and tailor settings in this file according to your country and device hardware. Please take care of national regulations when selecting the frequency band for LoRaWAN. ## src/loraconf.h -Create file `src/loraconf.h` using the template [src/loraconf.sample.h](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/loraconf.sample.h) and modify it, to use your personal values. -To join the network and activate your paxcounter, you have to configure either the preferred OTAA method or the ABP method. You should use OTAA, whenever possible. To understand the differences of the two methods, [this article](https://www.thethingsnetwork.org/docs/devices/registration.html) may be useful. +Create file `src/loraconf.h` using the template [src/loraconf_sample.h](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/loraconf_sample.h) and adjust settings to use your personal values. To join the network and activate your paxcounter, you must configure either OTAA or ABP join method. You should use OTAA, whenever possible. To understand the differences of the two methods, [this article](https://www.thethingsnetwork.org/docs/devices/registration.html) may be useful. To configure OTAA, leave `#define LORA_ABP` deactivated (commented). To use ABP, activate (uncomment) `#define LORA_ABP` in the file `src/loraconf.h`. -The file `src/loraconf.h.sample` contains more information about the values to provide. +The file `src/loraconf_sample.h` contains more information about the values to provide. ## src/ota.conf Create file `src/ota.conf` using the template [src/ota.sample.conf](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/ota.sample.conf) and enter your WIFI network&key. These settings are used for downloading updates. If you want to push own OTA updates you need a Bintray account. Enter your Bintray user account data in ota.conf. If you don't need wireless firmware updates just rename ota.sample.conf to ota.conf. @@ -480,7 +479,16 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0x14 set payload mask - byte 1 = sensor data payload mask (0..255, meaning of bits see above) + byte 1 = sensor data payload mask (0..255, meaning of bits see below) + 0x01 = GPS_DATA + 0x02 = ALARM_DATA + 0x04 = MEMS_DATA + 0x08 = COUNT_DATA (default) + 0x10 = SENSOR_1_DATA (ENS-COUNTS) + 0x20 = SENSOR_2_DATA + 0x40 = SENSOR_3_DATA + 0x80 = BATT_DATA + bytes can be combined eg COUNT_DATA ;SENSOR_1_DATA ;BATT_DATA: `0x08 | 0x10 |0x80 = 0x98` 0x15 set BME data on/off diff --git a/platformio.ini b/platformio_orig.ini similarity index 99% rename from platformio.ini rename to platformio_orig.ini index 695b6435..0edbc702 100644 --- a/platformio.ini +++ b/platformio_orig.ini @@ -46,7 +46,7 @@ description = Paxcounter is a device for metering passenger flows in realtime. I [common] ; for release_version use max. 10 chars total, use any decimal format like "a.b.c" -release_version = 2.0.12 +release_version = 2.0.15 ; 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 diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 45f1b65d..5589f4b2 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -6,8 +6,11 @@ // Local logging tag static const char TAG[] = __FILE__; -// we use NMEA $GPZDA sentence field 1 for time synchronization -// $GPZDA gives time for preceding pps pulse, but does not has a constant offset +// we use NMEA ZDA sentence field 1 for time synchronization +// ZDA gives time for preceding pps pulse +// downsight is that it does not have a constant offset +// thus precision is only +/- 1 second + TinyGPSPlus gps; TinyGPSCustom gpstime(gps, "GPZDA", 1); // field 1 = UTC time static const String ZDA_Request = "$EIGPQ,ZDA*39\r\n"; @@ -95,7 +98,7 @@ time_t get_gpstime(uint16_t *msec) { time_t time_sec = 0; - // poll NMEA $GPZDA sentence + // poll NMEA ZDA sentence #ifdef GPS_SERIAL GPS_Serial.print(ZDA_Request); // wait for gps NMEA answer @@ -159,6 +162,13 @@ void gps_loop(void *pvParameters) { delay(2); // 2ms delay according L76 datasheet } #endif + + // if time hasn't been synchronised yet, and we have a valid GPS time, + // update time from GPS. + if (timeSource == _unsynced && gpstime.isUpdated() && gpstime.isValid()) { + calibrateTime(); + } + } // if // show NMEA data in verbose mode, useful for debugging GPS, bu tvery noisy diff --git a/src/loraconf.sample.h b/src/loraconf_sample.h similarity index 96% rename from src/loraconf.sample.h rename to src/loraconf_sample.h index a088cdc2..3b7d9cb2 100644 --- a/src/loraconf.sample.h +++ b/src/loraconf_sample.h @@ -1,89 +1,88 @@ -#ifndef __LORACONF_H__ -#define __LORACONF_H__ - -#if (HAS_LORA) - -/************************************************************ - * LMIC LoRaWAN configuration - * - * Read the values from TTN console (or whatever applies), insert them here, - * and rename this file to src/loraconf.h - * - * You can configure OTAA or ABP Activation. In order to use ABP, uncomment - * (enable) the following line, but you should only do so, if you have good - * reasons for not using OTAA. - * - *************************************************************/ - -//#define LORA_ABP - -#ifndef LORA_ABP -/************************************************************ - * OTAA configuration - * - * DEVEUI, APPEUI and APPKEY should all be specified in MSB format as - * displayed in TTN console, so you can cut & paste from there. This is different - * from standard LMIC-Arduino which expects DEVEUI and APPEUI in LSB format. - * For TTN, APPEUI in MSB format always starts with 0x70, 0xB3, 0xD5. - - * Set your DEVEUI here, if your device has have a fixed one. - * If you leave this untouched, then the DEVEUI will be derived from device's - * MAC adress during startup and will be displayed on device's screen as well as - * on serial console, if you set 'verbose 1' in paxcounter.conf and - * 'debug_level 3' in platformio.ini. - * If using a board with Microchip 24AA02E64 Uinique ID for deveui, the DEVEUI - * will be overwritten by the one contained in the Microchip module. - * - ************************************************************/ - -static const u1_t DEVEUI[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -static const u1_t APPEUI[8] = {0x70, 0xB3, 0xD5, 0x00, 0x00, 0x00, 0x00, 0x00}; - -static const u1_t APPKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -#endif - -#ifdef LORA_ABP -/************************************************************ - * ABP configuration (for development) - * - * Get your - * - Network Session Key (NWKSKEY) - * - App Session Key and your (APPSKEY) - * - Device Address (DEVADDR) - * from e.g. TTN console and replace the example values below. - * - * NOTE: Use MSB format (as displayed in TTN console, so you can cut & paste - * from there) - * - * NOTE: You may also need to adjust lorawan_abp.cpp in order to configure - * different channels and data rate channels to match your country's regulations - * and your network's settings. - * - ************************************************************/ - -// ID of LoRaAlliance assigned Network (for a list, see e.g. here -// https://www.thethingsnetwork.org/docs/lorawan/prefix-assignments.html) -static const u1_t NETID = 0x13; // TTN - -static const u1_t NWKSKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - -static const u1_t APPSKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - -static const u4_t DEVADDR = - 0x00000000; // <-- Change this address for every node! - -// set additional ABP parameters in loraconf_abp.cpp -void setABPParameters(); - -#endif - -#endif // HAS_LORA - -#endif // __LORACONF_H__ \ No newline at end of file +#ifndef __LORACONF_H__ +#define __LORACONF_H__ + +#if (HAS_LORA) + +/************************************************************ + * LMIC LoRaWAN configuration + * + * Read the values from TTN console (or whatever applies), insert them here, + * and rename this file to src/loraconf.h + * + * You can configure OTAA or ABP Activation. In order to use ABP, uncomment + * (enable) the following line, but you should only do so, if you have good + * reasons for not using OTAA. + * + *************************************************************/ + +//#define LORA_ABP + +#ifndef LORA_ABP +/************************************************************ + * OTAA configuration + * + * DEVEUI, APPEUI and APPKEY should all be specified in MSB format as + * displayed in TTN console, so you can cut & paste from there. This is different + * from standard LMIC-Arduino which expects DEVEUI and APPEUI in LSB format. + * For TTN, APPEUI in MSB format always starts with 0x70, 0xB3, 0xD5. + * Set your DEVEUI here, if your device has have a fixed one. + * If you leave this untouched, then the DEVEUI will be derived from device's + * MAC adress during startup and will be displayed on device's screen as well as + * on serial console, if you set 'verbose 1' in paxcounter.conf and + * 'debug_level 3' in platformio.ini. + * If using a board with Microchip 24AA02E64 Uinique ID for deveui, the DEVEUI + * will be overwritten by the one contained in the Microchip module. + * + ************************************************************/ + +static const u1_t DEVEUI[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +static const u1_t APPEUI[8] = {0x70, 0xB3, 0xD5, 0x00, 0x00, 0x00, 0x00, 0x00}; + +static const u1_t APPKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +#endif + +#ifdef LORA_ABP +/************************************************************ + * ABP configuration (for development) + * + * Get your + * - Network Session Key (NWKSKEY) + * - App Session Key and your (APPSKEY) + * - Device Address (DEVADDR) + * from e.g. TTN console and replace the example values below. + * + * NOTE: Use MSB format (as displayed in TTN console, so you can cut & paste + * from there) + * + * NOTE: You may also need to adjust lorawan_abp.cpp in order to configure + * different channels and data rate channels to match your country's regulations + * and your network's settings. + * + ************************************************************/ + +// ID of LoRaAlliance assigned Network (for a list, see e.g. here +// https://www.thethingsnetwork.org/docs/lorawan/prefix-assignments.html) +static const u1_t NETID = 0x13; // TTN + +static const u1_t NWKSKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00}; + +static const u1_t APPSKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00}; + +static const u4_t DEVADDR = + 0x00000000; // <-- Change this address for every node! + +// set additional ABP parameters in loraconf_abp.cpp +void setABPParameters(); + +#endif + +#endif // HAS_LORA + +#endif // __LORACONF_H__ diff --git a/src/main.cpp b/src/main.cpp index e7b497c7..175e78f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -488,11 +488,6 @@ void setup() { #warning you did not specify a time source, time will not be synched #endif -// initialize gps time -#if (HAS_GPS) - get_gpstime(); -#endif - #if (defined HAS_IF482 || defined HAS_DCF77) ESP_LOGI(TAG, "Starting Clock Controller..."); clock_init(); diff --git a/src/ota.sample.conf b/src/ota_sample.conf similarity index 100% rename from src/ota.sample.conf rename to src/ota_sample.conf diff --git a/src/paxcounter.conf b/src/paxcounter_orig.conf similarity index 98% rename from src/paxcounter.conf rename to src/paxcounter_orig.conf index 57264b72..a174d819 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter_orig.conf @@ -29,7 +29,7 @@ // set to 0 if you do not want to enable this function // for additional sensors (added by some user) -#define HAS_SENSOR_1 0 // set to 1 if you want to transmit CWA counter +#define HAS_SENSOR_1 0 // set to 1 if you want to transmit CWA counter #define HAS_SENSOR_2 0 // not used #define HAS_SENSOR_3 0 // not used #define HAS_SENSORS (HAS_SENSOR_1 || HAS_SENSOR_2 || HAS_SENSOR_3) // to simplify things @@ -89,7 +89,7 @@ #define TIME_SYNC_INTERVAL_RETRY 10 // retry time sync after lost sync each .. minutes [default = 10], 0 means off #define TIME_SYNC_SAMPLES 1 // number of time requests for averaging, max. 255 #define TIME_SYNC_CYCLE 60 // delay between two time samples [seconds] -#define TIME_SYNC_TIMEOUT 300 // timeout waiting for timeserver answer [seconds] +#define TIME_SYNC_TIMEOUT 400 // timeout waiting for timeserver answer [seconds] #define TIME_SYNC_COMPILEDATE 0 // set to 1 to use compile date to initialize RTC after power outage [default = 0] // time zone, see https://github.com/JChristensen/Timezone/blob/master/examples/WorldClock/WorldClock.ino diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index bc9e52b5..21513a05 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -26,12 +26,13 @@ Ticker timesyncer; void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); } void calibrateTime(void) { - + ESP_LOGD(TAG, "[%0.3f] calibrateTime, timeSource == %d", millis() / 1000.0, + timeSource); time_t t = 0; uint16_t t_msec = 0; // kick off asychronous lora timesync if we have -#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN) +#if (HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)) timesync_request(); #endif @@ -43,17 +44,17 @@ void calibrateTime(void) { // has RTC -> fallback to RTC time #ifdef HAS_RTC t = get_rtctime(); - timeSource = _rtc; + // set time from RTC - method will check if time is valid + setMyTime((uint32_t)t, t_msec, _rtc); #endif // no RTC -> fallback to GPS time #if (HAS_GPS) t = get_gpstime(&t_msec); - timeSource = _gps; + // set time from GPS - method will check if time is valid + setMyTime((uint32_t)t, t_msec, _gps); #endif - setMyTime((uint32_t)t, t_msec, timeSource); // set time - } // fallback else