From de15924447789be0b08ef9fb57e83154b9667c8d Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 22 Jul 2018 20:27:58 +0200 Subject: [PATCH] v1.3.93 --- platformio.ini | 1 + src/battery.cpp | 2 +- src/globals.h | 3 +- src/homecycle.cpp | 40 ++++++++++++++++++++ src/homecycle.h | 6 +++ src/main.cpp | 92 ++++++++++----------------------------------- src/main.h | 3 ++ src/paxcounter.conf | 9 ++--- src/senddata.cpp | 36 +++++++++++++++++- src/senddata.h | 1 + 10 files changed, 112 insertions(+), 81 deletions(-) create mode 100644 src/homecycle.cpp create mode 100644 src/homecycle.h diff --git a/platformio.ini b/platformio.ini index 30e427f6..b2c640d5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -40,6 +40,7 @@ lib_deps_rgbled = SmartLeds@>=1.1.3 lib_deps_gps = TinyGPSPlus@>=1.0.2 + Time@>=1.5 build_flags = ; we need build_flag for logging, otherwise we can't use ESP_LOGx in arduino framework ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- diff --git a/src/battery.cpp b/src/battery.cpp index 454538a9..11371748 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -45,7 +45,7 @@ uint16_t read_voltage() { #ifdef BATT_FACTOR voltage *= BATT_FACTOR; #endif - ESP_LOGI(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage); + ESP_LOGD(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage); return voltage; } diff --git a/src/globals.h b/src/globals.h index 10324461..9cc066f2 100644 --- a/src/globals.h +++ b/src/globals.h @@ -8,7 +8,7 @@ #include // attn: increment version after modifications to configData_t truct! -#define PROGVERSION "1.3.92" // use max 10 chars here! +#define PROGVERSION "1.3.93" // use max 10 chars here! #define PROGNAME "PAXCNT" // std::set for unified array functions @@ -44,6 +44,7 @@ extern uint16_t macs_total, macs_wifi, macs_ble, batt_voltage; // display values extern std::set macs; // temp storage for MACs extern hw_timer_t *channelSwitch, *sendCycle; extern portMUX_TYPE timerMux; +extern volatile int SendCycleTimerIRQ; #ifdef HAS_GPS #include "gps.h" diff --git a/src/homecycle.cpp b/src/homecycle.cpp new file mode 100644 index 00000000..c325e232 --- /dev/null +++ b/src/homecycle.cpp @@ -0,0 +1,40 @@ +/* This routine is called by interrupt in regular intervals */ +/* Interval can be set in paxcounter.conf (HOMECYCLE) */ + +// Basic config +#include "globals.h" +#include "senddata.h" + +// Local logging tag +static const char TAG[] = "main"; + +// do all housekeeping +void doHomework() { +// read battery voltage into global variable +#ifdef HAS_BATTERY_PROBE + batt_voltage = read_voltage(); + ESP_LOGI(TAG, "Measured Voltage: %dmV", batt_voltage); +#endif + +// sync time & date if we have valid gps time +#ifdef HAS_GPS + if (gps.time.isValid()) { + setTime(gps.time.hour(), gps.time.minute(), gps.time.second(), + gps.date.day(), gps.date.month(), gps.date.year()); + ESP_LOGI(TAG, "Time synced to %02d:%02d:%02d", hour(), minute(), second()); + } else { + ESP_LOGI(TAG, "No valid GPS time"); + } +#endif + + // check free memory + if (esp_get_minimum_free_heap_size() <= MEM_LOW) { + ESP_LOGW(TAG, + "Memory full, counter cleared (heap low water mark = %d Bytes / " + "free heap = %d bytes)", + esp_get_minimum_free_heap_size(), ESP.getFreeHeap()); + senddata(COUNTERPORT); // send data before clearing counters + reset_counters(); // clear macs container and reset all counters + reset_salt(); // get new salt for salting hashes + } +} \ No newline at end of file diff --git a/src/homecycle.h b/src/homecycle.h new file mode 100644 index 00000000..4d1cd468 --- /dev/null +++ b/src/homecycle.h @@ -0,0 +1,6 @@ +#ifndef _HOMECYCLE_H +#define _HOMECYCLE_H + +void doHomework(void); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index d4f2ead2..7cf1d1fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,11 +35,11 @@ uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0, // hardware timer for cyclic tasks hw_timer_t *channelSwitch = NULL, *displaytimer = NULL, *sendCycle = NULL, - *battCycle = NULL; + *homeCycle = NULL; // this variables will be changed in the ISR, and read in main loop -static volatile int ButtonPressedIRQ = 0, ChannelTimerIRQ = 0, - SendCycleTimerIRQ = 0, DisplayTimerIRQ = 0, BattReadIRQ = 0; +volatile int ButtonPressedIRQ = 0, ChannelTimerIRQ = 0, SendCycleTimerIRQ = 0, + DisplayTimerIRQ = 0, HomeCycleIRQ = 0; portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // sync main loop and ISR when modifying IRQ @@ -113,6 +113,12 @@ void IRAM_ATTR SendCycleIRQ() { portEXIT_CRITICAL(&timerMux); } +void IRAM_ATTR homeCycleIRQ() { + portENTER_CRITICAL(&timerMux); + HomeCycleIRQ++; + portEXIT_CRITICAL(&timerMux); +} + #ifdef HAS_DISPLAY void IRAM_ATTR DisplayIRQ() { portENTER_CRITICAL_ISR(&timerMux); @@ -129,21 +135,14 @@ void updateDisplay() { } #endif -#ifdef HAS_BATTERY_PROBE -void IRAM_ATTR BattCycleIRQ() { - portENTER_CRITICAL(&timerMux); - BattReadIRQ++; - portEXIT_CRITICAL(&timerMux); -} -void readBattery() { - if (BattReadIRQ) { +void checkHousekeeping() { + if (HomeCycleIRQ) { portENTER_CRITICAL(&timerMux); - BattReadIRQ = 0; + HomeCycleIRQ = 0; portEXIT_CRITICAL(&timerMux); - batt_voltage = read_voltage(); + doHomework(); } } -#endif #ifdef HAS_BUTTON void IRAM_ATTR ButtonIRQ() { ButtonPressedIRQ++; } @@ -193,43 +192,6 @@ uint64_t uptime() { return (uint64_t)high32 << 32 | low32; } -void sendPayload() { - - if (SendCycleTimerIRQ) { - portENTER_CRITICAL(&timerMux); - SendCycleTimerIRQ = 0; - portEXIT_CRITICAL(&timerMux); - - // append counter data to payload - payload.reset(); - payload.addCount(macs_wifi, cfg.blescan ? macs_ble : 0); - // append GPS data, if present - -#ifdef HAS_GPS - if ((cfg.gpsmode) && (gps.location.isValid())) { - gps_read(); - payload.addGPS(gps_status); - } - // log NMEA status, useful for debugging GPS connection - ESP_LOGD(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d", - gps.passedChecksum(), gps.failedChecksum(), - gps.sentencesWithFix()); - // log GPS position if we have a fix - if ((cfg.gpsmode) && (gps.location.isValid())) { - gps_read(); - ESP_LOGI(TAG, "lat=%.6f | lon=%.6f | %u Sats | HDOP=%.1f | Altitude=%um", - gps_status.latitude / (float)1e6, - gps_status.longitude / (float)1e6, gps_status.satellites, - gps_status.hdop / (float)100, gps_status.altitude); - } else { - ESP_LOGI(TAG, "No valid GPS position or GPS disabled"); - } -#endif - - senddata(COUNTERPORT); - } -} // sendPayload() - /* begin Aruino SETUP * ------------------------------------------------------------ */ @@ -367,13 +329,11 @@ void setup() { timerAlarmWrite(sendCycle, cfg.sendcycle * 2 * 10000, true); timerAlarmEnable(sendCycle); - // setup battery read cycle trigger IRQ using esp32 hardware timer 3 -#ifdef HAS_BATTERY_PROBE - battCycle = timerBegin(3, 8000, true); - timerAttachInterrupt(battCycle, &BattCycleIRQ, true); - timerAlarmWrite(battCycle, BATTREADCYCLE * 10000, true); - timerAlarmEnable(battCycle); -#endif + // setup house keeping cycle trigger IRQ using esp32 hardware timer 3 + homeCycle = timerBegin(3, 8000, true); + timerAttachInterrupt(homeCycle, &homeCycleIRQ, true); + timerAlarmWrite(homeCycle, HOMECYCLE * 10000, true); + timerAlarmEnable(homeCycle); // show payload encoder #if PAYLOAD_ENCODER == 1 @@ -464,24 +424,12 @@ void loop() { readButton(); #endif -#ifdef HAS_BATTERY_PROBE - readBattery(); -#endif - #ifdef HAS_DISPLAY updateDisplay(); #endif - // check free memory - if (esp_get_minimum_free_heap_size() <= MEM_LOW) { - ESP_LOGI(TAG, - "Memory full, counter cleared (heap low water mark = %d Bytes / " - "free heap = %d bytes)", - esp_get_minimum_free_heap_size(), ESP.getFreeHeap()); - senddata(COUNTERPORT); // send data before clearing counters - reset_counters(); // clear macs container and reset all counters - reset_salt(); // get new salt for salting hashes - } + // check housekeeping cycle and to homework if expired + checkHousekeeping(); // check send cycle and send payload if cycle is expired sendPayload(); diff --git a/src/main.h b/src/main.h index e09abf3d..6b418289 100644 --- a/src/main.h +++ b/src/main.h @@ -5,10 +5,13 @@ #include "macsniff.h" #include "configmanager.h" #include "senddata.h" +#include "homecycle.h" #include // needed for reading ESP32 chip attributes #include // needed for Wifi event handler +#include + void reset_counters(void); void blink_LED(uint16_t set_color, uint16_t set_blinkduration); void led_loop(void); diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 15bce027..ab6d96fe 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -8,8 +8,8 @@ #define VERBOSE 1 // comment out to silence the device, for mute use build option // Payload send cycle and encoding -#define SEND_SECS 30 // payload send cycle [seconds/2] -> 240 sec. -#define PAYLOAD_ENCODER 3 // payload encoder: 1=Plain, 2=Packed, 3=CayenneLPP dynamic, 4=CayenneLPP packed +#define SEND_SECS 30 // payload send cycle [seconds/2] -> 60 sec. +#define PAYLOAD_ENCODER 1 // payload encoder: 1=Plain, 2=Packed, 3=CayenneLPP dynamic, 4=CayenneLPP packed // Set this to include BLE counting and vendor filter functions #define VENDORFILTER 1 // comment out if you want to count things, not people @@ -40,9 +40,6 @@ #define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec. // LoRa payload default parameters -#define PAYLOAD_ENCODER 1 // select payload encoder: 1=Plain [default], 2=Packed, 3=CayenneLPP dynmic, 4=CayenneLPP packed -#define SEND_SECS 30 // payload send cycle [seconds/2] -> 60 sec. - #define MEM_LOW 2048 // [Bytes] low memory threshold triggering a send cycle #define RETRANSMIT_RCMD 5 // [seconds] wait time before retransmitting rcommand results #define PAYLOAD_BUFFER_SIZE 51 // maximum size of payload block per transmit @@ -62,7 +59,7 @@ // Some hardware settings #define RGBLUMINOSITY 30 // RGB LED luminosity [default = 30%] #define DISPLAYREFRESH_MS 40 // OLED refresh cycle in ms [default = 40] -> 1000/40 = 25 frames per second -#define BATTREADCYCLE 60 // battery measuring cycle in seconds [default = 60 secs] +#define HOMECYCLE 60 // house keeping cycle in seconds [default = 60 secs] // LMIC settings // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored diff --git a/src/senddata.cpp b/src/senddata.cpp index 8efe7fd3..e9133491 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -31,4 +31,38 @@ void senddata(uint8_t port) { ESP_LOGI(TAG, "Counter cleared"); } -} // senddata \ No newline at end of file +} // senddata + +void sendPayload() { + + if (SendCycleTimerIRQ) { + portENTER_CRITICAL(&timerMux); + SendCycleTimerIRQ = 0; + portEXIT_CRITICAL(&timerMux); + + // append counter data to payload + payload.reset(); + payload.addCount(macs_wifi, cfg.blescan ? macs_ble : 0); + // append GPS data, if present + +#ifdef HAS_GPS + // show NMEA data in debug mode, useful for debugging GPS on board + // connection + ESP_LOGD(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d", + gps.passedChecksum(), gps.failedChecksum(), + gps.sentencesWithFix()); + // log GPS position if we have a fix and gps data mode is enabled + if ((cfg.gpsmode) && (gps.location.isValid())) { + gps_read(); + payload.addGPS(gps_status); + ESP_LOGD(TAG, "lat=%.6f | lon=%.6f | %u Sats | HDOP=%.1f | Altitude=%um", + gps_status.latitude / (float)1e6, + gps_status.longitude / (float)1e6, gps_status.satellites, + gps_status.hdop / (float)100, gps_status.altitude); + } else { + ESP_LOGD(TAG, "No valid GPS position or GPS data mode disabled"); + } +#endif + senddata(COUNTERPORT); + } +} // sendpayload(); \ No newline at end of file diff --git a/src/senddata.h b/src/senddata.h index b2418162..49be9f75 100644 --- a/src/senddata.h +++ b/src/senddata.h @@ -2,5 +2,6 @@ #define _SENDDATA_H void senddata(uint8_t port); +void sendPayload(void); #endif // _SENDDATA_H_ \ No newline at end of file