From a6c631db241fb6f1b6588c786a96a0adfc3e8726 Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Thu, 4 Mar 2021 16:02:24 +0100 Subject: [PATCH 01/68] paxexpress integration --- build.py | 2 +- lib/BintrayClient/src/BintrayClient.cpp | 9 +++------ src/ota.cpp | 2 -- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/build.py b/build.py index 0d4e24bc7..ff7bd1619 100644 --- a/build.py +++ b/build.py @@ -118,7 +118,7 @@ def publish_bintray(source, target, env): firmware_path = str(source[0]) firmware_name = basename(firmware_path) url = "/".join([ - "https://api.bintray.com", "content", + "https://pax.express", "content", user, repository, package, version, firmware_name ]) diff --git a/lib/BintrayClient/src/BintrayClient.cpp b/lib/BintrayClient/src/BintrayClient.cpp index 34fa357fb..5c3012908 100644 --- a/lib/BintrayClient/src/BintrayClient.cpp +++ b/lib/BintrayClient/src/BintrayClient.cpp @@ -24,12 +24,9 @@ BintrayClient::BintrayClient(const String &user, const String &repository, const String &package) : m_user(user), m_repo(repository), m_package(package), - m_storage_host("dl.bintray.com"), - m_api_host("api.bintray.com") + m_storage_host("pax.express"), + m_api_host("pax.express") { - m_certificates.emplace_back("cloudfront.net", CLOUDFRONT_API_ROOT_CA); - m_certificates.emplace_back("akamai.bintray.com", BINTRAY_AKAMAI_ROOT_CA); - m_certificates.emplace_back("bintray.com", BINTRAY_API_ROOT_CA); } String BintrayClient::getUser() const @@ -83,7 +80,7 @@ String BintrayClient::requestHTTPContent(const String &url) const { String payload; HTTPClient http; - http.begin(url, getCertificate(url)); + http.begin(url); int httpCode = http.GET(); if (httpCode > 0) diff --git a/src/ota.cpp b/src/ota.cpp index bf4a41e04..2d80c23dd 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -150,7 +150,6 @@ int do_ota_update() { WiFiClientSecure client; - client.setCACert(bintray.getCertificate(currentHost)); client.setTimeout(RESPONSE_TIMEOUT_MS); if (!client.connect(currentHost.c_str(), port)) { @@ -162,7 +161,6 @@ int do_ota_update() { while (redirect) { if (currentHost != prevHost) { client.stop(); - client.setCACert(bintray.getCertificate(currentHost)); if (!client.connect(currentHost.c_str(), port)) { ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s", currentHost.c_str()); From a03220dd1f5cd48ea99f8b826c76628e08d5ce95 Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Wed, 17 Mar 2021 11:46:35 +0100 Subject: [PATCH 02/68] Added new setInsecure call for PAX.express OTA --- src/ota.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ota.cpp b/src/ota.cpp index 2d80c23dd..ad11822c6 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -151,7 +151,7 @@ int do_ota_update() { WiFiClientSecure client; client.setTimeout(RESPONSE_TIMEOUT_MS); - + client.setInsecure(); if (!client.connect(currentHost.c_str(), port)) { ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str()); ota_display(3, " E", "connection lost"); From 3f3027aa69de87e835c4c2efeebbee354b57002e Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Tue, 2 Mar 2021 18:36:34 +0100 Subject: [PATCH 03/68] libpax integration --- include/cyclic.h | 3 +++ include/globals.h | 3 +++ include/senddata.h | 8 ++++++ platformio_orig.ini | 4 +++ src/blecsan.cpp | 2 ++ src/cyclic.cpp | 5 +++- src/display.cpp | 18 ++++++++++++- src/main.cpp | 56 ++++++++++++++++++++++++++++++++++++++++ src/paxcounter_orig.conf | 5 +++- src/rcommand.cpp | 16 ++++++++++++ src/reset.cpp | 2 ++ src/senddata.cpp | 29 +++++++++++++++++++-- src/wifiscan.cpp | 4 ++- 13 files changed, 149 insertions(+), 6 deletions(-) diff --git a/include/cyclic.h b/include/cyclic.h index 8b16046cf..0fbbc6ca8 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -12,6 +12,9 @@ #include "sdcard.h" #include "macsniff.h" #include "reset.h" +#ifdef LIBPAX +#include +#endif extern Ticker cyclicTimer; diff --git a/include/globals.h b/include/globals.h index 1930a3f50..272d8b572 100644 --- a/include/globals.h +++ b/include/globals.h @@ -141,6 +141,9 @@ extern uint8_t volatile channel; // wifi channel rotation counter extern uint8_t volatile rf_load; // RF traffic indicator extern uint8_t batt_level; // display value extern uint16_t volatile macs_wifi, macs_ble; // display values +#ifdef LIBPAX +extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values +#endif extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC extern timesource_t timeSource; extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ; diff --git a/include/senddata.h b/include/senddata.h index c99b1f5a8..98cd4c5d0 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -9,10 +9,18 @@ #include "display.h" #include "sdcard.h" + #if (COUNT_ENS) #include "corona.h" #endif +#if (LIBPAX) +#include + +extern struct count_payload_t count_from_libpax; +#endif + + extern Ticker sendTimer; void SendPayload(uint8_t port); diff --git a/platformio_orig.ini b/platformio_orig.ini index 3c55b8bba..86e1f0813 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -89,6 +89,7 @@ lib_deps_basic = lewisxhe/AXP202X_Library @ ^1.1.3 geeksville/esp32-micro-sdcard @ ^0.1.1 256dpi/MQTT @ ^2.4.7 + https://github.com/dbSuS/libpax lib_deps_all = ${common.lib_deps_basic} ${common.lib_deps_lora} @@ -105,6 +106,9 @@ build_flags_basic = '-DPROGVERSION="${common.release_version}"' '-Wno-unknown-pragmas' '-Wno-unused-variable' + '-D LIBPAX_WIFI' + '-D LIBPAX_BLE' + '-D LIBPAX_ARDUINO' build_flags_sensors = -Llib/Bosch-BSEC/src/esp32/ -lalgobsec diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 9b29d7a94..071a80072 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -1,3 +1,4 @@ +#ifndef LIBPAX // some code snippets taken from // https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner @@ -300,3 +301,4 @@ void stop_BLEscan(void) { ESP_LOGI(TAG, "Bluetooth scanner stopped"); #endif // BLECOUNTER } // stop_BLEscan +#endif \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index db0e9d2fa..61185f0ff 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -134,11 +134,14 @@ uint32_t getFreeRAM() { } void reset_counters() { + #if ((WIFICOUNTER) || (BLECOUNTER)) +#ifndef LIBPAX macs.clear(); // clear all macs container macs_wifi = 0; macs_ble = 0; - renew_salt(); // get new salt + renew_salt(); // get new salt +#endif #ifdef HAS_DISPLAY dp_plotCurve(0, true); #endif diff --git a/src/display.cpp b/src/display.cpp index 65331bf68..711bdc958 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -262,7 +262,11 @@ void dp_drawPage(time_t t, bool nextpage) { #if ((WIFICOUNTER) && (BLECOUNTER)) if (cfg.wifiscan) +#ifndef LIBPAX dp_printf("WIFI:%-5d", macs_wifi); +#else + dp_printf("WIFI:%-5d", libpax_macs_wifi); +#endif else dp_printf("WIFI:off"); if (cfg.blescan) @@ -271,17 +275,29 @@ void dp_drawPage(time_t t, bool nextpage) { dp_printf(" CWA:%-5d", cwa_report()); else #endif - dp_printf("BLTH:%-5d", macs_ble); +#ifndef LIBPAX + dp_printf("BLTH:%-5d", macs_ble); +#else + dp_printf("BLTH:%-5d", libpax_macs_ble); +#endif else dp_printf(" BLTH:off"); #elif ((WIFICOUNTER) && (!BLECOUNTER)) if (cfg.wifiscan) +#ifndef LIBPAX dp_printf("WIFI:%-5d", macs_wifi); +#else + dp_printf("WIFI:%-5d", libpax_macs_wifi); +#endif else dp_printf("WIFI:off"); #elif ((!WIFICOUNTER) && (BLECOUNTER)) if (cfg.blescan) +#ifndef LIBPAX dp_printf("BLTH:%-5d", macs_ble); +#else + dp_printf("BLTH:%-5d", libpax_macs_ble); +#endif #if (COUNT_ENS) if (cfg.enscount) dp_printf("(CWA:%d)", cwa_report()); diff --git a/src/main.cpp b/src/main.cpp index 1decf9015..c03e136f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -90,9 +90,15 @@ triggers pps 1 sec impulse configData_t cfg; // struct holds current device configuration char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message uint8_t batt_level = 0; // display value +#ifndef LIBPAX uint8_t volatile channel = WIFI_CHANNEL_MIN; // channel rotation counter +#endif uint8_t volatile rf_load = 0; // RF traffic indicator +#ifndef LIBPAX uint16_t volatile macs_wifi = 0, macs_ble = 0; // globals for display +#else +uint16_t volatile libpax_macs_ble, libpax_macs_wifi; +#endif hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; @@ -113,6 +119,17 @@ TimeChangeRule myDST = DAYLIGHT_TIME; TimeChangeRule mySTD = STANDARD_TIME; Timezone myTZ(myDST, mySTD); +// libpax payload +#ifdef LIBPAX +struct count_payload_t count_from_libpax; + +void process_count(void) { + printf("pax: %d; %d; %d;\n", count_from_libpax.pax, count_from_libpax.wifi_count, count_from_libpax.ble_count); + libpax_macs_ble = count_from_libpax.ble_count; + libpax_macs_wifi = count_from_libpax.wifi_count; +} +#endif + // local Tag for logging static const char TAG[] = __FILE__; @@ -298,9 +315,39 @@ void setup() { if (RTC_runmode == RUNMODE_MAINTENANCE) start_boot_menu(); +#ifndef LIBPAX // start mac processing task ESP_LOGI(TAG, "Starting MAC processor..."); macQueueInit(); +#else +ESP_LOGI(TAG, "Starting libpax..."); +#if (defined WIFICOUNTER || defined BLECOUNTER) + struct libpax_config_t configuration; + libpax_default_config(&configuration); + ESP_LOGI(TAG, "BLESCAN: %d", cfg.blescan); + ESP_LOGI(TAG, "WIFISCAN: %d", cfg.wifiscan); + if(!(cfg.blescan && cfg.wifiscan)) { + configuration.wificounter = cfg.wifiscan; + configuration.blecounter = cfg.blescan; + + configuration.wifi_channel_map = WIFI_CHANNEL_ALL; + configuration.wifi_channel_switch_interval = cfg.wifichancycle; + configuration.wifi_rssi_threshold = cfg.rssilimit; + + configuration.blescantime = cfg.blescantime; + + int config_update = libpax_update_config(&configuration); + if(config_update != 0) { + ESP_LOGE(TAG, "Error in libpax configuration."); + } else { + libpax_counter_init(process_count, &count_from_libpax, 60*1000, 1); + libpax_counter_start(); + } + } else { + ESP_LOGE(TAG, "Error in libpax configuration: Wifi and BLE are not supported at the same time!"); + } +#endif +#endif // start rcommand processing task ESP_LOGI(TAG, "Starting rcommand interpreter..."); @@ -310,14 +357,18 @@ void setup() { // or remove bluetooth stack from RAM, if option bluetooth is not compiled #if (BLECOUNTER) strcat_P(features, " BLE"); +#ifndef LIBPAX if (cfg.blescan) { ESP_LOGI(TAG, "Starting Bluetooth..."); start_BLEscan(); } else btStop(); +#endif #else // remove bluetooth stack to gain more free memory +#ifndef LIBPAX btStop(); +#endif esp_bt_mem_release(ESP_BT_MODE_BTDM); esp_coex_preference_set( ESP_COEX_PREFER_WIFI); // configure Wifi/BT coexist lib @@ -427,6 +478,7 @@ void setup() { #if (WIFICOUNTER) strcat_P(features, " WIFI"); +#ifndef LIBPAX // install wifi driver in RAM and start channel hopping wifi_sniffer_init(); // start wifi sniffing, if enabled @@ -435,6 +487,8 @@ void setup() { switch_wifi_sniffer(1); } else switch_wifi_sniffer(0); +#endif + #else // remove wifi driver from RAM, if option wifi not compiled esp_wifi_deinit(); @@ -443,7 +497,9 @@ void setup() { // initialize salt value using esp_random() called by random() in // arduino-esp32 core. Note: do this *after* wifi has started, since // function gets it's seed from RF noise +#ifndef LIBPAX reset_counters(); +#endif // start state machine ESP_LOGI(TAG, "Starting Interrupt Handler..."); diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index 3440d8fc2..83f8b3767 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -20,7 +20,7 @@ // MAC sniffing parameters #define MACFILTER 1 // set to 0 if you want to scan all devices, 1 to scan only devices with random MACs (aka smartphones) [default = 1] -#define BLECOUNTER 1 // set to 0 if you do not want to install the BLE sniffer +#define BLECOUNTER 0 // set to 0 if you do not want to install the BLE sniffer #define WIFICOUNTER 1 // set to 0 if you do not want to install the WIFI sniffer #define MAC_QUEUE_SIZE 50 // size of MAC processing buffer (number of MACs) [default = 50] @@ -29,6 +29,9 @@ #define BLESCANWINDOW 80 // [milliseconds] scan window, see below, 3 .. 10240, default 80ms #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle +// Use libpax instead of default counting algorithms +#define LIBPAX 1 + // Corona Exposure Notification Service(ENS) counter #define COUNT_ENS 1 // count found number of devices which advertise Exposure Notification Service // set to 1 if you want to enable this function [default=0] diff --git a/src/rcommand.cpp b/src/rcommand.cpp index e4b009b35..353d25c04 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -72,6 +72,7 @@ void set_sleepcycle(uint8_t val[]) { void set_wifichancycle(uint8_t val[]) { cfg.wifichancycle = val[0]; + #ifndef LIBAPX // update Wifi channel rotation timer period if (cfg.wifichancycle > 0) { if (xTimerIsTimerActive(WifiChanTimer) == pdFALSE) @@ -88,10 +89,14 @@ void set_wifichancycle(uint8_t val[]) { channel = WIFI_CHANNEL_MIN; ESP_LOGI(TAG, "Remote command: set Wifi channel hopping to off"); } + #else + // TODO update libpax configuration + #endif } void set_blescantime(uint8_t val[]) { cfg.blescantime = val[0]; + #ifndef LIBPAX ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds", cfg.blescantime / float(100)); // stop & restart BLE scan task to apply new parameter @@ -99,6 +104,9 @@ void set_blescantime(uint8_t val[]) { stop_BLEscan(); start_BLEscan(); } + #else + // TODO update libpax configuration + #endif } void set_countmode(uint8_t val[]) { @@ -240,20 +248,28 @@ void set_loraadr(uint8_t val[]) { void set_blescan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val[0] ? "on" : "off"); + #ifndef LIBPAX macs_ble = 0; // clear BLE counter cfg.blescan = val[0] ? 1 : 0; if (cfg.blescan) start_BLEscan(); else stop_BLEscan(); + #else + // TODO update libpax configuration + #endif } void set_wifiscan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set WIFI scanner to %s", val[0] ? "on" : "off"); + #ifndef LIBPAX macs_wifi = 0; // clear WIFI counter cfg.wifiscan = val[0] ? 1 : 0; switch_wifi_sniffer(cfg.wifiscan); + #else + // TODO update libpax configuration + #endif } void set_wifiant(uint8_t val[]) { diff --git a/src/reset.cpp b/src/reset.cpp index 350e33cb6..c15b6c7b6 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -107,6 +107,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { sendTimer.detach(); // switch off radio and other power consuming hardware +#ifndef LIBPAX #if (WIFICOUNTER) switch_wifi_sniffer(0); #endif @@ -114,6 +115,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { stop_BLEscan(); btStop(); #endif +#endif #if (HAS_SDS011) sds011_sleep(void); #endif diff --git a/src/senddata.cpp b/src/senddata.cpp index f5738552d..28ccd28ea 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -57,7 +57,11 @@ void SendPayload(uint8_t port) { // write data to sdcard, if present #if (HAS_SDCARD) if (port == COUNTERPORT) { +#ifndef LIBPAX sdcardWriteData(macs_wifi, macs_ble +#else + sdcardWriteData(libpax_macs_wifi, libpax_macs_ble +#endif #if (COUNT_ENS) , cwa_report() @@ -87,9 +91,20 @@ void sendData() { case COUNT_DATA: payload.reset(); #if !(PAYLOAD_OPENSENSEBOX) +#ifndef LIBPAX payload.addCount(macs_wifi, MAC_SNIFF_WIFI); - if (cfg.blescan) +#else + ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); + payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); +#endif + if (cfg.blescan) { +#ifndef LIBPAX payload.addCount(macs_ble, MAC_SNIFF_BLE); +#else + ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); + payload.addCount(libpax_macs_ble, MAC_SNIFF_BLE); +#endif + } #endif #if (HAS_GPS) if (GPSPORT == COUNTERPORT) { @@ -102,9 +117,19 @@ void sendData() { } #endif #if (PAYLOAD_OPENSENSEBOX) +#ifndef LIBPAX payload.addCount(macs_wifi, MAC_SNIFF_WIFI); - if (cfg.blescan) +#else + ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); + payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); +#endif + if (cfg.blescan) { +#ifndef LIBPAX payload.addCount(macs_ble, MAC_SNIFF_BLE); +#else + ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); + payload.addCount(libpax_macs_ble, MAC_SNIFF_BLE); +#endif #endif #if (HAS_SDS011) sds011_store(&sds_status); diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index a4399a055..2632bae4b 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -1,3 +1,4 @@ +#ifndef LIBPAX // Basic Config #include "globals.h" #include "wifiscan.h" @@ -102,4 +103,5 @@ void switch_wifi_sniffer(uint8_t state) { esp_wifi_set_promiscuous(false); esp_wifi_stop(); } -} \ No newline at end of file +} +#endif \ No newline at end of file From 93dcace8bd7861767c0b43295a3d430ba505a4d5 Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Wed, 3 Mar 2021 14:13:40 +0100 Subject: [PATCH 04/68] Tryout for on demand switching between BLE and Wifi counting in libpax --- include/libpax_helpers.h | 10 ++++++++++ src/libpax_helpers.cpp | 18 ++++++++++++++++++ src/main.cpp | 16 ++-------------- src/rcommand.cpp | 29 +++++++++++++++++++++++++---- 4 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 include/libpax_helpers.h create mode 100644 src/libpax_helpers.cpp diff --git a/include/libpax_helpers.h b/include/libpax_helpers.h new file mode 100644 index 000000000..dd3c02387 --- /dev/null +++ b/include/libpax_helpers.h @@ -0,0 +1,10 @@ +#ifndef _LIBPAX_HELPERS_H +#define _LIBPAX_HELPERS_H + +#include "globals.h" +#include +#include + +void init_libpax(); + +#endif \ No newline at end of file diff --git a/src/libpax_helpers.cpp b/src/libpax_helpers.cpp new file mode 100644 index 000000000..b5693d653 --- /dev/null +++ b/src/libpax_helpers.cpp @@ -0,0 +1,18 @@ +#include "libpax_helpers.h" + +// libpax payload +#ifdef LIBPAX +struct count_payload_t count_from_libpax; +uint16_t volatile libpax_macs_ble, libpax_macs_wifi; + +void process_count(void) { + printf("pax: %d; %d; %d;\n", count_from_libpax.pax, count_from_libpax.wifi_count, count_from_libpax.ble_count); + libpax_macs_ble = count_from_libpax.ble_count; + libpax_macs_wifi = count_from_libpax.wifi_count; +} + +void init_libpax() { + libpax_counter_init(process_count, &count_from_libpax, 60*1000, 1); + libpax_counter_start(); +} +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c03e136f6..5195e99ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,6 +86,7 @@ triggers pps 1 sec impulse // Basic Config #include "main.h" +#include "libpax_helpers.h" configData_t cfg; // struct holds current device configuration char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message @@ -96,8 +97,6 @@ uint8_t volatile channel = WIFI_CHANNEL_MIN; // channel rotation counter uint8_t volatile rf_load = 0; // RF traffic indicator #ifndef LIBPAX uint16_t volatile macs_wifi = 0, macs_ble = 0; // globals for display -#else -uint16_t volatile libpax_macs_ble, libpax_macs_wifi; #endif hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; @@ -119,16 +118,6 @@ TimeChangeRule myDST = DAYLIGHT_TIME; TimeChangeRule mySTD = STANDARD_TIME; Timezone myTZ(myDST, mySTD); -// libpax payload -#ifdef LIBPAX -struct count_payload_t count_from_libpax; - -void process_count(void) { - printf("pax: %d; %d; %d;\n", count_from_libpax.pax, count_from_libpax.wifi_count, count_from_libpax.ble_count); - libpax_macs_ble = count_from_libpax.ble_count; - libpax_macs_wifi = count_from_libpax.wifi_count; -} -#endif // local Tag for logging static const char TAG[] = __FILE__; @@ -340,8 +329,7 @@ ESP_LOGI(TAG, "Starting libpax..."); if(config_update != 0) { ESP_LOGE(TAG, "Error in libpax configuration."); } else { - libpax_counter_init(process_count, &count_from_libpax, 60*1000, 1); - libpax_counter_start(); + init_libpax(); } } else { ESP_LOGE(TAG, "Error in libpax configuration: Wifi and BLE are not supported at the same time!"); diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 353d25c04..ae0d99b76 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -1,6 +1,7 @@ // Basic Config #include "globals.h" #include "rcommand.h" +#include "libpax_helpers.h" // Local logging tag static const char TAG[] = __FILE__; @@ -248,27 +249,47 @@ void set_loraadr(uint8_t val[]) { void set_blescan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val[0] ? "on" : "off"); + cfg.blescan = val[0] ? 1 : 0; #ifndef LIBPAX macs_ble = 0; // clear BLE counter - cfg.blescan = val[0] ? 1 : 0; if (cfg.blescan) start_BLEscan(); else stop_BLEscan(); #else - // TODO update libpax configuration + if(cfg.blescan) { + cfg.wifiscan = 0; + // Restart of libpax while switching scannings does not work atm + // libpax_counter_stop(); + // libpax_config_t current_config; + // libpax_get_current_config(¤t_config); + // current_config.wificounter = 0; + // current_config.blecounter = 1; + // libpax_update_config(¤t_config); + // init_libpax(); + } #endif } void set_wifiscan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set WIFI scanner to %s", val[0] ? "on" : "off"); + cfg.wifiscan = val[0] ? 1 : 0; #ifndef LIBPAX macs_wifi = 0; // clear WIFI counter - cfg.wifiscan = val[0] ? 1 : 0; switch_wifi_sniffer(cfg.wifiscan); #else - // TODO update libpax configuration + if(cfg.wifiscan) { + cfg.blescan = 0; + // Restart of libpax while switching scannings does not work atm + // libpax_counter_stop(); + // libpax_config_t current_config; + // libpax_get_current_config(¤t_config); + // current_config.wificounter = 1; + // current_config.blecounter = 0; + // libpax_update_config(¤t_config); + // init_libpax(); + } #endif } From d9cdeddecfc0c2bc2c0f9b2727189e47f6b558e6 Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Thu, 18 Mar 2021 09:46:52 +0100 Subject: [PATCH 05/68] Reenable BLE Wifi switching for libpax --- include/cyclic.h | 2 +- include/globals.h | 2 +- include/senddata.h | 2 +- platformio_orig.ini | 2 +- src/blecsan.cpp | 2 +- src/cyclic.cpp | 2 +- src/display.cpp | 8 +++---- src/libpax_helpers.cpp | 2 +- src/main.cpp | 49 +++++++++++++++++++--------------------- src/paxcounter_orig.conf | 2 +- src/rcommand.cpp | 40 ++++++++++++-------------------- src/reset.cpp | 2 +- src/senddata.cpp | 10 ++++---- src/wifiscan.cpp | 2 +- 14 files changed, 57 insertions(+), 70 deletions(-) diff --git a/include/cyclic.h b/include/cyclic.h index 0fbbc6ca8..03d64b18f 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -12,7 +12,7 @@ #include "sdcard.h" #include "macsniff.h" #include "reset.h" -#ifdef LIBPAX +#if LIBPAX #include #endif diff --git a/include/globals.h b/include/globals.h index 272d8b572..950d21b17 100644 --- a/include/globals.h +++ b/include/globals.h @@ -141,7 +141,7 @@ extern uint8_t volatile channel; // wifi channel rotation counter extern uint8_t volatile rf_load; // RF traffic indicator extern uint8_t batt_level; // display value extern uint16_t volatile macs_wifi, macs_ble; // display values -#ifdef LIBPAX +#if LIBPAX extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values #endif extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC diff --git a/include/senddata.h b/include/senddata.h index 98cd4c5d0..ea56ac99d 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -14,7 +14,7 @@ #include "corona.h" #endif -#if (LIBPAX) +#if LIBPAX #include extern struct count_payload_t count_from_libpax; diff --git a/platformio_orig.ini b/platformio_orig.ini index 86e1f0813..31840a8ab 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -89,7 +89,7 @@ lib_deps_basic = lewisxhe/AXP202X_Library @ ^1.1.3 geeksville/esp32-micro-sdcard @ ^0.1.1 256dpi/MQTT @ ^2.4.7 - https://github.com/dbSuS/libpax + https://github.com/dbSuS/libpax#v0.1.1 lib_deps_all = ${common.lib_deps_basic} ${common.lib_deps_lora} diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 071a80072..139f7e743 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -1,4 +1,4 @@ -#ifndef LIBPAX +#if !(LIBPAX) // some code snippets taken from // https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 61185f0ff..40729f3fd 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -136,7 +136,7 @@ uint32_t getFreeRAM() { void reset_counters() { #if ((WIFICOUNTER) || (BLECOUNTER)) -#ifndef LIBPAX +#if !(LIBPAX) macs.clear(); // clear all macs container macs_wifi = 0; macs_ble = 0; diff --git a/src/display.cpp b/src/display.cpp index 711bdc958..e2e1c02c6 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -262,7 +262,7 @@ void dp_drawPage(time_t t, bool nextpage) { #if ((WIFICOUNTER) && (BLECOUNTER)) if (cfg.wifiscan) -#ifndef LIBPAX +#if !(LIBPAX) dp_printf("WIFI:%-5d", macs_wifi); #else dp_printf("WIFI:%-5d", libpax_macs_wifi); @@ -275,7 +275,7 @@ void dp_drawPage(time_t t, bool nextpage) { dp_printf(" CWA:%-5d", cwa_report()); else #endif -#ifndef LIBPAX +#if !(LIBPAX) dp_printf("BLTH:%-5d", macs_ble); #else dp_printf("BLTH:%-5d", libpax_macs_ble); @@ -284,7 +284,7 @@ void dp_drawPage(time_t t, bool nextpage) { dp_printf(" BLTH:off"); #elif ((WIFICOUNTER) && (!BLECOUNTER)) if (cfg.wifiscan) -#ifndef LIBPAX +#if !(LIBPAX) dp_printf("WIFI:%-5d", macs_wifi); #else dp_printf("WIFI:%-5d", libpax_macs_wifi); @@ -293,7 +293,7 @@ void dp_drawPage(time_t t, bool nextpage) { dp_printf("WIFI:off"); #elif ((!WIFICOUNTER) && (BLECOUNTER)) if (cfg.blescan) -#ifndef LIBPAX +#if !(LIBPAX) dp_printf("BLTH:%-5d", macs_ble); #else dp_printf("BLTH:%-5d", libpax_macs_ble); diff --git a/src/libpax_helpers.cpp b/src/libpax_helpers.cpp index b5693d653..b02f93d02 100644 --- a/src/libpax_helpers.cpp +++ b/src/libpax_helpers.cpp @@ -1,7 +1,7 @@ #include "libpax_helpers.h" // libpax payload -#ifdef LIBPAX +#if LIBPAX struct count_payload_t count_from_libpax; uint16_t volatile libpax_macs_ble, libpax_macs_wifi; diff --git a/src/main.cpp b/src/main.cpp index 5195e99ac..4a86064eb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -91,11 +91,11 @@ triggers pps 1 sec impulse configData_t cfg; // struct holds current device configuration char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message uint8_t batt_level = 0; // display value -#ifndef LIBPAX +#if !(LIBPAX) uint8_t volatile channel = WIFI_CHANNEL_MIN; // channel rotation counter #endif uint8_t volatile rf_load = 0; // RF traffic indicator -#ifndef LIBPAX +#if !(LIBPAX) uint16_t volatile macs_wifi = 0, macs_ble = 0; // globals for display #endif @@ -304,35 +304,31 @@ void setup() { if (RTC_runmode == RUNMODE_MAINTENANCE) start_boot_menu(); -#ifndef LIBPAX +#if !(LIBPAX) // start mac processing task ESP_LOGI(TAG, "Starting MAC processor..."); macQueueInit(); #else -ESP_LOGI(TAG, "Starting libpax..."); + ESP_LOGI(TAG, "Starting libpax..."); #if (defined WIFICOUNTER || defined BLECOUNTER) struct libpax_config_t configuration; libpax_default_config(&configuration); ESP_LOGI(TAG, "BLESCAN: %d", cfg.blescan); ESP_LOGI(TAG, "WIFISCAN: %d", cfg.wifiscan); - if(!(cfg.blescan && cfg.wifiscan)) { - configuration.wificounter = cfg.wifiscan; - configuration.blecounter = cfg.blescan; - - configuration.wifi_channel_map = WIFI_CHANNEL_ALL; - configuration.wifi_channel_switch_interval = cfg.wifichancycle; - configuration.wifi_rssi_threshold = cfg.rssilimit; - - configuration.blescantime = cfg.blescantime; - - int config_update = libpax_update_config(&configuration); - if(config_update != 0) { - ESP_LOGE(TAG, "Error in libpax configuration."); - } else { - init_libpax(); - } + configuration.wificounter = cfg.wifiscan; + configuration.blecounter = cfg.blescan; + + configuration.wifi_channel_map = WIFI_CHANNEL_ALL; + configuration.wifi_channel_switch_interval = cfg.wifichancycle; + configuration.wifi_rssi_threshold = cfg.rssilimit; + + configuration.blescantime = cfg.blescantime; + + int config_update = libpax_update_config(&configuration); + if(config_update != 0) { + ESP_LOGE(TAG, "Error in libpax configuration."); } else { - ESP_LOGE(TAG, "Error in libpax configuration: Wifi and BLE are not supported at the same time!"); + init_libpax(); } #endif #endif @@ -345,7 +341,7 @@ ESP_LOGI(TAG, "Starting libpax..."); // or remove bluetooth stack from RAM, if option bluetooth is not compiled #if (BLECOUNTER) strcat_P(features, " BLE"); -#ifndef LIBPAX +#if !(LIBPAX) if (cfg.blescan) { ESP_LOGI(TAG, "Starting Bluetooth..."); start_BLEscan(); @@ -354,14 +350,15 @@ ESP_LOGI(TAG, "Starting libpax..."); #endif #else // remove bluetooth stack to gain more free memory -#ifndef LIBPAX +#if !(LIBPAX) btStop(); -#endif esp_bt_mem_release(ESP_BT_MODE_BTDM); esp_coex_preference_set( ESP_COEX_PREFER_WIFI); // configure Wifi/BT coexist lib #endif +#endif + // initialize gps #if (HAS_GPS) strcat_P(features, " GPS"); @@ -466,7 +463,7 @@ ESP_LOGI(TAG, "Starting libpax..."); #if (WIFICOUNTER) strcat_P(features, " WIFI"); -#ifndef LIBPAX +#if !(LIBPAX) // install wifi driver in RAM and start channel hopping wifi_sniffer_init(); // start wifi sniffing, if enabled @@ -485,7 +482,7 @@ ESP_LOGI(TAG, "Starting libpax..."); // initialize salt value using esp_random() called by random() in // arduino-esp32 core. Note: do this *after* wifi has started, since // function gets it's seed from RF noise -#ifndef LIBPAX +#if !(LIBPAX) reset_counters(); #endif diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index 83f8b3767..638962a7a 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -30,7 +30,7 @@ #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle // Use libpax instead of default counting algorithms -#define LIBPAX 1 +#define LIBPAX 0 // Corona Exposure Notification Service(ENS) counter #define COUNT_ENS 1 // count found number of devices which advertise Exposure Notification Service diff --git a/src/rcommand.cpp b/src/rcommand.cpp index ae0d99b76..1c49f53e7 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -97,7 +97,7 @@ void set_wifichancycle(uint8_t val[]) { void set_blescantime(uint8_t val[]) { cfg.blescantime = val[0]; - #ifndef LIBPAX + #if !(LIBPAX) ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds", cfg.blescantime / float(100)); // stop & restart BLE scan task to apply new parameter @@ -250,24 +250,19 @@ void set_loraadr(uint8_t val[]) { void set_blescan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val[0] ? "on" : "off"); cfg.blescan = val[0] ? 1 : 0; - #ifndef LIBPAX + #if !(LIBPAX) macs_ble = 0; // clear BLE counter if (cfg.blescan) start_BLEscan(); else stop_BLEscan(); #else - if(cfg.blescan) { - cfg.wifiscan = 0; - // Restart of libpax while switching scannings does not work atm - // libpax_counter_stop(); - // libpax_config_t current_config; - // libpax_get_current_config(¤t_config); - // current_config.wificounter = 0; - // current_config.blecounter = 1; - // libpax_update_config(¤t_config); - // init_libpax(); - } + libpax_counter_stop(); + libpax_config_t current_config; + libpax_get_current_config(¤t_config); + current_config.blecounter = cfg.blescan; + libpax_update_config(¤t_config); + init_libpax(); #endif } @@ -275,21 +270,16 @@ void set_wifiscan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set WIFI scanner to %s", val[0] ? "on" : "off"); cfg.wifiscan = val[0] ? 1 : 0; - #ifndef LIBPAX + #if !(LIBPAX) macs_wifi = 0; // clear WIFI counter switch_wifi_sniffer(cfg.wifiscan); #else - if(cfg.wifiscan) { - cfg.blescan = 0; - // Restart of libpax while switching scannings does not work atm - // libpax_counter_stop(); - // libpax_config_t current_config; - // libpax_get_current_config(¤t_config); - // current_config.wificounter = 1; - // current_config.blecounter = 0; - // libpax_update_config(¤t_config); - // init_libpax(); - } + libpax_counter_stop(); + libpax_config_t current_config; + libpax_get_current_config(¤t_config); + current_config.wificounter = cfg.wifiscan; + libpax_update_config(¤t_config); + init_libpax(); #endif } diff --git a/src/reset.cpp b/src/reset.cpp index c15b6c7b6..4691b7e69 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -107,7 +107,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { sendTimer.detach(); // switch off radio and other power consuming hardware -#ifndef LIBPAX +#if !(LIBPAX) #if (WIFICOUNTER) switch_wifi_sniffer(0); #endif diff --git a/src/senddata.cpp b/src/senddata.cpp index 28ccd28ea..4c1ef4d92 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -57,7 +57,7 @@ void SendPayload(uint8_t port) { // write data to sdcard, if present #if (HAS_SDCARD) if (port == COUNTERPORT) { -#ifndef LIBPAX +#if !(LIBPAX) sdcardWriteData(macs_wifi, macs_ble #else sdcardWriteData(libpax_macs_wifi, libpax_macs_ble @@ -91,14 +91,14 @@ void sendData() { case COUNT_DATA: payload.reset(); #if !(PAYLOAD_OPENSENSEBOX) -#ifndef LIBPAX +#if !(LIBPAX) payload.addCount(macs_wifi, MAC_SNIFF_WIFI); #else ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); #endif if (cfg.blescan) { -#ifndef LIBPAX +#if !(LIBPAX) payload.addCount(macs_ble, MAC_SNIFF_BLE); #else ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); @@ -117,14 +117,14 @@ void sendData() { } #endif #if (PAYLOAD_OPENSENSEBOX) -#ifndef LIBPAX +#if !(LIBPAX) payload.addCount(macs_wifi, MAC_SNIFF_WIFI); #else ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); #endif if (cfg.blescan) { -#ifndef LIBPAX +#if !(LIBPAX) payload.addCount(macs_ble, MAC_SNIFF_BLE); #else ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index 2632bae4b..f601489d6 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -1,4 +1,4 @@ -#ifndef LIBPAX +#if !(LIBPAX) // Basic Config #include "globals.h" #include "wifiscan.h" From 3c4b2218e4fe5a4992975318794c488c04ce30f8 Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Thu, 4 Mar 2021 16:02:24 +0100 Subject: [PATCH 06/68] paxexpress integration --- build.py | 2 +- lib/BintrayClient/src/BintrayClient.cpp | 9 +++------ src/ota.cpp | 2 -- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/build.py b/build.py index 0d4e24bc7..ff7bd1619 100644 --- a/build.py +++ b/build.py @@ -118,7 +118,7 @@ def publish_bintray(source, target, env): firmware_path = str(source[0]) firmware_name = basename(firmware_path) url = "/".join([ - "https://api.bintray.com", "content", + "https://pax.express", "content", user, repository, package, version, firmware_name ]) diff --git a/lib/BintrayClient/src/BintrayClient.cpp b/lib/BintrayClient/src/BintrayClient.cpp index 34fa357fb..5c3012908 100644 --- a/lib/BintrayClient/src/BintrayClient.cpp +++ b/lib/BintrayClient/src/BintrayClient.cpp @@ -24,12 +24,9 @@ BintrayClient::BintrayClient(const String &user, const String &repository, const String &package) : m_user(user), m_repo(repository), m_package(package), - m_storage_host("dl.bintray.com"), - m_api_host("api.bintray.com") + m_storage_host("pax.express"), + m_api_host("pax.express") { - m_certificates.emplace_back("cloudfront.net", CLOUDFRONT_API_ROOT_CA); - m_certificates.emplace_back("akamai.bintray.com", BINTRAY_AKAMAI_ROOT_CA); - m_certificates.emplace_back("bintray.com", BINTRAY_API_ROOT_CA); } String BintrayClient::getUser() const @@ -83,7 +80,7 @@ String BintrayClient::requestHTTPContent(const String &url) const { String payload; HTTPClient http; - http.begin(url, getCertificate(url)); + http.begin(url); int httpCode = http.GET(); if (httpCode > 0) diff --git a/src/ota.cpp b/src/ota.cpp index bf4a41e04..2d80c23dd 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -150,7 +150,6 @@ int do_ota_update() { WiFiClientSecure client; - client.setCACert(bintray.getCertificate(currentHost)); client.setTimeout(RESPONSE_TIMEOUT_MS); if (!client.connect(currentHost.c_str(), port)) { @@ -162,7 +161,6 @@ int do_ota_update() { while (redirect) { if (currentHost != prevHost) { client.stop(); - client.setCACert(bintray.getCertificate(currentHost)); if (!client.connect(currentHost.c_str(), port)) { ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s", currentHost.c_str()); From 6b9d6b33b37c0c9cb0721db7c8220ab88462ddb8 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 22 Mar 2021 21:40:24 +0100 Subject: [PATCH 07/68] add reset after rcommand 09 02 --- README.md | 4 ++-- src/rcommand.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eb47ccfa5..1cd859c49 100644 --- a/README.md +++ b/README.md @@ -437,8 +437,8 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0x09 reset functions (send this command UNconfirmed only to avoid boot loops!) 0 = restart device (coldstart) - 1 = reset MAC counter to zero - 2 = reset device to factory settings + 1 = zeroize MAC counter + 2 = reset device to factory settings and restart device 3 = flush send queues 4 = restart device (warmstart) 8 = reboot device to maintenance mode (local web server) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index e4b009b35..af0f38b78 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -20,8 +20,9 @@ void set_reset(uint8_t val[]) { reset_counters(); // clear macs break; case 2: // reset device to factory settings - ESP_LOGI(TAG, "Remote command: reset device to factory settings"); + ESP_LOGI(TAG, "Remote command: reset device to factory settings and restart"); eraseConfig(); + do_reset(false); break; case 3: // reset send queues ESP_LOGI(TAG, "Remote command: flush send queue"); From 6a3fcd919136f733e1318ea217d3e450ed672804 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 22 Mar 2021 22:38:08 +0100 Subject: [PATCH 08/68] pax.express migration (work in progress) --- README.md | 4 ++-- platformio_orig.ini | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1cd859c49..f5a1dd3b9 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ To configure OTAA, leave `#define LORA_ABP` deactivated (commented). To use ABP, 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 via WiFi, either from a remote https server, or locally via WebUI. If you want to use a remote server, 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. +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 via WiFi, either from a remote https server, or locally via WebUI. If you want to use a remote server, you need a PAX.express repository. Enter your PAX.express credentials in ota.conf. If you don't need wireless firmware updates just rename ota.sample.conf to ota.conf. # Building @@ -125,7 +125,7 @@ The LoPy/LoPy4/FiPy board needs to be set manually. See these The original Pycom firmware is not needed, so there is no need to update it before flashing Paxcounter. Just flash the compiled paxcounter binary (.elf file) on your LoPy/LoPy4/FiPy. If you later want to go back to the Pycom firmware, download the firmware from Pycom and flash it over. - **over the air (OTA), download via WiFi:** -After the ESP32 board is initially flashed and has joined a LoRaWAN network, the firmware can update itself by OTA. This process is kicked off by sending a remote control command (see below) via LoRaWAN to the board. The board then tries to connect via WiFi to a cloud service (JFrog Bintray), checks for update, and if available downloads the binary and reboots with it. If something goes wrong during this process, the board reboots back to the current version. Prerequisites for OTA are: 1. You own a Bintray repository, 2. you pushed the update binary to the Bintray repository, 3. internet access via encrypted (WPA2) WiFi is present at the board's site, 4. WiFi credentials were set in ota.conf and initially flashed to the board. Step 2 runs automated, just enter the credentials in ota.conf and set `upload_protocol = custom` in platformio.ini. Then press build and lean back watching platformio doing build and upload. +After the ESP32 board is initially flashed and has joined a LoRaWAN network, the firmware can update itself by OTA. This process is kicked off by sending a remote control command (see below) via LoRaWAN to the board. The board then tries to connect via WiFi to a cloud service (PAX.express), checks for update, and if available downloads the binary and reboots with it. If something goes wrong during this process, the board reboots back to the current version. Prerequisites for OTA are: 1. You own a PAX.express repository, 2. you pushed the update binary to your PAX.express repository, 3. internet access via encrypted (WPA2) WiFi is present at the board's site, 4. WiFi credentials were set in ota.conf and initially flashed to the board. Step 2 runs automated, just enter the credentials in ota.conf and set `upload_protocol = custom` in platformio.ini. Then press build and lean back watching platformio doing build and upload. - **over the air (OTA), upload via WiFi:** If option *BOOTMENU* is defined in `paxcounter.conf`, the ESP32 board will try to connect to a known WiFi access point each time cold starting (after a power cycle or a reset), using the WiFi credentials given in `ota.conf`. Once connected to the WiFi it will fire up a simple webserver, providing a bootstrap menu waiting for a user interaction (pressing "START" button in menu). This process will time out after *BOOTDELAY* seconds, ensuring booting the device to runmode. Once a user interaction in bootstrap menu was detected, the timeout will be extended to *BOOTTIMEOUT* seconds. During this time a firmware upload can be performed manually by user, e.g. using a smartphone in tethering mode providing the firmware upload file. diff --git a/platformio_orig.ini b/platformio_orig.ini index 31840a8ab..8837ef4c5 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -41,7 +41,7 @@ halfile = ttgobeam10.h [platformio] ; upload firmware to board with usb cable default_envs = usb -; upload firmware to a jfrog bintray repository +; upload firmware to pax.express repository ;default_envs = ota ; use latest versions of libraries ;default_envs = dev From 8c9325320a5fc9e89f560aa425ff08f1321ee016 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 22 Mar 2021 23:10:07 +0100 Subject: [PATCH 09/68] pax.express migration 2 (work in progress) --- build.py | 36 +++++++++++++++++----------------- lib/BintrayClient/library.json | 2 +- src/ota.cpp | 10 +++++----- src/ota_sample.conf | 8 ++++---- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/build.py b/build.py index ff7bd1619..ced50f0a5 100644 --- a/build.py +++ b/build.py @@ -86,35 +86,35 @@ key, value = line.partition("=")[::2] mykeys[key.strip()] = str(value).strip() -# usage of bintray: see https://github.com/r0oland/bintray-secure-ota +# usage of paxexpress: see https://github.com/paxexpress/docs -# get bintray user credentials from ota key file -user = mykeys["BINTRAY_USER"] -repository = mykeys["BINTRAY_REPO"] -apitoken = mykeys["BINTRAY_API_TOKEN"] +# get paxexpress credentials from ota key file +user = mykeys["PAXEXPRESS_USER"] +repository = mykeys["PAXEXPRESS_REPO"] +apitoken = mykeys["PAXEXPRESS_API_TOKEN"] -# get bintray upload parameters from platformio environment +# get paxexpress upload parameters from platformio environment version = config.get("common", "release_version") package, dummy = halconfig.partition(".")[::2] -# put bintray user credentials to platformio environment -env.Replace(BINTRAY_USER=user) -env.Replace(BINTRAY_REPO=repository) -env.Replace(BINTRAY_API_TOKEN=apitoken) +# put paxexpress user credentials to platformio environment +env.Replace(PAXEXPRESS_USER=user) +env.Replace(PAXEXPRESS_REPO=repository) +env.Replace(PAXEXPRESS_API_TOKEN=apitoken) # get runtime credentials and put them to compiler directive env.Append(BUILD_FLAGS=[ u'-DWIFI_SSID=\\"' + mykeys["OTA_WIFI_SSID"] + '\\"', u'-DWIFI_PASS=\\"' + mykeys["OTA_WIFI_PASS"] + '\\"', - u'-DBINTRAY_USER=\\"' + mykeys["BINTRAY_USER"] + '\\"', - u'-DBINTRAY_REPO=\\"' + mykeys["BINTRAY_REPO"] + '\\"', - u'-DBINTRAY_PACKAGE=\\"' + package + '\\"', + u'-DPAXEXPRESS_USER=\\"' + mykeys["PAXEXPRESS_USER"] + '\\"', + u'-DPAXEXPRESS_REPO=\\"' + mykeys["PAXEXPRESS_REPO"] + '\\"', + u'-DPAXEXPRESS_PACKAGE=\\"' + package + '\\"', u'-DARDUINO_LMIC_PROJECT_CONFIG_H=' + lmicconfig, u'-I \"' + srcdir + '\"' ]) -# function for pushing new firmware to bintray storage using API -def publish_bintray(source, target, env): +# function for pushing new firmware to paxexpress storage using API +def publish_paxexpress(source, target, env): firmware_path = str(source[0]) firmware_name = basename(firmware_path) url = "/".join([ @@ -122,7 +122,7 @@ def publish_bintray(source, target, env): user, repository, package, version, firmware_name ]) - print("Uploading {0} to Bintray. Version: {1}".format( + print("Uploading {0} to PAX.express. Version: {1}".format( firmware_name, version)) print(url) @@ -145,9 +145,9 @@ def publish_bintray(source, target, env): ("%s\n%s" % (r.status_code, r.text) if r else str(e))) env.Exit(1) - print("The firmware has been successfuly published at Bintray.com!") + print("Firmware has been successfully published at PAX.express!") # put build file name and upload command to platformio environment env.Replace( PROGNAME="firmware_" + package + "_v%s" % version, - UPLOADCMD=publish_bintray) + UPLOADCMD=publish_paxexpress) diff --git a/lib/BintrayClient/library.json b/lib/BintrayClient/library.json index 677cec5c8..1f36ebac8 100644 --- a/lib/BintrayClient/library.json +++ b/lib/BintrayClient/library.json @@ -16,7 +16,7 @@ "include": "bintray-secure-ota/lib/BintrayClient" }, "dependencies": { - "ArduinoJson": "^5.13.1" + "ArduinoJson": "^6" }, "version": "1.0.0", "frameworks": "arduino", diff --git a/src/ota.cpp b/src/ota.cpp index ad11822c6..db02d04fd 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -21,8 +21,8 @@ using namespace std; -const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE); -// usage of bintray: see https://github.com/r0oland/bintray-secure-ota +const BintrayClient paxexpress(PAXEXPRESS_USER, PAXEXPRESS_REPO, PAXEXPRESS_PACKAGE); +// usage of paxexpress: see https://github.com/paxexpress/docs // Connection port (HTTPS) const int port = 443; @@ -121,7 +121,7 @@ int do_ota_update() { if (WiFi.status() != WL_CONNECTED) return 1; - const String latest = bintray.getLatestVersion(); + const String latest = paxexpress.getLatestVersion(); if (latest.length() == 0) { ESP_LOGI(TAG, "Could not fetch info on latest firmware"); @@ -138,14 +138,14 @@ int do_ota_update() { ota_display(3, "**", ""); if (WiFi.status() != WL_CONNECTED) return 1; - String firmwarePath = bintray.getBinaryPath(latest); + String firmwarePath = paxexpress.getBinaryPath(latest); if (!firmwarePath.endsWith(".bin")) { ESP_LOGI(TAG, "Unsupported binary format"); ota_display(3, " E", "file type error"); return -1; } - String currentHost = bintray.getStorageHost(); + String currentHost = paxexpress.getStorageHost(); String prevHost = currentHost; WiFiClientSecure client; diff --git a/src/ota_sample.conf b/src/ota_sample.conf index a10d79dc5..7a6425dca 100644 --- a/src/ota_sample.conf +++ b/src/ota_sample.conf @@ -2,7 +2,7 @@ OTA_WIFI_SSID = MyHomeWifi OTA_WIFI_PASS = FooBar42! -[bintray] -BINTRAY_USER = MyBintrayUser -BINTRAY_REPO = MyBintrayRepo -BINTRAY_API_TOKEN = 3894a7a51d70c6523c1b7479261c34845ebf7878 \ No newline at end of file +[paxexpress] +PAXEXPRESS_USER = MyPaxexpressUser +PAXEXPRESS_REPO = MyPaxexpressRepo +PAXEXPRESS_API_TOKEN = 3894a7a51d70c6523c1b7479261c34845ebf7878 \ No newline at end of file From 5836e713b5c1cb02b7176cd1edcfa09647fd1fea Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 22 Mar 2021 23:39:40 +0100 Subject: [PATCH 10/68] update onebitdisplay lib --- platformio_orig.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio_orig.ini b/platformio_orig.ini index 8837ef4c5..be4f08fa5 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -64,7 +64,7 @@ display_library = ; set by build.py and taken from hal file lib_deps_lora = mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 lib_deps_display = - bitbank2/OneBitDisplay @ 1.9.0 + bitbank2/OneBitDisplay @ 1.9.1 bitbank2/BitBang_I2C @ ^2.1.3 ricmoo/QRCode @ ^0.0.1 bodmer/TFT_eSPI @ ^2.3.51 From 59bc06daebb702f5ce33f99b68b763e23eb04678 Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Tue, 23 Mar 2021 15:51:30 +0100 Subject: [PATCH 11/68] Fixed compiling problem by bumping libpax version --- platformio_orig.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio_orig.ini b/platformio_orig.ini index be4f08fa5..485b53c23 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -89,7 +89,7 @@ lib_deps_basic = lewisxhe/AXP202X_Library @ ^1.1.3 geeksville/esp32-micro-sdcard @ ^0.1.1 256dpi/MQTT @ ^2.4.7 - https://github.com/dbSuS/libpax#v0.1.1 + https://github.com/dbSuS/libpax#v0.1.2 lib_deps_all = ${common.lib_deps_basic} ${common.lib_deps_lora} From 0ef522cf1bde4ece4285696d1cfdd6928d43ae7d Mon Sep 17 00:00:00 2001 From: Michael Solinski Date: Tue, 23 Mar 2021 15:52:20 +0100 Subject: [PATCH 12/68] Restated project defaults for BLECOUNTER --- src/paxcounter_orig.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index 638962a7a..05246eabb 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -20,7 +20,7 @@ // MAC sniffing parameters #define MACFILTER 1 // set to 0 if you want to scan all devices, 1 to scan only devices with random MACs (aka smartphones) [default = 1] -#define BLECOUNTER 0 // set to 0 if you do not want to install the BLE sniffer +#define BLECOUNTER 1 // set to 0 if you do not want to install the BLE sniffer #define WIFICOUNTER 1 // set to 0 if you do not want to install the WIFI sniffer #define MAC_QUEUE_SIZE 50 // size of MAC processing buffer (number of MACs) [default = 50] @@ -30,7 +30,7 @@ #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle // Use libpax instead of default counting algorithms -#define LIBPAX 0 +#define LIBPAX 0 // set to 1 to enable libpax // Corona Exposure Notification Service(ENS) counter #define COUNT_ENS 1 // count found number of devices which advertise Exposure Notification Service From 049048dd6e9afebc4391ca490894c5a30efec3a8 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 23 Mar 2021 23:08:35 +0100 Subject: [PATCH 13/68] libpax integration 1 (work in progress) --- src/cyclic.cpp | 6 ++++-- src/reset.cpp | 4 +++- src/wifiscan.cpp | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 40729f3fd..49d87f8b7 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -29,9 +29,11 @@ void doHousekeeping() { ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(irqHandlerTask), eTaskGetState(irqHandlerTask)); +#if !(LIBPAX) ESP_LOGD(TAG, "MACprocessor %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(macProcessTask), eTaskGetState(macProcessTask)); +#endif ESP_LOGD(TAG, "Rcommand interpreter %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(rcmdTask), eTaskGetState(rcmdTask)); #if (HAS_LORA) @@ -136,11 +138,11 @@ uint32_t getFreeRAM() { void reset_counters() { #if ((WIFICOUNTER) || (BLECOUNTER)) -#if !(LIBPAX) +#if !(LIBPAX) macs.clear(); // clear all macs container macs_wifi = 0; macs_ble = 0; - renew_salt(); // get new salt + renew_salt(); // get new salt #endif #ifdef HAS_DISPLAY dp_plotCurve(0, true); diff --git a/src/reset.cpp b/src/reset.cpp index 4691b7e69..84e734e84 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -107,7 +107,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { sendTimer.detach(); // switch off radio and other power consuming hardware -#if !(LIBPAX) +#if !(LIBPAX) #if (WIFICOUNTER) switch_wifi_sniffer(0); #endif @@ -120,8 +120,10 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { sds011_sleep(void); #endif +#if !(LIBPAX) // stop MAC processing vTaskDelete(macProcessTask); +#endif // halt interrupts accessing i2c bus mask_user_IRQ(); diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index f601489d6..fd9295191 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -104,4 +104,4 @@ void switch_wifi_sniffer(uint8_t state) { esp_wifi_stop(); } } -#endif \ No newline at end of file +#endif // !(LIBPAX) \ No newline at end of file From d43657be232c9b2b87aa2ff917c5810b97213697 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 23 Mar 2021 23:08:51 +0100 Subject: [PATCH 14/68] onebitdisplay lib update --- platformio_orig.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio_orig.ini b/platformio_orig.ini index be4f08fa5..58c6fd929 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -64,7 +64,7 @@ display_library = ; set by build.py and taken from hal file lib_deps_lora = mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 lib_deps_display = - bitbank2/OneBitDisplay @ 1.9.1 + bitbank2/OneBitDisplay @ ^1.9.1 bitbank2/BitBang_I2C @ ^2.1.3 ricmoo/QRCode @ ^0.0.1 bodmer/TFT_eSPI @ ^2.3.51 From 2854f2dd13a2b4c53c92548ebaf24aa07f91c8be Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 23 Mar 2021 23:09:52 +0100 Subject: [PATCH 15/68] ota.cpp: stabilize and speedup wifi login --- src/ota.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/ota.cpp b/src/ota.cpp index db02d04fd..4c8b7140e 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -21,7 +21,8 @@ using namespace std; -const BintrayClient paxexpress(PAXEXPRESS_USER, PAXEXPRESS_REPO, PAXEXPRESS_PACKAGE); +const BintrayClient paxexpress(PAXEXPRESS_USER, PAXEXPRESS_REPO, + PAXEXPRESS_PACKAGE); // usage of paxexpress: see https://github.com/paxexpress/docs // Connection port (HTTPS) @@ -41,6 +42,15 @@ inline String getHeaderValue(String header, String headerName) { void start_ota_update() { + uint8_t mac[6]; + char clientId[20]; + + // hash 6 byte MAC to 4 byte hash + esp_eth_get_mac(mac); + const uint32_t hashedmac = myhash((const char *)mac, 6); + snprintf(clientId, 20, "paxcounter_%08x", hashedmac); + const char *host = clientId; + switch_LED(LED_ON); // init display @@ -65,8 +75,28 @@ void start_ota_update() { ESP_LOGI(TAG, "Starting Wifi OTA update"); ota_display(1, "**", WIFI_SSID); + WiFi.disconnect(true); + WiFi.config(INADDR_NONE, INADDR_NONE, + INADDR_NONE); // call is only a workaround for bug in WiFi class + // see https://github.com/espressif/arduino-esp32/issues/806 + WiFi.setHostname(host); WiFi.mode(WIFI_STA); + WiFi.begin(); + + // Connect to WiFi network + // workaround applied here to bypass WIFI_AUTH failure + // see https://github.com/espressif/arduino-esp32/issues/2501 + + // 1st try WiFi.begin(WIFI_SSID, WIFI_PASS); + while (WiFi.status() == WL_DISCONNECTED) { + delay(500); + } + // 2nd try + if (WiFi.status() != WL_CONNECTED) { + WiFi.begin(WIFI_SSID, WIFI_PASS); + delay(500); + } uint8_t i = WIFI_MAX_TRY; int ret = 1; // 0 = finished, 1 = retry, -1 = abort @@ -74,7 +104,6 @@ void start_ota_update() { while (i--) { ESP_LOGI(TAG, "Trying to connect to %s, attempt %u of %u", WIFI_SSID, WIFI_MAX_TRY - i, WIFI_MAX_TRY); - delay(10000); // wait for stable connect if (WiFi.status() == WL_CONNECTED) { // we now have wifi connection and try to do an OTA over wifi update ESP_LOGI(TAG, "Connected to %s", WIFI_SSID); @@ -89,6 +118,7 @@ void start_ota_update() { if (WiFi.status() == WL_CONNECTED) goto end; // OTA update finished or OTA max attemps reached } + delay(10000); // wait for stable connect WiFi.reconnect(); } From cc7508aa412daa6dc408b59550b2c668246d8319 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 25 Mar 2021 10:30:38 +0100 Subject: [PATCH 16/68] libpax-only initial push --- include/blescan.h | 17 --- include/cyclic.h | 6 +- include/globals.h | 14 +- include/macsniff.h | 25 ---- include/main.h | 2 - include/mallocator.h | 54 ------- include/rcommand.h | 3 - include/senddata.h | 8 +- include/wifiscan.h | 19 --- src/blecsan.cpp | 304 --------------------------------------- src/cyclic.cpp | 11 -- src/display.cpp | 16 --- src/libpax_helpers.cpp | 11 +- src/lorawan.cpp | 12 ++ src/macsniff.cpp | 198 ------------------------- src/main.cpp | 66 +-------- src/paxcounter_orig.conf | 3 - src/rcommand.cpp | 55 ++----- src/reset.cpp | 14 -- src/senddata.cpp | 162 +++++++++------------ src/sensor.cpp | 1 - src/wifiscan.cpp | 107 -------------- 22 files changed, 107 insertions(+), 1001 deletions(-) delete mode 100644 include/blescan.h delete mode 100644 include/macsniff.h delete mode 100644 include/mallocator.h delete mode 100644 include/wifiscan.h delete mode 100644 src/blecsan.cpp delete mode 100644 src/macsniff.cpp delete mode 100644 src/wifiscan.cpp diff --git a/include/blescan.h b/include/blescan.h deleted file mode 100644 index e9252e886..000000000 --- a/include/blescan.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _BLESCAN_H -#define _BLESCAN_H - -#include "globals.h" -#include "macsniff.h" - -// Bluetooth specific includes -#include -#include -#include -#include // needed for BLE_ADDR types, do not remove -#include - -void start_BLEscan(void); -void stop_BLEscan(void); - -#endif \ No newline at end of file diff --git a/include/cyclic.h b/include/cyclic.h index 03d64b18f..26ce3fd59 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -1,6 +1,7 @@ #ifndef _CYCLIC_H #define _CYCLIC_H +#include #include "globals.h" #include "senddata.h" #include "rcommand.h" @@ -10,11 +11,8 @@ #include "display.h" #include "sds011read.h" #include "sdcard.h" -#include "macsniff.h" #include "reset.h" -#if LIBPAX -#include -#endif +#include "led.h" extern Ticker cyclicTimer; diff --git a/include/globals.h b/include/globals.h index 950d21b17..d63e5b7fc 100644 --- a/include/globals.h +++ b/include/globals.h @@ -15,7 +15,6 @@ #include #include #include -#include "mallocator.h" #include #define _bit(b) (1U << (b)) @@ -100,13 +99,6 @@ typedef struct { uint8_t Message[PAYLOAD_BUFFER_SIZE]; } MessageBuffer_t; -// Struct for MAC processing queue -typedef struct { - uint8_t mac[6]; - int8_t rssi; - snifftype_t sniff_type; -} MacBuffer_t; - typedef struct { int32_t latitude; int32_t longitude; @@ -131,7 +123,6 @@ typedef struct { float pm25; } sdsStatus_t; -extern std::set, Mallocator> macs; extern std::array::iterator it; extern std::array beacons; @@ -140,15 +131,12 @@ extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer extern uint8_t volatile channel; // wifi channel rotation counter extern uint8_t volatile rf_load; // RF traffic indicator extern uint8_t batt_level; // display value -extern uint16_t volatile macs_wifi, macs_ble; // display values -#if LIBPAX extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values -#endif extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC extern timesource_t timeSource; extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ; extern SemaphoreHandle_t I2Caccess; -extern TaskHandle_t irqHandlerTask, ClockTask, macProcessTask; +extern TaskHandle_t irqHandlerTask, ClockTask; extern TimerHandle_t WifiChanTimer; extern Timezone myTZ; diff --git a/include/macsniff.h b/include/macsniff.h deleted file mode 100644 index 6bc346efe..000000000 --- a/include/macsniff.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef _MACSNIFF_H -#define _MACSNIFF_H - -// ESP32 Functions -#include - -// Hash function for scrambling MAC addresses -#include "hash.h" -#include "senddata.h" -#include "cyclic.h" -#include "led.h" - -#if (COUNT_ENS) -#include "corona.h" -#endif - -uint32_t renew_salt(void); -uint64_t macConvert(uint8_t *paddr); -esp_err_t macQueueInit(void); -void mac_process(void *pvParameters); -void IRAM_ATTR mac_add(uint8_t *paddr, int8_t rssi, snifftype_t sniff_type); -uint16_t mac_analyze(MacBuffer_t MacBuffer); -void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb); - -#endif diff --git a/include/main.h b/include/main.h index 2a5bd1045..8fcc6b7ec 100644 --- a/include/main.h +++ b/include/main.h @@ -8,8 +8,6 @@ #include "globals.h" #include "reset.h" #include "i2c.h" -#include "blescan.h" -#include "wifiscan.h" #include "configmanager.h" #include "cyclic.h" #include "beacon_array.h" diff --git a/include/mallocator.h b/include/mallocator.h deleted file mode 100644 index 7d32d409d..000000000 --- a/include/mallocator.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - -This Mallocator code was taken from: - -CppCon2014 Presentations -STL Features And Implementation Techniques -Stephan T. Lavavej - -https://github.com/CppCon/CppCon2014 - -*/ - - -#ifndef _MALLOCATOR_H -#define _MALLOCATOR_H - -#include // size_t, malloc, free -#include // bad_alloc, bad_array_new_length -#include "esp32-hal-psram.h" // ps_malloc - -template struct Mallocator { - typedef T value_type; - Mallocator() noexcept {} // default ctor not required - template Mallocator(const Mallocator &) noexcept {} - template bool operator==(const Mallocator &) const noexcept { - return true; - } - template bool operator!=(const Mallocator &) const noexcept { - return false; - } - - T *allocate(const size_t n) const { - if (n == 0) { - return nullptr; - } - if (n > static_cast(-1) / sizeof(T)) { - throw std::bad_array_new_length(); - } - -#ifndef BOARD_HAS_PSRAM - void *const pv = malloc(n * sizeof(T)); -#else - void *const pv = ps_malloc(n * sizeof(T)); -#endif - - if (!pv) { - throw std::bad_alloc(); - } - return static_cast(pv); - } - void deallocate(T *const p, size_t) const noexcept { free(p); } -}; - -#endif \ No newline at end of file diff --git a/include/rcommand.h b/include/rcommand.h index 8149d668f..4d59d75f4 100644 --- a/include/rcommand.h +++ b/include/rcommand.h @@ -8,12 +8,9 @@ #include "configmanager.h" #include "lorawan.h" #include "sensor.h" -#include "macsniff.h" -#include "wifiscan.h" #include "cyclic.h" #include "timekeeper.h" #include "timesync.h" -#include "blescan.h" // maximum number of elements in rcommand interpreter queue #define RCMD_QUEUE_SIZE 5 diff --git a/include/senddata.h b/include/senddata.h index ea56ac99d..f8666770c 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -1,6 +1,7 @@ #ifndef _SENDDATA_H #define _SENDDATA_H +#include #include "spislave.h" #include "mqttclient.h" #include "cyclic.h" @@ -9,18 +10,11 @@ #include "display.h" #include "sdcard.h" - #if (COUNT_ENS) #include "corona.h" #endif -#if LIBPAX -#include - extern struct count_payload_t count_from_libpax; -#endif - - extern Ticker sendTimer; void SendPayload(uint8_t port); diff --git a/include/wifiscan.h b/include/wifiscan.h deleted file mode 100644 index 53a414c31..000000000 --- a/include/wifiscan.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef _WIFISCAN_H -#define _WIFISCAN_H - -// ESP32 Functions -#include - -#include "hash.h" // Hash function for scrambling MAC addresses -#include "antenna.h" // code for switching wifi antennas -#include "macsniff.h" - -extern TimerHandle_t WifiChanTimer; - -void wifi_sniffer_init(void); -void switch_wifi_sniffer(uint8_t state); -void IRAM_ATTR wifi_sniffer_packet_handler(void *buff, - wifi_promiscuous_pkt_type_t type); -void switchWifiChannel(TimerHandle_t xTimer); - -#endif \ No newline at end of file diff --git a/src/blecsan.cpp b/src/blecsan.cpp deleted file mode 100644 index 139f7e743..000000000 --- a/src/blecsan.cpp +++ /dev/null @@ -1,304 +0,0 @@ -#if !(LIBPAX) -// some code snippets taken from -// https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner - -#include "blescan.h" - -#define BT_BD_ADDR_HEX(addr) \ - addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] - -// local Tag for logging -static const char TAG[] = "bluetooth"; - -#ifdef VERBOSE -const char *bt_addr_t_to_string(esp_ble_addr_type_t type) { - switch (type) { - case BLE_ADDR_TYPE_PUBLIC: - return "BLE_ADDR_TYPE_PUBLIC"; - case BLE_ADDR_TYPE_RANDOM: - return "BLE_ADDR_TYPE_RANDOM"; - case BLE_ADDR_TYPE_RPA_PUBLIC: - return "BLE_ADDR_TYPE_RPA_PUBLIC"; - case BLE_ADDR_TYPE_RPA_RANDOM: - return "BLE_ADDR_TYPE_RPA_RANDOM"; - default: - return "Unknown addr_t"; - } -} // bt_addr_t_to_string - -const char *btsig_gap_type(uint32_t gap_type) { - switch (gap_type) { - case 0x01: - return "Flags"; - case 0x02: - return "Incomplete List of 16-bit Service Class UUIDs"; - case 0x03: - return "Complete List of 16-bit Service Class UUIDs"; - case 0x04: - return "Incomplete List of 32-bit Service Class UUIDs"; - case 0x05: - return "Complete List of 32-bit Service Class UUIDs"; - case 0x06: - return "Incomplete List of 128-bit Service Class UUIDs"; - case 0x07: - return "Complete List of 128-bit Service Class UUIDs"; - case 0x08: - return "Shortened Local Name"; - case 0x09: - return "Complete Local Name"; - case 0x0A: - return "Tx Power Level"; - case 0x0D: - return "Class of Device"; - case 0x0E: - return "Simple Pairing Hash C/C-192"; - case 0x0F: - return "Simple Pairing Randomizer R/R-192"; - case 0x10: - return "Device ID/Security Manager TK Value"; - case 0x11: - return "Security Manager Out of Band Flags"; - case 0x12: - return "Slave Connection Interval Range"; - case 0x14: - return "List of 16-bit Service Solicitation UUIDs"; - case 0x1F: - return "List of 32-bit Service Solicitation UUIDs"; - case 0x15: - return "List of 128-bit Service Solicitation UUIDs"; - case 0x16: - return "Service Data - 16-bit UUID"; - case 0x20: - return "Service Data - 32-bit UUID"; - case 0x21: - return "Service Data - 128-bit UUID"; - case 0x22: - return "LE Secure Connections Confirmation Value"; - case 0x23: - return "LE Secure Connections Random Value"; - case 0x24: - return "URI"; - case 0x25: - return "Indoor Positioning"; - case 0x26: - return "Transport Discovery Data"; - case 0x17: - return "Public Target Address"; - case 0x18: - return "Random Target Address"; - case 0x19: - return "Appearance"; - case 0x1A: - return "Advertising Interval"; - case 0x1B: - return "LE Bluetooth Device Address"; - case 0x1C: - return "LE Role"; - case 0x1D: - return "Simple Pairing Hash C-256"; - case 0x1E: - return "Simple Pairing Randomizer R-256"; - case 0x3D: - return "3D Information Data"; - case 0xFF: - return "Manufacturer Specific Data"; - - default: - return "Unknown type"; - } -} // btsig_gap_type -#endif - -// using IRAM_ATTR here to speed up callback function -IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, - esp_ble_gap_cb_param_t *param) { - - esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param; - -#if (COUNT_ENS) - // UUID of Exposure Notification Service (ENS) - // https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf - static const char ensMagicBytes[] = "\x16\x6f\xfd"; -#endif - -#ifdef VERBOSE - ESP_LOGV(TAG, "BT payload rcvd -> type: 0x%.2x -> %s", *p->scan_rst.ble_adv, - btsig_gap_type(*p->scan_rst.ble_adv)); -#endif - - switch (event) { - case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: - // restart scan - esp_ble_gap_start_scanning(BLESCANTIME); - break; - - case ESP_GAP_BLE_SCAN_RESULT_EVT: - // evaluate scan results - if (p->scan_rst.search_evt == - ESP_GAP_SEARCH_INQ_CMPL_EVT) // Inquiry complete, scan is done - { // restart scan - esp_ble_gap_start_scanning(BLESCANTIME); - return; - } - - if (p->scan_rst.search_evt == - ESP_GAP_SEARCH_INQ_RES_EVT) // Inquiry result for a peer device - { // evaluate sniffed packet -#ifdef VERBOSE - ESP_LOGV(TAG, "Device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", - BT_BD_ADDR_HEX(p->scan_rst.bda)); - ESP_LOGV(TAG, "Addr_type : %s", - bt_addr_t_to_string(p->scan_rst.ble_addr_type)); - ESP_LOGV(TAG, "RSSI : %d", p->scan_rst.rssi); -#endif - -#if (MACFILTER) - if ((p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RANDOM) || - (p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RPA_RANDOM)) { -#ifdef VERBOSE - ESP_LOGV(TAG, "BT device filtered"); -#endif - break; - } -#endif - - // add this device mac to processing queue - -#if (COUNT_ENS) - // check for ens signature - if (cfg.enscount) { - if (strstr((const char *)p->scan_rst.ble_adv, ensMagicBytes) != NULL) - mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, - MAC_SNIFF_BLE_ENS); - else - mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE); - } -#else - mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE); -#endif - - /* to be improved in macfilter: - // you can search for elements in the payload using the - // function esp_ble_resolve_adv_data() - // - // Like this, that scans for the "Complete name" (looking inside the - payload buffer) - // uint8_t len; - // uint8_t *data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, - ESP_BLE_AD_TYPE_NAME_CMPL, &len); - - filter BLE devices using their advertisements to get filter alternative - to vendor OUI if macfiltering is on, we ... - - want to count: mobile phones and tablets - - don't want to count: beacons, peripherals (earphones, headsets, - printers), cars and machines see - https://github.com/nkolban/ESP32_BLE_Arduino/blob/master/src/BLEAdvertisedDevice.cpp - - http://www.libelium.com/products/meshlium/smartphone-detection/ - - https://www.question-defense.com/2013/01/12/bluetooth-cod-bluetooth-class-of-deviceclass-of-service-explained - - https://www.bluetooth.com/specifications/assigned-numbers/baseband - - "The Class of Device (CoD) in case of Bluetooth which allows us to - differentiate the type of device (smartphone, handsfree, computer, - LAN/network AP). With this parameter we can differentiate among - pedestrians and vehicles." - - */ - - } // evaluate sniffed packet - break; - - default: - break; - } // switch -} // gap_callback_handler - -esp_err_t register_ble_callback(bool unregister = false) { - - if (unregister) { - - ESP_LOGI(TAG, "Unregister GAP callback..."); - esp_ble_gap_stop_scanning(); - esp_ble_gap_register_callback(NULL); - - } - - else { - - ESP_LOGI(TAG, "Register GAP callback..."); - - // This function is called when gap event occurs, such as scan result. - // register the scan callback function to the gap module - esp_ble_gap_register_callback(&gap_callback_handler); - - static esp_ble_scan_params_t ble_scan_params = { - .scan_type = BLE_SCAN_TYPE_PASSIVE, - .own_addr_type = BLE_ADDR_TYPE_RANDOM, - .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL, - - /* - #if (MACFILTER) - .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_WLIST_PRA_DIR, - // ADV_IND, ADV_NONCONN_IND, ADV_SCAN_IND packets are used - for broadcasting - // data in broadcast applications (e.g., Beacons), so we - don't want them in - // macfilter mode - #else - .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL, - #endif - */ - - .scan_interval = - (uint16_t)(cfg.blescantime * 10 / 0.625), // Time = N * 0.625 msec - .scan_window = - (uint16_t)(BLESCANWINDOW / 0.625), // Time = N * 0.625 msec - .scan_duplicate = BLE_SCAN_DUPLICATE_ENABLE}; - - ESP_LOGI(TAG, "Set GAP scan parameters"); - - // This function is called to set scan parameters. - esp_ble_gap_set_scan_params(&ble_scan_params); - } - - return ESP_OK; - -} // register_ble_callback - -void start_BLEscan(void) { -#if (BLECOUNTER) - ESP_LOGI(TAG, "Initializing bluetooth scanner ..."); - // Initialize BT controller to allocate task and other resource. - if (btStart()) { // enable bt_controller - esp_coex_preference_set(ESP_COEX_PREFER_BT); - esp_bluedroid_init(); - esp_bluedroid_enable(); - // Register callback function for capturing bluetooth packets - register_ble_callback(false); - ESP_LOGI(TAG, "Bluetooth scanner started"); - } else { - ESP_LOGE(TAG, "Bluetooth controller start failed. Resetting device"); - do_reset(true); - } -#endif // BLECOUNTER -} // start_BLEscan - -void stop_BLEscan(void) { -#if (BLECOUNTER) - ESP_LOGI(TAG, "Shutting down bluetooth scanner ..."); - register_ble_callback(true); // unregister capture function - ESP_LOGD(TAG, "bluedroid disable..."); - esp_bluedroid_disable(); - ESP_LOGD(TAG, "bluedroid deinit..."); - esp_bluedroid_deinit(); - if (!btStop()) { // disable bt_controller - ESP_LOGE(TAG, "Bluetooth controller stop failed. Resetting device"); - do_reset(true); - } - esp_coex_preference_set(ESP_COEX_PREFER_WIFI); - ESP_LOGI(TAG, "Bluetooth scanner stopped"); -#endif // BLECOUNTER -} // stop_BLEscan -#endif \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 49d87f8b7..dd1150079 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -29,11 +29,6 @@ void doHousekeeping() { ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(irqHandlerTask), eTaskGetState(irqHandlerTask)); -#if !(LIBPAX) - ESP_LOGD(TAG, "MACprocessor %d bytes left | Taskstate = %d", - uxTaskGetStackHighWaterMark(macProcessTask), - eTaskGetState(macProcessTask)); -#endif ESP_LOGD(TAG, "Rcommand interpreter %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(rcmdTask), eTaskGetState(rcmdTask)); #if (HAS_LORA) @@ -138,12 +133,6 @@ uint32_t getFreeRAM() { void reset_counters() { #if ((WIFICOUNTER) || (BLECOUNTER)) -#if !(LIBPAX) - macs.clear(); // clear all macs container - macs_wifi = 0; - macs_ble = 0; - renew_salt(); // get new salt -#endif #ifdef HAS_DISPLAY dp_plotCurve(0, true); #endif diff --git a/src/display.cpp b/src/display.cpp index e2e1c02c6..91ee570b2 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -262,11 +262,7 @@ void dp_drawPage(time_t t, bool nextpage) { #if ((WIFICOUNTER) && (BLECOUNTER)) if (cfg.wifiscan) -#if !(LIBPAX) - dp_printf("WIFI:%-5d", macs_wifi); -#else dp_printf("WIFI:%-5d", libpax_macs_wifi); -#endif else dp_printf("WIFI:off"); if (cfg.blescan) @@ -275,29 +271,17 @@ void dp_drawPage(time_t t, bool nextpage) { dp_printf(" CWA:%-5d", cwa_report()); else #endif -#if !(LIBPAX) - dp_printf("BLTH:%-5d", macs_ble); -#else dp_printf("BLTH:%-5d", libpax_macs_ble); -#endif else dp_printf(" BLTH:off"); #elif ((WIFICOUNTER) && (!BLECOUNTER)) if (cfg.wifiscan) -#if !(LIBPAX) - dp_printf("WIFI:%-5d", macs_wifi); -#else dp_printf("WIFI:%-5d", libpax_macs_wifi); -#endif else dp_printf("WIFI:off"); #elif ((!WIFICOUNTER) && (BLECOUNTER)) if (cfg.blescan) -#if !(LIBPAX) - dp_printf("BLTH:%-5d", macs_ble); -#else dp_printf("BLTH:%-5d", libpax_macs_ble); -#endif #if (COUNT_ENS) if (cfg.enscount) dp_printf("(CWA:%d)", cwa_report()); diff --git a/src/libpax_helpers.cpp b/src/libpax_helpers.cpp index b02f93d02..b637d226d 100644 --- a/src/libpax_helpers.cpp +++ b/src/libpax_helpers.cpp @@ -1,18 +1,17 @@ #include "libpax_helpers.h" // libpax payload -#if LIBPAX struct count_payload_t count_from_libpax; uint16_t volatile libpax_macs_ble, libpax_macs_wifi; void process_count(void) { - printf("pax: %d; %d; %d;\n", count_from_libpax.pax, count_from_libpax.wifi_count, count_from_libpax.ble_count); + printf("pax: %d; %d; %d;\n", count_from_libpax.pax, + count_from_libpax.wifi_count, count_from_libpax.ble_count); libpax_macs_ble = count_from_libpax.ble_count; libpax_macs_wifi = count_from_libpax.wifi_count; } void init_libpax() { - libpax_counter_init(process_count, &count_from_libpax, 60*1000, 1); - libpax_counter_start(); -} -#endif \ No newline at end of file + libpax_counter_init(process_count, &count_from_libpax, 60 * 1000, 1); + libpax_counter_start(); +} \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index fd601e979..b7a7fbddf 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -163,6 +163,18 @@ void os_getDevEui(u1_t *buf) { #if (VERBOSE) +// Display a key +void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb) { + const uint8_t *p; + char keystring[len + 1] = "", keybyte[3]; + for (uint8_t i = 0; i < len; i++) { + p = lsb ? key + len - i - 1 : key + i; + snprintf(keybyte, 3, "%02X", *p); + strncat(keystring, keybyte, 2); + } + ESP_LOGI(TAG, "%s: %s", name, keystring); +} + // Display OTAA keys void showLoraKeys(void) { // LMIC may not have used callback to fill diff --git a/src/macsniff.cpp b/src/macsniff.cpp deleted file mode 100644 index e031d36e4..000000000 --- a/src/macsniff.cpp +++ /dev/null @@ -1,198 +0,0 @@ - -// Basic Config -#include "globals.h" -#include "macsniff.h" - -// Local logging tag -static const char TAG[] = __FILE__; - -static QueueHandle_t MacQueue; -TaskHandle_t macProcessTask; - -static uint32_t salt = renew_salt(); - -uint32_t renew_salt(void) { - salt = esp_random(); - ESP_LOGV(TAG, "new salt = %04X", salt); - return salt; -} - -int8_t isBeacon(uint64_t mac) { - it = std::find(beacons.begin(), beacons.end(), mac); - if (it != beacons.end()) - return std::distance(beacons.begin(), it); - else - return -1; -} - -// Display a key -void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb) { - const uint8_t *p; - char keystring[len + 1] = "", keybyte[3]; - for (uint8_t i = 0; i < len; i++) { - p = lsb ? key + len - i - 1 : key + i; - snprintf(keybyte, 3, "%02X", *p); - strncat(keystring, keybyte, 2); - } - ESP_LOGI(TAG, "%s: %s", name, keystring); -} - -uint64_t macConvert(uint8_t *paddr) { - uint64_t *mac; - mac = (uint64_t *)paddr; - return (__builtin_bswap64(*mac) >> 16); -} - -esp_err_t macQueueInit() { - _ASSERT(MAC_QUEUE_SIZE > 0); - MacQueue = xQueueCreate(MAC_QUEUE_SIZE, sizeof(MacBuffer_t)); - if (MacQueue == 0) { - ESP_LOGE(TAG, "Could not create MAC processing queue. Aborting."); - return ESP_FAIL; - } - ESP_LOGI(TAG, "MAC processing queue created, size %d Bytes", - MAC_QUEUE_SIZE * sizeof(MacBuffer_t)); - - xTaskCreatePinnedToCore(mac_process, // task function - "mac_process", // name of task - 3072, // stack size of task - (void *)1, // parameter of the task - 1, // priority of the task - &macProcessTask, // task handle - 1); // CPU core - - return ESP_OK; -} - -// sniffed MAC processing task -void mac_process(void *pvParameters) { - _ASSERT((uint32_t)pvParameters == 1); // FreeRTOS check - - MacBuffer_t MacBuffer; - - while (1) { - - // fetch next or wait for incoming MAC from sniffing queue - if (xQueueReceive(MacQueue, &MacBuffer, portMAX_DELAY) != pdTRUE) { - ESP_LOGE(TAG, "Premature return from xQueueReceive() with no data!"); - continue; - } - - // update traffic indicator - rf_load = uxQueueMessagesWaiting(MacQueue); - // process fetched mac - mac_analyze(MacBuffer); - } - delay(2); // yield to CPU -} - -// enqueue message in MAC processing queue -void IRAM_ATTR mac_add(uint8_t *paddr, int8_t rssi, snifftype_t sniff_type) { - - MacBuffer_t MacBuffer; - - MacBuffer.rssi = rssi; - MacBuffer.sniff_type = sniff_type; - memcpy(MacBuffer.mac, paddr, 6); - - if (xQueueSendToBackFromISR(MacQueue, (void *)&MacBuffer, (TickType_t)0) != - pdPASS) - ESP_LOGW(TAG, "Dense radio traffic, packet lost!"); -} - -uint16_t mac_analyze(MacBuffer_t MacBuffer) { - - uint32_t *mac; // pointer to shortened 4 byte MAC - uint32_t saltedmac; - uint16_t hashedmac; - - if ((cfg.rssilimit) && - (MacBuffer.rssi < cfg.rssilimit)) { // rssi is negative value - ESP_LOGI(TAG, "%s RSSI %d -> ignoring (limit: %d)", - (MacBuffer.sniff_type == MAC_SNIFF_WIFI) ? "WIFI" : "BLTH", - MacBuffer.rssi, cfg.rssilimit); - return 0; - } - - // in beacon monitor mode check if seen MAC is a known beacon - if (cfg.monitormode) { - int8_t beaconID = isBeacon(macConvert(MacBuffer.mac)); - if (beaconID >= 0) { - ESP_LOGI(TAG, "Beacon ID#%d detected", beaconID); - blink_LED(COLOR_WHITE, 2000); - payload.reset(); - payload.addAlarm(MacBuffer.rssi, beaconID); - SendPayload(BEACONPORT); - } - }; - - // only last 3 MAC Address bytes are used for MAC address anonymization - // but since it's uint32 we take 4 bytes to avoid 1st value to be 0. - // this gets MAC in msb (= reverse) order, but doesn't matter for hashing it. - mac = (uint32_t *)(MacBuffer.mac + 2); - - // salt and hash MAC, and if new unique one, store identifier in container - // and increment counter on display - // https://en.wikipedia.org/wiki/MAC_Address_Anonymization - - // reversed 4 byte MAC added to current salt - saltedmac = *mac + salt; - - // hashed 4 byte MAC - // to save RAM, we use only lower 2 bytes of hash, since collisions don't - // matter in our use case - hashedmac = myhash((const char *)&saltedmac, 4); - - auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique - bool added = - newmac.second ? true : false; // true if hashed MAC is unique in container - - // Count only if MAC was not yet seen - if (added) { - - switch (MacBuffer.sniff_type) { - - case MAC_SNIFF_WIFI: - macs_wifi++; // increment Wifi MACs counter - blink_LED(COLOR_GREEN, 50); - break; - - case MAC_SNIFF_BLE: - macs_ble++; // increment BLE Macs counter - blink_LED(COLOR_MAGENTA, 50); - break; -#if (COUNT_ENS) - case MAC_SNIFF_BLE_ENS: - macs_ble++; // increment BLE Macs counter - cwa_mac_add(hashedmac); // process ENS beacon - blink_LED(COLOR_WHITE, 50); - break; -#endif - default: - break; - - } // switch - } // added - - // Log scan result - ESP_LOGV(TAG, - "%s %s RSSI %ddBi -> MAC %0x:%0x:%0x:%0x:%0x:%0x -> salted %04X" - " -> hashed %04X -> WiFi:%d " - "BLTH:%d " -#if (COUNT_ENS) - "(CWA:%d)" -#endif - "-> %d Bytes left", - added ? "new " : "known", - MacBuffer.sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH", - MacBuffer.rssi, MacBuffer.mac[0], MacBuffer.mac[1], MacBuffer.mac[2], - MacBuffer.mac[3], MacBuffer.mac[4], MacBuffer.mac[5], saltedmac, - hashedmac, macs_wifi, macs_ble, -#if (COUNT_ENS) - cwa_report(), -#endif - getFreeRAM()); - - // if an unknown Wifi or BLE mac was counted, return hash of this mac, else 0 - return (added ? hashedmac : 0); -} diff --git a/src/main.cpp b/src/main.cpp index 4a86064eb..fbf7780e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,6 @@ timesync_proc 1 3 processes realtime time sync requests irqhandler 1 2 cyclic tasks (i.e. displayrefresh) triggered by timers gpsloop 1 1 reads data from GPS via serial or i2c lorasendtask 1 1 feeds data from lora sendqueue to lmcic -macprocess 1 1 MAC analyzer loop rmcd_process 1 1 Remote command interpreter loop IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator @@ -91,13 +90,7 @@ triggers pps 1 sec impulse configData_t cfg; // struct holds current device configuration char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message uint8_t batt_level = 0; // display value -#if !(LIBPAX) -uint8_t volatile channel = WIFI_CHANNEL_MIN; // channel rotation counter -#endif -uint8_t volatile rf_load = 0; // RF traffic indicator -#if !(LIBPAX) -uint16_t volatile macs_wifi = 0, macs_ble = 0; // globals for display -#endif +uint8_t volatile rf_load = 0; // RF traffic indicator hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; @@ -106,10 +99,6 @@ SemaphoreHandle_t I2Caccess; bool volatile TimePulseTick = false; timesource_t timeSource = _unsynced; -// container holding unique MAC address hashes with Memory Alloctor using PSRAM, -// if present -DRAM_ATTR std::set, Mallocator> macs; - // initialize payload encoder PayloadConvert payload(PAYLOAD_BUFFER_SIZE); @@ -118,7 +107,6 @@ TimeChangeRule myDST = DAYLIGHT_TIME; TimeChangeRule mySTD = STANDARD_TIME; Timezone myTZ(myDST, mySTD); - // local Tag for logging static const char TAG[] = __FILE__; @@ -304,13 +292,8 @@ void setup() { if (RTC_runmode == RUNMODE_MAINTENANCE) start_boot_menu(); -#if !(LIBPAX) - // start mac processing task - ESP_LOGI(TAG, "Starting MAC processor..."); - macQueueInit(); -#else ESP_LOGI(TAG, "Starting libpax..."); -#if (defined WIFICOUNTER || defined BLECOUNTER) +#if (defined WIFICOUNTER || defined BLECOUNTER) struct libpax_config_t configuration; libpax_default_config(&configuration); ESP_LOGI(TAG, "BLESCAN: %d", cfg.blescan); @@ -325,39 +308,20 @@ void setup() { configuration.blescantime = cfg.blescantime; int config_update = libpax_update_config(&configuration); - if(config_update != 0) { + if (config_update != 0) { ESP_LOGE(TAG, "Error in libpax configuration."); } else { init_libpax(); } #endif -#endif - - // start rcommand processing task - ESP_LOGI(TAG, "Starting rcommand interpreter..."); - rcmd_init(); -// start BLE scan callback if BLE function is enabled in NVRAM configuration -// or remove bluetooth stack from RAM, if option bluetooth is not compiled #if (BLECOUNTER) strcat_P(features, " BLE"); -#if !(LIBPAX) - if (cfg.blescan) { - ESP_LOGI(TAG, "Starting Bluetooth..."); - start_BLEscan(); - } else - btStop(); -#endif -#else - // remove bluetooth stack to gain more free memory -#if !(LIBPAX) - btStop(); - esp_bt_mem_release(ESP_BT_MODE_BTDM); - esp_coex_preference_set( - ESP_COEX_PREFER_WIFI); // configure Wifi/BT coexist lib #endif -#endif + // start rcommand processing task + ESP_LOGI(TAG, "Starting rcommand interpreter..."); + rcmd_init(); // initialize gps #if (HAS_GPS) @@ -463,29 +427,11 @@ void setup() { #if (WIFICOUNTER) strcat_P(features, " WIFI"); -#if !(LIBPAX) - // install wifi driver in RAM and start channel hopping - wifi_sniffer_init(); - // start wifi sniffing, if enabled - if (cfg.wifiscan) { - ESP_LOGI(TAG, "Starting Wifi..."); - switch_wifi_sniffer(1); - } else - switch_wifi_sniffer(0); -#endif - #else // remove wifi driver from RAM, if option wifi not compiled esp_wifi_deinit(); #endif - // initialize salt value using esp_random() called by random() in - // arduino-esp32 core. Note: do this *after* wifi has started, since - // function gets it's seed from RF noise -#if !(LIBPAX) - reset_counters(); -#endif - // start state machine ESP_LOGI(TAG, "Starting Interrupt Handler..."); xTaskCreatePinnedToCore(irqHandler, // task function diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index 638962a7a..fdf874f4e 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -29,9 +29,6 @@ #define BLESCANWINDOW 80 // [milliseconds] scan window, see below, 3 .. 10240, default 80ms #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle -// Use libpax instead of default counting algorithms -#define LIBPAX 0 - // Corona Exposure Notification Service(ENS) counter #define COUNT_ENS 1 // count found number of devices which advertise Exposure Notification Service // set to 1 if you want to enable this function [default=0] diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 38d9d7d07..218f933ef 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -21,7 +21,8 @@ void set_reset(uint8_t val[]) { reset_counters(); // clear macs break; case 2: // reset device to factory settings - ESP_LOGI(TAG, "Remote command: reset device to factory settings and restart"); + ESP_LOGI(TAG, + "Remote command: reset device to factory settings and restart"); eraseConfig(); do_reset(false); break; @@ -74,41 +75,12 @@ void set_sleepcycle(uint8_t val[]) { void set_wifichancycle(uint8_t val[]) { cfg.wifichancycle = val[0]; - #ifndef LIBAPX - // update Wifi channel rotation timer period - if (cfg.wifichancycle > 0) { - if (xTimerIsTimerActive(WifiChanTimer) == pdFALSE) - xTimerStart(WifiChanTimer, (TickType_t)0); - xTimerChangePeriod(WifiChanTimer, pdMS_TO_TICKS(cfg.wifichancycle * 10), - 100); - ESP_LOGI( - TAG, - "Remote command: set Wifi channel hopping interval to %.1f seconds", - cfg.wifichancycle / float(100)); - } else { - xTimerStop(WifiChanTimer, (TickType_t)0); - esp_wifi_set_channel(WIFI_CHANNEL_MIN, WIFI_SECOND_CHAN_NONE); - channel = WIFI_CHANNEL_MIN; - ESP_LOGI(TAG, "Remote command: set Wifi channel hopping to off"); - } - #else // TODO update libpax configuration - #endif } void set_blescantime(uint8_t val[]) { cfg.blescantime = val[0]; - #if !(LIBPAX) - ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds", - cfg.blescantime / float(100)); - // stop & restart BLE scan task to apply new parameter - if (cfg.blescan) { - stop_BLEscan(); - start_BLEscan(); - } - #else - // TODO update libpax configuration - #endif + // TODO update libpax configuration } void set_countmode(uint8_t val[]) { @@ -202,12 +174,18 @@ void set_sensor(uint8_t val[]) { #endif } +uint64_t macConvert(uint8_t *paddr) { + uint64_t *mac; + mac = (uint64_t *)paddr; + return (__builtin_bswap64(*mac) >> 16); +} + void set_beacon(uint8_t val[]) { uint8_t id = val[0]; // use first parameter as beacon storage id memmove(val, val + 1, 6); // strip off storage id beacons[id] = macConvert(val); // store beacon MAC in array ESP_LOGI(TAG, "Remote command: set beacon ID#%d", id); - printKey("MAC", val, 6, false); // show beacon MAC + //printKey("MAC", val, 6, false); // show beacon MAC } void set_monitor(uint8_t val[]) { @@ -251,37 +229,24 @@ void set_loraadr(uint8_t val[]) { void set_blescan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val[0] ? "on" : "off"); cfg.blescan = val[0] ? 1 : 0; - #if !(LIBPAX) - macs_ble = 0; // clear BLE counter - if (cfg.blescan) - start_BLEscan(); - else - stop_BLEscan(); - #else libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.blecounter = cfg.blescan; libpax_update_config(¤t_config); init_libpax(); - #endif } void set_wifiscan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set WIFI scanner to %s", val[0] ? "on" : "off"); cfg.wifiscan = val[0] ? 1 : 0; - #if !(LIBPAX) - macs_wifi = 0; // clear WIFI counter - switch_wifi_sniffer(cfg.wifiscan); - #else libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.wificounter = cfg.wifiscan; libpax_update_config(¤t_config); init_libpax(); - #endif } void set_wifiant(uint8_t val[]) { diff --git a/src/reset.cpp b/src/reset.cpp index 84e734e84..cbabc9d04 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -107,24 +107,10 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { sendTimer.detach(); // switch off radio and other power consuming hardware -#if !(LIBPAX) -#if (WIFICOUNTER) - switch_wifi_sniffer(0); -#endif -#if (BLECOUNTER) - stop_BLEscan(); - btStop(); -#endif -#endif #if (HAS_SDS011) sds011_sleep(void); #endif -#if !(LIBPAX) - // stop MAC processing - vTaskDelete(macProcessTask); -#endif - // halt interrupts accessing i2c bus mask_user_IRQ(); diff --git a/src/senddata.cpp b/src/senddata.cpp index 4c1ef4d92..12fe735c9 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -3,9 +3,7 @@ Ticker sendTimer; -void setSendIRQ() { - xTaskNotify(irqHandlerTask, SENDCYCLE_IRQ, eSetBits); -} +void setSendIRQ() { xTaskNotify(irqHandlerTask, SENDCYCLE_IRQ, eSetBits); } // put data to send in RTos Queues used for transmit over channels Lora and SPI void SendPayload(uint8_t port) { @@ -57,11 +55,7 @@ void SendPayload(uint8_t port) { // write data to sdcard, if present #if (HAS_SDCARD) if (port == COUNTERPORT) { -#if !(LIBPAX) - sdcardWriteData(macs_wifi, macs_ble -#else sdcardWriteData(libpax_macs_wifi, libpax_macs_ble -#endif #if (COUNT_ENS) , cwa_report() @@ -91,19 +85,11 @@ void sendData() { case COUNT_DATA: payload.reset(); #if !(PAYLOAD_OPENSENSEBOX) -#if !(LIBPAX) - payload.addCount(macs_wifi, MAC_SNIFF_WIFI); -#else ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); -#endif if (cfg.blescan) { -#if !(LIBPAX) - payload.addCount(macs_ble, MAC_SNIFF_BLE); -#else ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); payload.addCount(libpax_macs_ble, MAC_SNIFF_BLE); -#endif } #endif #if (HAS_GPS) @@ -117,125 +103,117 @@ void sendData() { } #endif #if (PAYLOAD_OPENSENSEBOX) -#if !(LIBPAX) - payload.addCount(macs_wifi, MAC_SNIFF_WIFI); -#else ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); -#endif if (cfg.blescan) { -#if !(LIBPAX) - payload.addCount(macs_ble, MAC_SNIFF_BLE); -#else ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); payload.addCount(libpax_macs_ble, MAC_SNIFF_BLE); #endif -#endif #if (HAS_SDS011) - sds011_store(&sds_status); - payload.addSDS(sds_status); -#endif - SendPayload(COUNTERPORT); - // clear counter if not in cumulative counter mode - if (cfg.countermode != 1) { - reset_counters(); // clear macs container and reset all counters - ESP_LOGI(TAG, "Counter cleared"); - } + sds011_store(&sds_status); + payload.addSDS(sds_status); +#endif + SendPayload(COUNTERPORT); + // clear counter if not in cumulative counter mode + if (cfg.countermode != 1) { + reset_counters(); // clear macs container and reset all counters + ESP_LOGI(TAG, "Counter cleared"); + } #ifdef HAS_DISPLAY - else - dp_plotCurve(macs.size(), true); + else + dp_plotCurve(macs.size(), true); #endif - break; + break; #endif #if (HAS_BME) - case MEMS_DATA: - payload.reset(); - payload.addBME(bme_status); - SendPayload(BMEPORT); - break; + case MEMS_DATA: + payload.reset(); + payload.addBME(bme_status); + SendPayload(BMEPORT); + break; #endif #if (HAS_GPS) - case GPS_DATA: - if (GPSPORT != COUNTERPORT) { - // send GPS position only if we have a fix - if (gps_hasfix()) { - gps_storelocation(&gps_status); - payload.reset(); - payload.addGPS(gps_status); - SendPayload(GPSPORT); - } else - ESP_LOGD(TAG, "No valid GPS position"); - } - break; + case GPS_DATA: + if (GPSPORT != COUNTERPORT) { + // send GPS position only if we have a fix + if (gps_hasfix()) { + gps_storelocation(&gps_status); + payload.reset(); + payload.addGPS(gps_status); + SendPayload(GPSPORT); + } else + ESP_LOGD(TAG, "No valid GPS position"); + } + break; #endif #if (HAS_SENSORS) #if (HAS_SENSOR_1) - case SENSOR1_DATA: - payload.reset(); - payload.addSensor(sensor_read(1)); - SendPayload(SENSOR1PORT); + case SENSOR1_DATA: + payload.reset(); + payload.addSensor(sensor_read(1)); + SendPayload(SENSOR1PORT); #if (COUNT_ENS) - if (cfg.countermode != 1) - cwa_clear(); + if (cfg.countermode != 1) + cwa_clear(); #endif - break; + break; #endif #if (HAS_SENSOR_2) - case SENSOR2_DATA: - payload.reset(); - payload.addSensor(sensor_read(2)); - SendPayload(SENSOR2PORT); - break; + case SENSOR2_DATA: + payload.reset(); + payload.addSensor(sensor_read(2)); + SendPayload(SENSOR2PORT); + break; #endif #if (HAS_SENSOR_3) - case SENSOR3_DATA: - payload.reset(); - payload.addSensor(sensor_read(3)); - SendPayload(SENSOR3PORT); - break; + case SENSOR3_DATA: + payload.reset(); + payload.addSensor(sensor_read(3)); + SendPayload(SENSOR3PORT); + break; #endif #endif #if (defined BAT_MEASURE_ADC || defined HAS_PMU) - case BATT_DATA: - payload.reset(); - payload.addVoltage(read_voltage()); - SendPayload(BATTPORT); - break; + case BATT_DATA: + payload.reset(); + payload.addVoltage(read_voltage()); + SendPayload(BATTPORT); + break; #endif - } // switch - bitmask &= ~mask; - mask <<= 1; - } // while (bitmask) -} // sendData() + } // switch + bitmask &= ~mask; + mask <<= 1; + } // while (bitmask) + } // sendData() -void flushQueues(void) { - rcmd_queuereset(); + void flushQueues(void) { + rcmd_queuereset(); #if (HAS_LORA) - lora_queuereset(); + lora_queuereset(); #endif #ifdef HAS_SPI - spi_queuereset(); + spi_queuereset(); #endif #ifdef HAS_MQTT - mqtt_queuereset(); + mqtt_queuereset(); #endif -} + } -bool allQueuesEmtpy(void) { - uint32_t rc = rcmd_queuewaiting(); + bool allQueuesEmtpy(void) { + uint32_t rc = rcmd_queuewaiting(); #if (HAS_LORA) - rc += lora_queuewaiting(); + rc += lora_queuewaiting(); #endif #ifdef HAS_SPI - rc += spi_queuewaiting(); + rc += spi_queuewaiting(); #endif #ifdef HAS_MQTT - rc += mqtt_queuewaiting(); + rc += mqtt_queuewaiting(); #endif - return (rc == 0) ? true : false; -} + return (rc == 0) ? true : false; + } diff --git a/src/sensor.cpp b/src/sensor.cpp index 559fd60d0..60c148881 100644 --- a/src/sensor.cpp +++ b/src/sensor.cpp @@ -5,7 +5,6 @@ #if (COUNT_ENS) #include "payload.h" #include "corona.h" -#include "macsniff.h" extern PayloadConvert payload; #endif diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp deleted file mode 100644 index fd9295191..000000000 --- a/src/wifiscan.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#if !(LIBPAX) -// Basic Config -#include "globals.h" -#include "wifiscan.h" - -// Local logging tag -static const char TAG[] = "wifi"; - -TimerHandle_t WifiChanTimer; - -typedef struct { - unsigned frame_ctrl : 16; - unsigned duration_id : 16; - uint8_t addr1[6]; // receiver address - uint8_t addr2[6]; // sender address - uint8_t addr3[6]; // filtering address - unsigned sequence_ctrl : 16; - uint8_t addr4[6]; // optional -} wifi_ieee80211_mac_hdr_t; - -typedef struct { - wifi_ieee80211_mac_hdr_t hdr; - uint8_t payload[0]; // network data ended with 4 bytes csum (CRC32) -} wifi_ieee80211_packet_t; - -// using IRAM_ATTR here to speed up callback function -IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, - wifi_promiscuous_pkt_type_t type) { - - const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff; - const wifi_ieee80211_packet_t *ipkt = - (wifi_ieee80211_packet_t *)ppkt->payload; - const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr; - -// process seen MAC -#if MACFILTER - // we guess it's a smartphone, if U/L bit #2 of MAC is set - // bit #2 = 1 -> local mac (randomized) / bit #2 = 0 -> universal mac - if ((hdr->addr2[0] & 0b10) == 0) - return; - else -#endif - mac_add((uint8_t *)hdr->addr2, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI); -} - -// Software-timer driven Wifi channel rotation callback function -void switchWifiChannel(TimerHandle_t xTimer) { - channel = - (channel % WIFI_CHANNEL_MAX) + 1; // rotate channel 1..WIFI_CHANNEL_MAX - esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); -} - -void wifi_sniffer_init(void) { - - wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN, - WIFI_CHANNEL_MAX, 100, - WIFI_COUNTRY_POLICY_MANUAL}; - - wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); - wifi_cfg.event_handler = NULL; // we don't need a wifi event handler - wifi_cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM - wifi_cfg.wifi_task_core_id = 0; // we want wifi task running on core 0 - - wifi_promiscuous_filter_t wifi_filter = {.filter_mask = - WIFI_PROMIS_FILTER_MASK_MGMT | - WIFI_PROMIS_FILTER_MASK_DATA}; - - esp_wifi_init(&wifi_cfg); // start Wifi task - esp_wifi_set_country(&wifi_country); // set locales for RF and channels - esp_wifi_set_storage(WIFI_STORAGE_RAM); - esp_wifi_set_mode(WIFI_MODE_NULL); - esp_wifi_set_ps(WIFI_PS_NONE); // no modem power saving - esp_wifi_set_promiscuous_filter(&wifi_filter); // set frame filter - esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler); - - // setup wifi channel hopping timer - WifiChanTimer = xTimerCreate("WifiChannelTimer", - (cfg.wifichancycle > 0) - ? pdMS_TO_TICKS(cfg.wifichancycle * 10) - : pdMS_TO_TICKS(50), - pdTRUE, (void *)0, switchWifiChannel); -} - -void switch_wifi_sniffer(uint8_t state) { - if (state) { -// start sniffer -#if (BLECOUNTER) - // workaround needed for ESP-IDF v3.3 - // see https://github.com/espressif/esp-idf/issues/5427 - esp_wifi_set_ps(WIFI_PS_MIN_MODEM); -#endif - esp_wifi_start(); - esp_wifi_set_promiscuous(true); - esp_wifi_set_channel(WIFI_CHANNEL_MIN, WIFI_SECOND_CHAN_NONE); - // start channel hopping timer - if (cfg.wifichancycle > 0) - xTimerStart(WifiChanTimer, (TickType_t)0); - } else { - // start channel hopping timer - if (xTimerIsTimerActive(WifiChanTimer) != pdFALSE) - xTimerStop(WifiChanTimer, (TickType_t)0); - // stop sniffer - esp_wifi_set_promiscuous(false); - esp_wifi_stop(); - } -} -#endif // !(LIBPAX) \ No newline at end of file From be0018aea4bc49468a247f994366508cdb325095 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 25 Mar 2021 11:09:28 +0100 Subject: [PATCH 17/68] update libpax 0.1.1->0.1.2 --- platformio_orig.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio_orig.ini b/platformio_orig.ini index 58c6fd929..980261c40 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -89,7 +89,7 @@ lib_deps_basic = lewisxhe/AXP202X_Library @ ^1.1.3 geeksville/esp32-micro-sdcard @ ^0.1.1 256dpi/MQTT @ ^2.4.7 - https://github.com/dbSuS/libpax#v0.1.1 + https://github.com/dbSuS/libpax#v0.1.2 lib_deps_all = ${common.lib_deps_basic} ${common.lib_deps_lora} From bad60592c37830bb688251abda1160ac62797c8e Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 25 Mar 2021 11:42:39 +0100 Subject: [PATCH 18/68] don't count sleep wakeups as restarts --- src/reset.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/reset.cpp b/src/reset.cpp index cbabc9d04..67deadf57 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -56,6 +56,7 @@ void do_after_reset(void) { case SW_CPU_RESET: // 0x0c Software reset CPU // keep previous set runmode (update / normal / maintenance) + RTC_restarts++; break; case DEEPSLEEP_RESET: // 0x05 Deep Sleep reset digital core @@ -84,10 +85,10 @@ void do_after_reset(void) { case RTCWDT_RTC_RESET: // 0x10 RTC Watch dog reset digital core and rtc mode default: RTC_runmode = RUNMODE_POWERCYCLE; + RTC_restarts++; break; } - RTC_restarts++; ESP_LOGI(TAG, "Starting Software v%s (runmode=%d / restarts=%d)", PROGVERSION, RTC_runmode, RTC_restarts); } From 5777df2c48033ac1d6b927e3a8ea87ba914ac57c Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 25 Mar 2021 14:03:13 +0100 Subject: [PATCH 19/68] introduce global var device clientId --- include/globals.h | 1 + src/boot.cpp | 8 -------- src/main.cpp | 9 +++++++++ src/mqttclient.cpp | 8 +------- src/ota.cpp | 7 ------- src/reset.cpp | 3 --- 6 files changed, 11 insertions(+), 25 deletions(-) diff --git a/include/globals.h b/include/globals.h index d63e5b7fc..eb4f58b45 100644 --- a/include/globals.h +++ b/include/globals.h @@ -127,6 +127,7 @@ extern std::array::iterator it; extern std::array beacons; extern configData_t cfg; // current device configuration +extern char clientId[20]; // unique clientID extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer extern uint8_t volatile channel; // wifi channel rotation counter extern uint8_t volatile rf_load; // RF traffic indicator diff --git a/src/boot.cpp b/src/boot.cpp index 889476d10..c4679a95c 100644 --- a/src/boot.cpp +++ b/src/boot.cpp @@ -76,14 +76,6 @@ void IRAM_ATTR watchdog() { xTaskResumeFromISR(RestartHandle); } void start_boot_menu(void) { - uint8_t mac[6]; - char clientId[20]; - - // hash 6 byte MAC to 4 byte hash - esp_eth_get_mac(mac); - const uint32_t hashedmac = myhash((const char *)mac, 6); - snprintf(clientId, 20, "paxcounter_%08x", hashedmac); - const char *host = clientId; const char *ssid = WIFI_SSID; const char *password = WIFI_PASS; diff --git a/src/main.cpp b/src/main.cpp index fbf7780e8..79ebb1c13 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -91,6 +91,7 @@ configData_t cfg; // struct holds current device configuration char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message uint8_t batt_level = 0; // display value uint8_t volatile rf_load = 0; // RF traffic indicator +char clientId[20] = {0}; // unique ClientID hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; @@ -137,6 +138,14 @@ void setup() { // load device configuration from NVRAM and set runmode do_after_reset(); + // hash 6 byte device MAC to 4 byte clientID + uint8_t mac[6]; + esp_eth_get_mac(mac); + const uint32_t hashedmac = myhash((const char *)mac, 6); + snprintf(clientId, 20, "paxcounter_%08x", hashedmac); + ESP_LOGI(TAG, "Starting %s v%s (runmode=%d / restarts=%d)", clientId, + PROGVERSION, RTC_runmode, RTC_restarts); + // print chip information on startup if in verbose mode after coldstart #if (VERBOSE) diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index caba854f2..dd0770f8a 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -42,14 +42,8 @@ esp_err_t mqtt_init(void) { } int mqtt_connect(const char *my_host, const uint16_t my_port) { - IPAddress mqtt_server_ip; - uint8_t mac[6]; - char clientId[20]; - // hash 6 byte MAC to 4 byte hash - esp_eth_get_mac(mac); - const uint32_t hashedmac = myhash((const char *)mac, 6); - snprintf(clientId, 20, "paxcounter_%08x", hashedmac); + IPAddress mqtt_server_ip; ESP_LOGI(TAG, "MQTT name is %s", MQTT_CLIENTNAME); diff --git a/src/ota.cpp b/src/ota.cpp index 4c8b7140e..792dd4f01 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -42,13 +42,6 @@ inline String getHeaderValue(String header, String headerName) { void start_ota_update() { - uint8_t mac[6]; - char clientId[20]; - - // hash 6 byte MAC to 4 byte hash - esp_eth_get_mac(mac); - const uint32_t hashedmac = myhash((const char *)mac, 6); - snprintf(clientId, 20, "paxcounter_%08x", hashedmac); const char *host = clientId; switch_LED(LED_ON); diff --git a/src/reset.cpp b/src/reset.cpp index 67deadf57..f9ad8dd3e 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -88,9 +88,6 @@ void do_after_reset(void) { RTC_restarts++; break; } - - ESP_LOGI(TAG, "Starting Software v%s (runmode=%d / restarts=%d)", PROGVERSION, - RTC_runmode, RTC_restarts); } void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { From d08914a219a9e0af8656878058c5198ba8f4613c Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 25 Mar 2021 14:24:25 +0100 Subject: [PATCH 20/68] libpax integration code sanitizations --- src/libpax_helpers.cpp | 7 +++++-- src/main.cpp | 10 ++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/libpax_helpers.cpp b/src/libpax_helpers.cpp index b637d226d..b1f883369 100644 --- a/src/libpax_helpers.cpp +++ b/src/libpax_helpers.cpp @@ -1,12 +1,15 @@ #include "libpax_helpers.h" +// Local logging tag +static const char TAG[] = __FILE__; + // libpax payload struct count_payload_t count_from_libpax; uint16_t volatile libpax_macs_ble, libpax_macs_wifi; void process_count(void) { - printf("pax: %d; %d; %d;\n", count_from_libpax.pax, - count_from_libpax.wifi_count, count_from_libpax.ble_count); + ESP_LOGD(TAG, "pax: %d / %d / %d", count_from_libpax.pax, + count_from_libpax.wifi_count, count_from_libpax.ble_count); libpax_macs_ble = count_from_libpax.ble_count; libpax_macs_wifi = count_from_libpax.wifi_count; } diff --git a/src/main.cpp b/src/main.cpp index 79ebb1c13..62a27cc4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -305,16 +305,18 @@ void setup() { #if (defined WIFICOUNTER || defined BLECOUNTER) struct libpax_config_t configuration; libpax_default_config(&configuration); - ESP_LOGI(TAG, "BLESCAN: %d", cfg.blescan); - ESP_LOGI(TAG, "WIFISCAN: %d", cfg.wifiscan); - configuration.wificounter = cfg.wifiscan; - configuration.blecounter = cfg.blescan; + // configure WIFI sniffing + configuration.wificounter = cfg.wifiscan; configuration.wifi_channel_map = WIFI_CHANNEL_ALL; configuration.wifi_channel_switch_interval = cfg.wifichancycle; configuration.wifi_rssi_threshold = cfg.rssilimit; + ESP_LOGI(TAG, "WIFISCAN: %s", cfg.wifiscan ? "on" : "off"); + // configure BLE sniffing + configuration.blecounter = cfg.blescan; configuration.blescantime = cfg.blescantime; + ESP_LOGI(TAG, "BLESCAN: %s", cfg.blescan ? "on" : "off"); int config_update = libpax_update_config(&configuration); if (config_update != 0) { From 97ee6d653ce0d1b3e42e30edc5d27116660cbc87 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 25 Mar 2021 22:30:27 +0100 Subject: [PATCH 21/68] removed rf_load var --- include/globals.h | 1 - src/display.cpp | 1 - src/libpax_helpers.cpp | 3 ++- src/main.cpp | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/globals.h b/include/globals.h index eb4f58b45..5282f27f5 100644 --- a/include/globals.h +++ b/include/globals.h @@ -130,7 +130,6 @@ extern configData_t cfg; // current device configuration extern char clientId[20]; // unique clientID extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer extern uint8_t volatile channel; // wifi channel rotation counter -extern uint8_t volatile rf_load; // RF traffic indicator extern uint8_t batt_level; // display value extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC diff --git a/src/display.cpp b/src/display.cpp index 91ee570b2..fa2ecab86 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -312,7 +312,6 @@ void dp_drawPage(time_t t, bool nextpage) { dp_printf(" "); #endif dp_printf(" ch:%02d", channel); - // dp_printf(" due:%02d", rf_load); dp_println(); // line 5: RSSI limiter + free memory diff --git a/src/libpax_helpers.cpp b/src/libpax_helpers.cpp index b1f883369..4cecebbda 100644 --- a/src/libpax_helpers.cpp +++ b/src/libpax_helpers.cpp @@ -15,6 +15,7 @@ void process_count(void) { } void init_libpax() { - libpax_counter_init(process_count, &count_from_libpax, 60 * 1000, 1); + libpax_counter_init(process_count, &count_from_libpax, SENDCYCLE * 2 * 1000, + 1); libpax_counter_start(); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 62a27cc4c..afd413221 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -90,7 +90,6 @@ triggers pps 1 sec impulse configData_t cfg; // struct holds current device configuration char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message uint8_t batt_level = 0; // display value -uint8_t volatile rf_load = 0; // RF traffic indicator char clientId[20] = {0}; // unique ClientID hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; From fcf2e4f59ae27811825599a36e0dbdfb5a458d78 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Mar 2021 18:06:10 +0100 Subject: [PATCH 22/68] MQTT timeserver: TTN scheduling first, not replace --- src/Node-RED/Timeserver.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Node-RED/Timeserver.json b/src/Node-RED/Timeserver.json index 7cbd398c1..7931404fe 100644 --- a/src/Node-RED/Timeserver.json +++ b/src/Node-RED/Timeserver.json @@ -25,7 +25,7 @@ "t": "set", "p": "payload.schedule", "pt": "msg", - "to": "replace", + "to": "first", "tot": "str" }, { From b5314f1288094c45a63c22e6f337faa6fab458a6 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Mar 2021 18:07:30 +0100 Subject: [PATCH 23/68] new rcommand 0x88 set_time --- README.md | 5 ++++- src/rcommand.cpp | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f5a1dd3b9..c624bc1dc 100644 --- a/README.md +++ b/README.md @@ -579,10 +579,13 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0x01 = timeNeedsSync (last sync failed) 0x02 = timeSet (synched) -0x87 set time/date +0x87 sync time/date Device synchronizes it's time/date by calling the preconfigured time source. +0x88 set time/date + + bytes 1..4 = time/date to set in UTC epoch seconds (LSB, e.g. https://www.epochconverter.com/hex) # License diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 38d9d7d07..454102cfd 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -21,7 +21,8 @@ void set_reset(uint8_t val[]) { reset_counters(); // clear macs break; case 2: // reset device to factory settings - ESP_LOGI(TAG, "Remote command: reset device to factory settings and restart"); + ESP_LOGI(TAG, + "Remote command: reset device to factory settings and restart"); eraseConfig(); do_reset(false); break; @@ -74,7 +75,7 @@ void set_sleepcycle(uint8_t val[]) { void set_wifichancycle(uint8_t val[]) { cfg.wifichancycle = val[0]; - #ifndef LIBAPX +#ifndef LIBAPX // update Wifi channel rotation timer period if (cfg.wifichancycle > 0) { if (xTimerIsTimerActive(WifiChanTimer) == pdFALSE) @@ -91,14 +92,14 @@ void set_wifichancycle(uint8_t val[]) { channel = WIFI_CHANNEL_MIN; ESP_LOGI(TAG, "Remote command: set Wifi channel hopping to off"); } - #else - // TODO update libpax configuration - #endif +#else +// TODO update libpax configuration +#endif } void set_blescantime(uint8_t val[]) { cfg.blescantime = val[0]; - #if !(LIBPAX) +#if !(LIBPAX) ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds", cfg.blescantime / float(100)); // stop & restart BLE scan task to apply new parameter @@ -106,9 +107,9 @@ void set_blescantime(uint8_t val[]) { stop_BLEscan(); start_BLEscan(); } - #else - // TODO update libpax configuration - #endif +#else + // TODO update libpax configuration +#endif } void set_countmode(uint8_t val[]) { @@ -251,37 +252,37 @@ void set_loraadr(uint8_t val[]) { void set_blescan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val[0] ? "on" : "off"); cfg.blescan = val[0] ? 1 : 0; - #if !(LIBPAX) +#if !(LIBPAX) macs_ble = 0; // clear BLE counter if (cfg.blescan) start_BLEscan(); else stop_BLEscan(); - #else +#else libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.blecounter = cfg.blescan; libpax_update_config(¤t_config); init_libpax(); - #endif +#endif } void set_wifiscan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set WIFI scanner to %s", val[0] ? "on" : "off"); cfg.wifiscan = val[0] ? 1 : 0; - #if !(LIBPAX) +#if !(LIBPAX) macs_wifi = 0; // clear WIFI counter switch_wifi_sniffer(cfg.wifiscan); - #else +#else libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.wificounter = cfg.wifiscan; libpax_update_config(¤t_config); init_libpax(); - #endif +#endif } void set_wifiant(uint8_t val[]) { @@ -381,11 +382,18 @@ void get_time(uint8_t val[]) { SendPayload(TIMEPORT); }; -void set_time(uint8_t val[]) { +void set_timesync(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: timesync requested"); setTimeSyncIRQ(); }; +void set_time(uint8_t val[]) { + // swap byte order from msb to lsb, note: this is a platform dependent hack + uint32_t t = __builtin_bswap32(*(uint32_t *)(val)); + ESP_LOGI(TAG, "Remote command: set time to %d", t); + setMyTime(t, 0, _unsynced); +}; + void set_flush(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: flush"); // does nothing @@ -431,8 +439,8 @@ static const cmd_t table[] = { {0x21, set_saveconfig, 0}, {0x80, get_config, 0}, {0x81, get_status, 0}, {0x83, get_batt, 0}, {0x84, get_gps, 0}, {0x85, get_bme, 0}, - {0x86, get_time, 0}, {0x87, set_time, 0}, - {0x99, set_flush, 0}}; + {0x86, get_time, 0}, {0x87, set_timesync, 0}, + {0x88, set_time, 4}, {0x99, set_flush, 0}}; static const uint8_t cmdtablesize = sizeof(table) / sizeof(table[0]); // number of commands in command table From 295494bd8193a589881e2562b24e7a2e75f09d72 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Mar 2021 18:52:39 +0100 Subject: [PATCH 24/68] change sleepcycle to 16bit * 10 --- README.md | 6 +++--- include/globals.h | 2 +- src/configmanager.cpp | 2 +- src/irqhandler.cpp | 4 ++-- src/paxcounter_orig.conf | 2 +- src/rcommand.cpp | 8 +++++--- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c624bc1dc..1af14989e 100644 --- a/README.md +++ b/README.md @@ -530,8 +530,8 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0x19 set sleep cycle - 0 ... 255 device sleep cycle in seconds/2 - e.g. 120 -> device sleeps 240 seconds after each send cycle [default = 0] + bytes 1..2 = device sleep cycle in seconds/10 (MSB) + e.g. {0x04, 0xB0} -> device sleeps 20 minutes after each send cycle [default = 0] 0x20 load device configuration @@ -585,7 +585,7 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0x88 set time/date - bytes 1..4 = time/date to set in UTC epoch seconds (LSB, e.g. https://www.epochconverter.com/hex) + bytes 1..4 = time/date to set in UTC epoch seconds (MSB, e.g. https://www.epochconverter.com/hex) # License diff --git a/include/globals.h b/include/globals.h index 950d21b17..3c7ae9eaa 100644 --- a/include/globals.h +++ b/include/globals.h @@ -75,7 +75,7 @@ typedef struct __attribute__((packed)) { uint8_t countermode; // 0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed int16_t rssilimit; // threshold for rssilimiter, negative value! uint8_t sendcycle; // payload send cycle [seconds/2] - uint8_t sleepcycle; // sleep cycle [seconds/2] + uint16_t sleepcycle; // sleep cycle [seconds/10] uint8_t wifichancycle; // wifi channel switch cycle [seconds/100] uint8_t blescantime; // BLE scan cycle duration [seconds] uint8_t blescan; // 0=disabled, 1=enabled diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 5ef3c41f6..ceccfcdcb 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -42,7 +42,7 @@ static void defaultConfig(configData_t *myconfig) { COUNTERMODE; // 0=cyclic, 1=cumulative, 2=cyclic confirmed myconfig->rssilimit = 0; // threshold for rssilimiter, negative value! myconfig->sendcycle = SENDCYCLE; // payload send cycle [seconds/2] - myconfig->sleepcycle = SLEEPCYCLE; // sleep cycle [seconds/2] + myconfig->sleepcycle = SLEEPCYCLE; // sleep cycle [seconds/10] myconfig->wifichancycle = WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100] myconfig->blescantime = diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index 244910ad2..89bd4c236 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -90,9 +90,9 @@ void irqHandler(void *pvParameters) { // goto sleep if we have a sleep cycle if (cfg.sleepcycle) #ifdef HAS_BUTTON - enter_deepsleep(cfg.sleepcycle * 2, (gpio_num_t)HAS_BUTTON); + enter_deepsleep(cfg.sleepcycle * 10, (gpio_num_t)HAS_BUTTON); #else - enter_deepsleep(cfg.sleepcycle * 2); + enter_deepsleep(cfg.sleepcycle * 10); #endif } } // for diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index 05246eabb..3140b45ff 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -14,7 +14,7 @@ // Payload send cycle and encoding #define SENDCYCLE 30 // payload send cycle [seconds/2], 0 .. 255 -#define SLEEPCYCLE 0 // sleep time after a send cycle [seconds/2], 0 .. 255; 0 means no sleep [default = 0] +#define SLEEPCYCLE 0 // sleep time after a send cycle [seconds/10], 0 .. 65535; 0 means no sleep [default = 0] #define PAYLOAD_ENCODER 2 // payload encoder: 1=Plain, 2=Packed, 3=Cayenne LPP dynamic, 4=Cayenne LPP packed #define COUNTERMODE 0 // 0=cyclic, 1=cumulative, 2=cyclic confirmed diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 454102cfd..0fd2c933f 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -68,9 +68,11 @@ void set_sendcycle(uint8_t val[]) { } void set_sleepcycle(uint8_t val[]) { - cfg.sleepcycle = val[0]; + // swap byte order from msb to lsb, note: this is a platform dependent hack + uint16_t t = __builtin_bswap16(*(uint16_t *)(val)); + cfg.sleepcycle = t; ESP_LOGI(TAG, "Remote command: set sleep cycle to %d seconds", - cfg.sleepcycle * 2); + cfg.sleepcycle * 10); } void set_wifichancycle(uint8_t val[]) { @@ -435,7 +437,7 @@ static const cmd_t table[] = { {0x13, set_sensor, 2}, {0x14, set_payloadmask, 1}, {0x15, set_bme, 1}, {0x16, set_batt, 1}, {0x17, set_wifiscan, 1}, {0x18, set_enscount, 1}, - {0x19, set_sleepcycle, 1}, {0x20, set_loadconfig, 0}, + {0x19, set_sleepcycle, 2}, {0x20, set_loadconfig, 0}, {0x21, set_saveconfig, 0}, {0x80, get_config, 0}, {0x81, get_status, 0}, {0x83, get_batt, 0}, {0x84, get_gps, 0}, {0x85, get_bme, 0}, From 29277af8afe43849a63c0b6567dea5b56db65560 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Mar 2021 22:59:23 +0100 Subject: [PATCH 25/68] libpax integration 2 (work in progress) --- src/rcommand.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 31838de54..1eb8efae1 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -56,6 +56,12 @@ void set_reset(uint8_t val[]) { void set_rssi(uint8_t val[]) { cfg.rssilimit = val[0] * -1; + libpax_counter_stop(); + libpax_config_t current_config; + libpax_get_current_config(¤t_config); + current_config.wifi_rssi_threshold = cfg.rssilimit; + libpax_update_config(¤t_config); + init_libpax(); ESP_LOGI(TAG, "Remote command: set RSSI limit to %d", cfg.rssilimit); } @@ -77,12 +83,33 @@ void set_sleepcycle(uint8_t val[]) { void set_wifichancycle(uint8_t val[]) { cfg.wifichancycle = val[0]; - // TODO update libpax configuration + libpax_counter_stop(); + libpax_config_t current_config; + libpax_get_current_config(¤t_config); + + if (cfg.wifichancycle == 0) { + ESP_LOGI(TAG, "Remote command: set Wifi channel hopping to off"); + current_config.wifi_channel_map = WIFI_CHANNEL_1; + } else { + ESP_LOGI( + TAG, + "Remote command: set Wifi channel hopping interval to %.1f seconds", + cfg.wifichancycle / float(100)); + } + + current_config.wifi_channel_switch_interval = cfg.wifichancycle; + libpax_update_config(¤t_config); + init_libpax(); } void set_blescantime(uint8_t val[]) { cfg.blescantime = val[0]; - // TODO update libpax configuration + libpax_counter_stop(); + libpax_config_t current_config; + libpax_get_current_config(¤t_config); + current_config.blescantime = cfg.blescantime; + libpax_update_config(¤t_config); + init_libpax(); } void set_countmode(uint8_t val[]) { @@ -187,7 +214,7 @@ void set_beacon(uint8_t val[]) { memmove(val, val + 1, 6); // strip off storage id beacons[id] = macConvert(val); // store beacon MAC in array ESP_LOGI(TAG, "Remote command: set beacon ID#%d", id); - //printKey("MAC", val, 6, false); // show beacon MAC + // printKey("MAC", val, 6, false); // show beacon MAC } void set_monitor(uint8_t val[]) { From b2d6bb7dccd3fbb3dc547de83a0f5b3e846e8266 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Mar 2021 23:25:11 +0100 Subject: [PATCH 26/68] libpax integration 3 (remove macfilter) --- README.md | 10 ++-------- include/globals.h | 1 - src/TTN/packed_decoder.js | 2 +- src/TTNv3/packed_decodeUplink.js | 2 +- src/configmanager.cpp | 1 - src/main.cpp | 4 ---- src/paxcounter_orig.conf | 1 - src/payload.cpp | 15 +++++++++------ src/rcommand.cpp | 30 ++++++++++++------------------ 9 files changed, 25 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 1af14989e..89cafb893 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ Note: If you use this software you do this at your own risk. That means that you # Privacy disclosure -Paxcounter generates identifiers for sniffed MAC adresses and collects them temporary in the device's RAM for a configurable scan cycle time (default 60 seconds). After each scan cycle the collected identifiers are cleared. Identifiers are generated by salting and hashing MAC adresses. The random salt value changes after each scan cycle. Identifiers and MAC adresses are never transferred to the LoRaWAN network. No persistent storing of MAC adresses, identifiers or timestamps and no other kind of analytics than counting are implemented in this code. Wireless networks are not touched by this code, but MAC adresses from wireless devices as well within as not within wireless networks, regardless if encrypted or unencrypted, are sniffed and processed by this code. If the bluetooth option in the code is enabled, bluetooth MACs are scanned and processed by the included BLE stack, then hashed and counted by this code. +Paxcounter generates identifiers for sniffed Wifi or Bluetooth MAC adresses and and collects them temporary in the device's RAM for a configurable scan cycle time (default 60 seconds). After each scan cycle the collected identifiers are cleared. Identifiers are generated by using the last 2 bytes of universal MAC adresses. Personal MAC adresses remain untouched and are not evaluated. Identifiers and MAC adresses are never transferred to the LoRaWAN network. No persistent storing of MAC adresses, identifiers or timestamps and no other kind of analytics than counting are implemented in this code. Wireless networks are not touched by this code, but MAC adresses from wireless devices as well within as not within wireless networks, regardless if encrypted or unencrypted, are sniffed and processed by this code. # LED blink pattern @@ -221,7 +221,6 @@ This describes how to set up a mobile PaxCounter:
Follow all steps so far fo Bluetooth low energy service UUID 0xFD6F, used by Google/Apple COVID-19 Exposure Notification System, can be monitored and counted. By comparing with the total number of observed devices this gives an indication how many people staying in proximity are using Apps for tracing COVID-19 exposures, e.g. in Germany the "Corona Warn App". To achive best results with this funcion, use following settings in `paxcounter.conf`: #define COUNT_ENS 1 // enable ENS monitoring function - #define MACFILTER 0 // disable MAC filter #define BLECOUNTER 1 // enable bluetooth sniffing #define WIFICOUNTER 0 // disable wifi sniffing (improves BLE scan speed) #define HAS_SENSOR_1 1 // optional: transmit ENS counter data to server @@ -320,7 +319,7 @@ Hereafter described is the default *plain* format, which uses MSB bit numbering. byte 13: Wifi antenna switch (0=internal, 1=external) [default 0] byte 14: count randomizated MACs only (0=disabled, 1=enabled) [default 1] byte 15: RGB LED luminosity (0..100 %) [default 30] - byte 16: Payload filter mask + byte 16: 0 (reserved) byte 17: Beacon proximity alarm mode (1=on, 0=off) [default 0] bytes 18-28: Software version (ASCII format, terminating with zero) @@ -460,11 +459,6 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0 ... 255 duration for scanning a bluetooth advertising channel in seconds/100 e.g. 8 -> each channel is scanned for 80 milliseconds [default] -0x0D (NOT YET IMPLEMENTED) set BLE and WIFI MAC filter mode - - 0 = disabled (use to count devices, not people) - 1 = enabled [default] - 0x0E set Bluetooth scanner 0 = disabled diff --git a/include/globals.h b/include/globals.h index cf8e08695..4c601a722 100644 --- a/include/globals.h +++ b/include/globals.h @@ -80,7 +80,6 @@ typedef struct __attribute__((packed)) { uint8_t blescan; // 0=disabled, 1=enabled uint8_t wifiscan; // 0=disabled, 1=enabled uint8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4) - uint8_t macfilter; // 0=disabled, 1=enabled uint8_t rgblum; // RGB Led luminosity (0..100%) uint8_t monitormode; // 0=disabled, 1=enabled uint8_t payloadmask; // bitswitches for payload data diff --git a/src/TTN/packed_decoder.js b/src/TTN/packed_decoder.js index fb66d8322..09461db80 100644 --- a/src/TTN/packed_decoder.js +++ b/src/TTN/packed_decoder.js @@ -248,7 +248,7 @@ var bitmap1 = function (byte) { } var i = bytesToInt(byte); var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean); - return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'filter', 'alarm'] + return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'reserved', 'alarm'] .reduce(function (obj, pos, index) { obj[pos] = +bm[index]; return obj; diff --git a/src/TTNv3/packed_decodeUplink.js b/src/TTNv3/packed_decodeUplink.js index ac4916c45..0b1c36274 100644 --- a/src/TTNv3/packed_decodeUplink.js +++ b/src/TTNv3/packed_decodeUplink.js @@ -263,7 +263,7 @@ var bitmap1 = function (byte) { } var i = bytesToInt(byte); var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean); - return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'filter', 'alarm'] + return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'reserved', 'alarm'] .reduce(function (obj, pos, index) { obj[pos] = +bm[index]; return obj; diff --git a/src/configmanager.cpp b/src/configmanager.cpp index ceccfcdcb..55c604dd2 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -51,7 +51,6 @@ static void defaultConfig(configData_t *myconfig) { myconfig->blescan = BLECOUNTER; // 0=disabled, 1=enabled myconfig->wifiscan = WIFICOUNTER; // 0=disabled, 1=enabled myconfig->wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4) - myconfig->macfilter = MACFILTER; // 0=disabled, 1=enabled myconfig->rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%) myconfig->monitormode = 0; // 0=disabled, 1=enabled myconfig->payloadmask = PAYLOADMASK; // payloads as defined in default diff --git a/src/main.cpp b/src/main.cpp index afd413221..6f0ee4eda 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -399,10 +399,6 @@ void setup() { strcat_P(features, " SDS"); #endif -#if (MACFILTER) - strcat_P(features, " FILTER"); -#endif - // initialize matrix display #ifdef HAS_MATRIX_DISPLAY strcat_P(features, " LED_MATRIX"); diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index d35c39096..629ae30c7 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -19,7 +19,6 @@ #define COUNTERMODE 0 // 0=cyclic, 1=cumulative, 2=cyclic confirmed // MAC sniffing parameters -#define MACFILTER 1 // set to 0 if you want to scan all devices, 1 to scan only devices with random MACs (aka smartphones) [default = 1] #define BLECOUNTER 1 // set to 0 if you do not want to install the BLE sniffer #define WIFICOUNTER 1 // set to 0 if you do not want to install the WIFI sniffer #define MAC_QUEUE_SIZE 50 // size of MAC processing buffer (number of MACs) [default = 50] diff --git a/src/payload.cpp b/src/payload.cpp index 6c9439810..2e89b1f4a 100644 --- a/src/payload.cpp +++ b/src/payload.cpp @@ -49,7 +49,7 @@ void PayloadConvert::addConfig(configData_t value) { buffer[cursor++] = value.blescantime; buffer[cursor++] = value.blescan; buffer[cursor++] = value.wifiant; - buffer[cursor++] = value.macfilter; + buffer[cursor++] = 0; // reserved buffer[cursor++] = value.rgblum; buffer[cursor++] = value.payloadmask; buffer[cursor++] = value.monitormode; @@ -58,7 +58,8 @@ void PayloadConvert::addConfig(configData_t value) { } void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp, - uint32_t mem, uint8_t reset0, uint32_t restarts) { + uint32_t mem, uint8_t reset0, + uint32_t restarts) { buffer[cursor++] = highByte(voltage); buffer[cursor++] = lowByte(voltage); @@ -181,14 +182,15 @@ void PayloadConvert::addConfig(configData_t value) { writeUint8(value.rgblum); writeBitmap(value.adrmode ? true : false, value.screensaver ? true : false, value.screenon ? true : false, value.countermode ? true : false, - value.blescan ? true : false, value.wifiant ? true : false, - value.macfilter ? true : false, value.monitormode ? true : false); + value.blescan ? true : false, value.wifiant ? true : false, 0, + value.monitormode ? true : false); writeUint8(value.payloadmask); writeVersion(value.version); } void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp, - uint32_t mem, uint8_t reset0, uint32_t restarts) { + uint32_t mem, uint8_t reset0, + uint32_t restarts) { writeUint16(voltage); writeUptime(uptime); writeUint8((byte)cputemp); @@ -400,7 +402,8 @@ void PayloadConvert::addConfig(configData_t value) { } void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float celsius, - uint32_t mem, uint8_t reset0, uint32_t restarts) { + uint32_t mem, uint8_t reset0, + uint32_t restarts) { uint16_t temp = celsius * 10; uint16_t volt = voltage / 10; #if (defined BAT_MEASURE_ADC || defined HAS_PMU) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 1eb8efae1..12c83ce98 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -287,12 +287,6 @@ void set_wifiant(uint8_t val[]) { #endif } -void set_macfilter(uint8_t val[]) { - ESP_LOGI(TAG, "Remote command: set macfilter mode to %s", - val[0] ? "on" : "off"); - cfg.macfilter = val[0] ? 1 : 0; -} - void set_rgblum(uint8_t val[]) { // Avoid wrong parameters cfg.rgblum = (val[0] <= 100) ? (uint8_t)val[0] : RGBLUMINOSITY; @@ -422,18 +416,18 @@ static const cmd_t table[] = { {0x07, set_loraadr, 1}, {0x08, set_screensaver, 1}, {0x09, set_reset, 1}, {0x0a, set_sendcycle, 1}, {0x0b, set_wifichancycle, 1}, {0x0c, set_blescantime, 1}, - {0x0d, set_macfilter, 1}, {0x0e, set_blescan, 1}, - {0x0f, set_wifiant, 1}, {0x10, set_rgblum, 1}, - {0x11, set_monitor, 1}, {0x12, set_beacon, 7}, - {0x13, set_sensor, 2}, {0x14, set_payloadmask, 1}, - {0x15, set_bme, 1}, {0x16, set_batt, 1}, - {0x17, set_wifiscan, 1}, {0x18, set_enscount, 1}, - {0x19, set_sleepcycle, 2}, {0x20, set_loadconfig, 0}, - {0x21, set_saveconfig, 0}, {0x80, get_config, 0}, - {0x81, get_status, 0}, {0x83, get_batt, 0}, - {0x84, get_gps, 0}, {0x85, get_bme, 0}, - {0x86, get_time, 0}, {0x87, set_timesync, 0}, - {0x88, set_time, 4}, {0x99, set_flush, 0}}; + {0x0e, set_blescan, 1}, {0x0f, set_wifiant, 1}, + {0x10, set_rgblum, 1}, {0x11, set_monitor, 1}, + {0x12, set_beacon, 7}, {0x13, set_sensor, 2}, + {0x14, set_payloadmask, 1}, {0x15, set_bme, 1}, + {0x16, set_batt, 1}, {0x17, set_wifiscan, 1}, + {0x18, set_enscount, 1}, {0x19, set_sleepcycle, 2}, + {0x20, set_loadconfig, 0}, {0x21, set_saveconfig, 0}, + {0x80, get_config, 0}, {0x81, get_status, 0}, + {0x83, get_batt, 0}, {0x84, get_gps, 0}, + {0x85, get_bme, 0}, {0x86, get_time, 0}, + {0x87, set_timesync, 0}, {0x88, set_time, 4}, + {0x99, set_flush, 0}}; static const uint8_t cmdtablesize = sizeof(table) / sizeof(table[0]); // number of commands in command table From 4bb93b3c1057f4e07a4626aa853a610e408985be Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Mar 2021 23:25:54 +0100 Subject: [PATCH 27/68] libpax integration 4 (remove MAC_QUEUE_SIZE) --- src/paxcounter_orig.conf | 1 - 1 file changed, 1 deletion(-) diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index 629ae30c7..3ce748219 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -21,7 +21,6 @@ // MAC sniffing parameters #define BLECOUNTER 1 // set to 0 if you do not want to install the BLE sniffer #define WIFICOUNTER 1 // set to 0 if you do not want to install the WIFI sniffer -#define MAC_QUEUE_SIZE 50 // size of MAC processing buffer (number of MACs) [default = 50] // BLE scan parameters #define BLESCANTIME 0 // [seconds] scan duration, 0 means infinite [default], see note below From e44e4065001c1af472a98556ab644a6c7689e3be Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Mar 2021 23:36:39 +0100 Subject: [PATCH 28/68] libpax integration 5 (sanitize globals.h) --- include/globals.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/globals.h b/include/globals.h index 4c601a722..e5e4f35cb 100644 --- a/include/globals.h +++ b/include/globals.h @@ -128,7 +128,6 @@ extern std::array beacons; extern configData_t cfg; // current device configuration extern char clientId[20]; // unique clientID extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer -extern uint8_t volatile channel; // wifi channel rotation counter extern uint8_t batt_level; // display value extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC @@ -136,7 +135,6 @@ extern timesource_t timeSource; extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ; extern SemaphoreHandle_t I2Caccess; extern TaskHandle_t irqHandlerTask, ClockTask; -extern TimerHandle_t WifiChanTimer; extern Timezone myTZ; #endif From 7d401697f6b0a0f3bc635697bc10d91e8cc580ec Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 31 Mar 2021 09:44:23 +0200 Subject: [PATCH 29/68] issue #774 --- include/globals.h | 2 +- src/gpsread.cpp | 5 +++-- src/rcommand.cpp | 2 +- src/timekeeper.cpp | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/globals.h b/include/globals.h index cf8e08695..5a0551238 100644 --- a/include/globals.h +++ b/include/globals.h @@ -50,7 +50,7 @@ #define _seconds() millis() / 1000.0 -enum timesource_t { _gps, _rtc, _lora, _unsynced }; +enum timesource_t { _gps, _rtc, _lora, _set, _unsynced }; enum snifftype_t { MAC_SNIFF_WIFI, MAC_SNIFF_BLE, MAC_SNIFF_BLE_ENS }; enum runmode_t { RUNMODE_POWERCYCLE, diff --git a/src/gpsread.cpp b/src/gpsread.cpp index d253bcb25..7801a4cec 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -120,7 +120,7 @@ time_t get_gpstime(uint16_t *msec) { t = makeTime(tm); ESP_LOGD(TAG, "GPS time/date = %02d:%02d:%02d / %02d.%02d.%2d", tm.Hour, - tm.Minute, tm.Second, tm.Day, tm.Month, tm.Year + 1970); + tm.Minute, tm.Second, tm.Day, tm.Month, tm.Year + 1970); // add protocol delay with millisecond precision t += delay_ms / 1000 - 1; // whole seconds @@ -163,7 +163,8 @@ void gps_loop(void *pvParameters) { // (only) while device time is not set or unsynched, and we have a valid // GPS time, we trigger a device time update to poll time from GPS - if (timeSource == _unsynced && gpstime.isUpdated()) { + if ((timeSource == _unsynced || timeSource == _set) && + gpstime.isUpdated()) { now(); calibrateTime(); } diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 31838de54..4b0801d30 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -357,7 +357,7 @@ void set_time(uint8_t val[]) { // swap byte order from msb to lsb, note: this is a platform dependent hack uint32_t t = __builtin_bswap32(*(uint32_t *)(val)); ESP_LOGI(TAG, "Remote command: set time to %d", t); - setMyTime(t, 0, _unsynced); + setMyTime(t, 0, _set); }; void set_flush(uint8_t val[]) { diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index f49adc7c9..d1da77448 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -12,7 +12,7 @@ static const char TAG[] = __FILE__; // symbol to display current time source -const char timeSetSymbols[] = {'G', 'R', 'L', '?'}; +const char timeSetSymbols[] = {'G', 'R', 'L', 'S', '?'}; #ifdef HAS_IF482 #if (HAS_SDS011) From 74ca49b84e0a4e63664560ffd5eb1393f2efee11 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 31 Mar 2021 09:45:32 +0200 Subject: [PATCH 30/68] main.h: added include coexist.h --- include/main.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/main.h b/include/main.h index 8fcc6b7ec..6d2352b27 100644 --- a/include/main.h +++ b/include/main.h @@ -4,6 +4,7 @@ #include // needed for reading ESP32 chip attributes #include // needed for Wifi event handler #include // needed for timers +#include // needed for coex version display #include "globals.h" #include "reset.h" From dea92ced65fee2746d5dcf4ca29b5aa206aefa25 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 31 Mar 2021 16:20:48 +0200 Subject: [PATCH 31/68] fix issue #773 (libpax counts display integration) --- README.md | 2 +- include/cyclic.h | 1 - include/display.h | 1 + src/cyclic.cpp | 26 +++++--------------------- src/display.cpp | 18 +++++++++++------- src/ledmatrixdisplay.cpp | 13 ++++++++----- src/rcommand.cpp | 8 +++----- src/senddata.cpp | 11 +++-------- 8 files changed, 32 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 1af14989e..56dc13760 100644 --- a/README.md +++ b/README.md @@ -437,7 +437,7 @@ Send for example `8386` as Downlink on Port 2 to get battery status and time/dat 0x09 reset functions (send this command UNconfirmed only to avoid boot loops!) 0 = restart device (coldstart) - 1 = zeroize MAC counter + 1 = (reserved, currently does nothing) 2 = reset device to factory settings and restart device 3 = flush send queues 4 = restart device (warmstart) diff --git a/include/cyclic.h b/include/cyclic.h index 26ce3fd59..3623a7b5b 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -18,7 +18,6 @@ extern Ticker cyclicTimer; void setCyclicIRQ(void); void doHousekeeping(void); -void reset_counters(void); uint32_t getFreeRAM(); #endif diff --git a/include/display.h b/include/display.h index 8fb8120f2..8b16f4129 100644 --- a/include/display.h +++ b/include/display.h @@ -1,6 +1,7 @@ #ifndef _DISPLAY_H #define _DISPLAY_H +#include #include "cyclic.h" #include "qrcode.h" diff --git a/src/cyclic.cpp b/src/cyclic.cpp index dd1150079..d197e2341 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -89,24 +89,18 @@ void doHousekeeping() { // check free heap memory if (ESP.getMinFreeHeap() <= MEM_LOW) { - ESP_LOGI(TAG, + ESP_LOGW(TAG, "Memory full, counter cleared (heap low water mark = %d Bytes / " "free heap = %d bytes)", ESP.getMinFreeHeap(), ESP.getFreeHeap()); - reset_counters(); // clear macs container and reset all counters - - if (ESP.getMinFreeHeap() <= MEM_LOW) // check again - do_reset(true); // memory leak, reset device + do_reset(true); // memory leak, reset device } // check free PSRAM memory #ifdef BOARD_HAS_PSRAM if (ESP.getMinFreePsram() <= MEM_LOW) { - ESP_LOGI(TAG, "PSRAM full, counter cleared"); - reset_counters(); // clear macs container and reset all counters - - if (ESP.getMinFreePsram() <= MEM_LOW) // check again - do_reset(true); // memory leak, reset device + ESP_LOGW(TAG, "PSRAM full, counter cleared"); + do_reset(true); // memory leak, reset device } #endif @@ -128,14 +122,4 @@ uint32_t getFreeRAM() { #else return ESP.getFreePsram(); #endif -} - -void reset_counters() { - -#if ((WIFICOUNTER) || (BLECOUNTER)) -#ifdef HAS_DISPLAY - dp_plotCurve(0, true); -#endif - -#endif -} +} \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index fa2ecab86..7c5b6d9a7 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -159,7 +159,7 @@ void dp_init(bool verbose) { #if !(BOOTMENU) delay(8000); #endif - + #endif // HAS_LORA } // verbose @@ -175,12 +175,15 @@ void dp_init(bool verbose) { void dp_refresh(bool nextPage) { + // update counter values from libpax + libpax_counter_count(&count_from_libpax); + #ifndef HAS_BUTTON static uint32_t framecounter = 0; #endif // update histogram - dp_plotCurve(macs.size(), false); + dp_plotCurve(count_from_libpax.pax, false); // if display is switched off we don't refresh it to relax cpu if (!DisplayIsOn && (DisplayIsOn == cfg.screenon)) @@ -238,7 +241,7 @@ void dp_drawPage(time_t t, bool nextpage) { // display number of unique macs total Wifi + BLE if (DisplayPage < 5) { dp_setFont(MY_FONT_STRETCHED); - dp_printf("%-5d", macs.size()); + dp_printf("%-5d", count_from_libpax.pax); } switch (DisplayPage) { @@ -262,7 +265,7 @@ void dp_drawPage(time_t t, bool nextpage) { #if ((WIFICOUNTER) && (BLECOUNTER)) if (cfg.wifiscan) - dp_printf("WIFI:%-5d", libpax_macs_wifi); + dp_printf("WIFI:%-5d", count_from_libpax.wifi_count); else dp_printf("WIFI:off"); if (cfg.blescan) @@ -271,17 +274,17 @@ void dp_drawPage(time_t t, bool nextpage) { dp_printf(" CWA:%-5d", cwa_report()); else #endif - dp_printf("BLTH:%-5d", libpax_macs_ble); + dp_printf("BLTH:%-5d", count_from_libpax.ble_count); else dp_printf(" BLTH:off"); #elif ((WIFICOUNTER) && (!BLECOUNTER)) if (cfg.wifiscan) - dp_printf("WIFI:%-5d", libpax_macs_wifi); + dp_printf("WIFI:%-5d", count_from_libpax.wifi_count); else dp_printf("WIFI:off"); #elif ((!WIFICOUNTER) && (BLECOUNTER)) if (cfg.blescan) - dp_printf("BLTH:%-5d", libpax_macs_ble); + dp_printf("BLTH:%-5d", count_from_libpax.ble_count); #if (COUNT_ENS) if (cfg.enscount) dp_printf("(CWA:%d)", cwa_report()); @@ -719,6 +722,7 @@ void dp_plotCurve(uint16_t count, bool reset) { static uint16_t last_count = 0, col = 0, row = 0; uint16_t v_scroll = 0; + // nothing new to plot? -> then exit early if ((last_count == count) && !reset) return; diff --git a/src/ledmatrixdisplay.cpp b/src/ledmatrixdisplay.cpp index 3e2737312..c727a766d 100644 --- a/src/ledmatrixdisplay.cpp +++ b/src/ledmatrixdisplay.cpp @@ -76,9 +76,12 @@ void refreshTheMatrixDisplay(bool nextPage) { if (cfg.countermode == 1) + // update counter values from libpax + libpax_counter_count(&count_from_libpax); + { // cumulative counter mode -> display total number of pax - if (ulLastNumMacs != macs.size()) { - ulLastNumMacs = macs.size(); + if (ulLastNumMacs != count_from_libpax.pax) { + ulLastNumMacs = count_from_libpax.pax; matrix.clear(); DrawNumber(String(ulLastNumMacs)); } @@ -86,10 +89,10 @@ void refreshTheMatrixDisplay(bool nextPage) { else { // cyclic counter mode -> plot a line diagram - if (ulLastNumMacs != macs.size()) { + if (ulLastNumMacs != count_from_libpax.pax) { // next count cycle? - if (macs.size() == 0) { + if (count_from_libpax.pax == 0) { // matrix full? then scroll left 1 dot, else increment column if (col < (LED_MATRIX_WIDTH - 1)) @@ -101,7 +104,7 @@ void refreshTheMatrixDisplay(bool nextPage) { matrix.drawPoint(col, row, 0); // clear current dot // scale and set new dot - ulLastNumMacs = macs.size(); + ulLastNumMacs = count_from_libpax.pax; level = ulLastNumMacs / LINE_DIAGRAM_DIVIDER; row = level <= LED_MATRIX_HEIGHT ? LED_MATRIX_HEIGHT - 1 - level % LED_MATRIX_HEIGHT diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 4b0801d30..5a48f466d 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -16,9 +16,8 @@ void set_reset(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: restart device cold"); do_reset(false); break; - case 1: // reset MAC counter - ESP_LOGI(TAG, "Remote command: reset MAC counter"); - reset_counters(); // clear macs + case 1: // reserved + // reset MAC counter deprecated by libpax integration break; case 2: // reset device to factory settings ESP_LOGI(TAG, @@ -105,7 +104,6 @@ void set_countmode(uint8_t val[]) { "Remote command: set counter mode called with invalid parameter(s)"); return; } - reset_counters(); // clear macs } void set_screensaver(uint8_t val[]) { @@ -187,7 +185,7 @@ void set_beacon(uint8_t val[]) { memmove(val, val + 1, 6); // strip off storage id beacons[id] = macConvert(val); // store beacon MAC in array ESP_LOGI(TAG, "Remote command: set beacon ID#%d", id); - //printKey("MAC", val, 6, false); // show beacon MAC + // printKey("MAC", val, 6, false); // show beacon MAC } void set_monitor(uint8_t val[]) { diff --git a/src/senddata.cpp b/src/senddata.cpp index 12fe735c9..d76655264 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -88,7 +88,8 @@ void sendData() { ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); if (cfg.blescan) { - ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); + ESP_LOGI(TAG, "Sending libpax ble count: %d", + libpax_macs_ble); payload.addCount(libpax_macs_ble, MAC_SNIFF_BLE); } #endif @@ -114,14 +115,8 @@ void sendData() { payload.addSDS(sds_status); #endif SendPayload(COUNTERPORT); - // clear counter if not in cumulative counter mode - if (cfg.countermode != 1) { - reset_counters(); // clear macs container and reset all counters - ESP_LOGI(TAG, "Counter cleared"); - } #ifdef HAS_DISPLAY - else - dp_plotCurve(macs.size(), true); + dp_plotCurve(libpax_macs_ble + libpax_macs_wifi, true); #endif break; #endif From e9d52ba560750c637850ee76b186fb69428d8087 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 31 Mar 2021 16:28:32 +0200 Subject: [PATCH 32/68] globals.h: fix missing extern var channel --- include/globals.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/globals.h b/include/globals.h index 90605bf10..4e5202ac6 100644 --- a/include/globals.h +++ b/include/globals.h @@ -130,6 +130,7 @@ extern char clientId[20]; // unique clientID extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer extern uint8_t batt_level; // display value extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values +extern uint8_t volatile channel; // wifi channel rotation counter extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC extern timesource_t timeSource; extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ; From 03796c39af0dbf08d4d3a3a18f5cc179b03c63bf Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 31 Mar 2021 18:59:00 +0200 Subject: [PATCH 33/68] reset.cpp: bugfix compiler error sds011_sleep() --- src/reset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reset.cpp b/src/reset.cpp index f9ad8dd3e..e7525ac37 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -106,7 +106,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { // switch off radio and other power consuming hardware #if (HAS_SDS011) - sds011_sleep(void); + sds011_sleep(); #endif // halt interrupts accessing i2c bus From e439d932b9acf44b1ffe398b7ddeb851b106145b Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 31 Mar 2021 19:02:01 +0200 Subject: [PATCH 34/68] code sanitization (decentralized several vars) --- include/globals.h | 1 - include/lorawan.h | 1 + include/main.h | 1 + src/display.cpp | 2 ++ src/i2c.cpp | 2 ++ src/irqhandler.cpp | 2 ++ src/ledmatrixdisplay.cpp | 2 ++ src/lorawan.cpp | 1 + src/main.cpp | 23 +++-------------------- src/payload.cpp | 3 +++ src/timekeeper.cpp | 11 +++++++++++ 11 files changed, 28 insertions(+), 21 deletions(-) diff --git a/include/globals.h b/include/globals.h index 4e5202ac6..225ebd937 100644 --- a/include/globals.h +++ b/include/globals.h @@ -127,7 +127,6 @@ extern std::array beacons; extern configData_t cfg; // current device configuration extern char clientId[20]; // unique clientID -extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer extern uint8_t batt_level; // display value extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values extern uint8_t volatile channel; // wifi channel rotation counter diff --git a/include/lorawan.h b/include/lorawan.h index e22397397..fd300d9da 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -19,6 +19,7 @@ #endif extern TaskHandle_t lmicTask, lorasendTask; +extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer esp_err_t lmic_init(void); void lora_setupForNetwork(bool preJoin); diff --git a/include/main.h b/include/main.h index 6d2352b27..765dc7bd6 100644 --- a/include/main.h +++ b/include/main.h @@ -20,5 +20,6 @@ #include "timekeeper.h" #include "corona.h" #include "boot.h" +#include "libpax_helpers.h" #endif \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index 7c5b6d9a7..2656246bc 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -47,6 +47,8 @@ uint8_t displaybuf[MY_DISPLAY_WIDTH * MY_DISPLAY_HEIGHT / 8] = {0}; static uint8_t plotbuf[MY_DISPLAY_WIDTH * MY_DISPLAY_HEIGHT / 8] = {0}; static int dp_row = 0, dp_col = 0, dp_font = 0; +hw_timer_t *displayIRQ = NULL; + QRCode qrcode; #ifdef HAS_DISPLAY diff --git a/src/i2c.cpp b/src/i2c.cpp index f8f1bbdf2..2d8820439 100644 --- a/src/i2c.cpp +++ b/src/i2c.cpp @@ -5,6 +5,8 @@ // Local logging tag static const char TAG[] = __FILE__; +SemaphoreHandle_t I2Caccess; + void i2c_init(void) { Wire.begin(MY_DISPLAY_SDA, MY_DISPLAY_SCL, 100000); } void i2c_deinit(void) { Wire.~TwoWire(); } diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index 89bd4c236..2e97f360d 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -3,6 +3,8 @@ // Local logging tag static const char TAG[] = __FILE__; +TaskHandle_t irqHandlerTask = NULL; + // irq handler task, handles all our application level interrupts void irqHandler(void *pvParameters) { diff --git a/src/ledmatrixdisplay.cpp b/src/ledmatrixdisplay.cpp index c727a766d..a927cb353 100644 --- a/src/ledmatrixdisplay.cpp +++ b/src/ledmatrixdisplay.cpp @@ -14,6 +14,8 @@ static uint8_t displaybuf[LED_MATRIX_WIDTH * LED_MATRIX_HEIGHT / 8] = {0}; static unsigned long ulLastNumMacs = 0; static time_t ulLastTime = myTZ.toLocal(now()); +hw_timer_t *matrixDisplayIRQ = NULL; + LEDMatrix matrix(LED_MATRIX_LA_74138, LED_MATRIX_LB_74138, LED_MATRIX_LC_74138, LED_MATRIX_LD_74138, LED_MATRIX_EN_74138, LED_MATRIX_DATA_R1, LED_MATRIX_LATCHPIN, LED_MATRIX_CLOCKPIN); diff --git a/src/lorawan.cpp b/src/lorawan.cpp index b7a7fbddf..d01cfff26 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -21,6 +21,7 @@ RTC_DATA_ATTR lmic_t RTC_LMIC; static QueueHandle_t LoraSendQueue; TaskHandle_t lmicTask = NULL, lorasendTask = NULL; +char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message class MyHalConfig_t : public Arduino_LMIC::HalConfiguration_t { diff --git a/src/main.cpp b/src/main.cpp index 6f0ee4eda..b20d5f34c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,31 +85,14 @@ triggers pps 1 sec impulse // Basic Config #include "main.h" -#include "libpax_helpers.h" + +// local Tag for logging +static const char TAG[] = __FILE__; configData_t cfg; // struct holds current device configuration -char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message uint8_t batt_level = 0; // display value char clientId[20] = {0}; // unique ClientID -hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; - -TaskHandle_t irqHandlerTask = NULL, ClockTask = NULL; -SemaphoreHandle_t I2Caccess; -bool volatile TimePulseTick = false; -timesource_t timeSource = _unsynced; - -// initialize payload encoder -PayloadConvert payload(PAYLOAD_BUFFER_SIZE); - -// set Time Zone for user setting from paxcounter.conf -TimeChangeRule myDST = DAYLIGHT_TIME; -TimeChangeRule mySTD = STANDARD_TIME; -Timezone myTZ(myDST, mySTD); - -// local Tag for logging -static const char TAG[] = __FILE__; - void setup() { char features[100] = ""; diff --git a/src/payload.cpp b/src/payload.cpp index 2e89b1f4a..740a250f3 100644 --- a/src/payload.cpp +++ b/src/payload.cpp @@ -1,6 +1,9 @@ #include "globals.h" #include "payload.h" +// initialize payload encoder +PayloadConvert payload(PAYLOAD_BUFFER_SIZE); + PayloadConvert::PayloadConvert(uint8_t size) { buffer = (uint8_t *)malloc(size); cursor = 0; diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index d1da77448..9019fd3b7 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -14,6 +14,17 @@ static const char TAG[] = __FILE__; // symbol to display current time source const char timeSetSymbols[] = {'G', 'R', 'L', 'S', '?'}; +// set Time Zone for user setting from paxcounter.conf +TimeChangeRule myDST = DAYLIGHT_TIME; +TimeChangeRule mySTD = STANDARD_TIME; +Timezone myTZ(myDST, mySTD); + +bool volatile TimePulseTick = false; +timesource_t timeSource = _unsynced; + +TaskHandle_t ClockTask = NULL; +hw_timer_t *ppsIRQ = NULL; + #ifdef HAS_IF482 #if (HAS_SDS011) #error cannot use IF482 together with SDS011 (both use UART#2) From 9335161676391feb43f35024a1669506dadc1217 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 31 Mar 2021 21:43:51 +0200 Subject: [PATCH 35/68] code sanitization (decentralized several vars, 2) --- include/configmanager.h | 2 ++ include/cyclic.h | 1 + include/display.h | 4 ++++ include/globals.h | 21 +-------------------- include/i2c.h | 2 ++ include/irqhandler.h | 2 ++ include/ledmatrixdisplay.h | 2 +- include/libpax_helpers.h | 2 ++ include/main.h | 1 + include/payload.h | 2 ++ include/rcommand.h | 1 + include/reset.h | 9 +++++++++ include/senddata.h | 1 + include/sensor.h | 2 ++ include/timekeeper.h | 7 +++++++ src/configmanager.cpp | 2 ++ src/main.cpp | 2 -- src/power.cpp | 2 ++ 18 files changed, 42 insertions(+), 23 deletions(-) diff --git a/include/configmanager.h b/include/configmanager.h index 958649947..e9c5e7c06 100644 --- a/include/configmanager.h +++ b/include/configmanager.h @@ -5,6 +5,8 @@ #include "reset.h" #include +extern configData_t cfg; + void saveConfig(bool erase = false); bool loadConfig(void); void eraseConfig(void); diff --git a/include/cyclic.h b/include/cyclic.h index 3623a7b5b..94619a147 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -13,6 +13,7 @@ #include "sdcard.h" #include "reset.h" #include "led.h" +#include "power.h" extern Ticker cyclicTimer; diff --git a/include/display.h b/include/display.h index 8b16f4129..338f62d58 100644 --- a/include/display.h +++ b/include/display.h @@ -4,6 +4,7 @@ #include #include "cyclic.h" #include "qrcode.h" +#include "power.h" #if (COUNT_ENS) #include "corona.h" @@ -77,7 +78,10 @@ #define QR_VERSION 3 // 29 x 29px const uint8_t QR_SCALEFACTOR = (MY_DISPLAY_HEIGHT - 4) / 29; // 4px borderlines + extern uint8_t DisplayIsOn, displaybuf[]; +extern hw_timer_t *displayIRQ; +extern uint8_t volatile channel; // wifi channel rotation counter void dp_setup(int contrast = 0); void dp_refresh(bool nextPage = false); diff --git a/include/globals.h b/include/globals.h index 225ebd937..b64790482 100644 --- a/include/globals.h +++ b/include/globals.h @@ -50,16 +50,7 @@ #define _seconds() millis() / 1000.0 -enum timesource_t { _gps, _rtc, _lora, _set, _unsynced }; enum snifftype_t { MAC_SNIFF_WIFI, MAC_SNIFF_BLE, MAC_SNIFF_BLE_ENS }; -enum runmode_t { - RUNMODE_POWERCYCLE, - RUNMODE_NORMAL, - RUNMODE_WAKEUP, - RUNMODE_UPDATE, - RUNMODE_SLEEP, - RUNMODE_MAINTENANCE -}; // Struct holding devices's runtime configuration // using packed to avoid compiler padding, because struct will be memcpy'd to @@ -125,16 +116,6 @@ typedef struct { extern std::array::iterator it; extern std::array beacons; -extern configData_t cfg; // current device configuration -extern char clientId[20]; // unique clientID -extern uint8_t batt_level; // display value -extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values -extern uint8_t volatile channel; // wifi channel rotation counter -extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC -extern timesource_t timeSource; -extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ; -extern SemaphoreHandle_t I2Caccess; -extern TaskHandle_t irqHandlerTask, ClockTask; -extern Timezone myTZ; +extern char clientId[20]; // unique clientID #endif diff --git a/include/i2c.h b/include/i2c.h index 7fcbd33a3..ce64ff70e 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -21,6 +21,8 @@ #define MY_DISPLAY_SCL SCL #endif +extern SemaphoreHandle_t I2Caccess; + void i2c_init(void); void i2c_deinit(void); void i2c_scan(void); diff --git a/include/irqhandler.h b/include/irqhandler.h index 5d40c63c5..8b14b293f 100644 --- a/include/irqhandler.h +++ b/include/irqhandler.h @@ -26,6 +26,8 @@ void mask_user_IRQ(); void unmask_user_IRQ(); void doIRQ(int irq); +extern TaskHandle_t irqHandlerTask; + #ifdef HAS_DISPLAY void IRAM_ATTR DisplayIRQ(); #endif diff --git a/include/ledmatrixdisplay.h b/include/ledmatrixdisplay.h index 9db9ebeeb..b27914c13 100644 --- a/include/ledmatrixdisplay.h +++ b/include/ledmatrixdisplay.h @@ -6,8 +6,8 @@ #include "ledmatrixdisplay.h" extern uint8_t MatrixDisplayIsOn; - extern LEDMatrix matrix; +extern hw_timer_t *matrixDisplayIRQ; void init_matrix_display(bool reverse = false); void refreshTheMatrixDisplay(bool nextPage = false); diff --git a/include/libpax_helpers.h b/include/libpax_helpers.h index dd3c02387..597492269 100644 --- a/include/libpax_helpers.h +++ b/include/libpax_helpers.h @@ -7,4 +7,6 @@ void init_libpax(); +extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values + #endif \ No newline at end of file diff --git a/include/main.h b/include/main.h index 765dc7bd6..8a46c6e4b 100644 --- a/include/main.h +++ b/include/main.h @@ -21,5 +21,6 @@ #include "corona.h" #include "boot.h" #include "libpax_helpers.h" +#include "power.h" #endif \ No newline at end of file diff --git a/include/payload.h b/include/payload.h index bf6a45777..cfa58720e 100644 --- a/include/payload.h +++ b/include/payload.h @@ -3,6 +3,7 @@ #include "sensor.h" #include "sds011read.h" +#include "gpsread.h" // MyDevices CayenneLPP 1.0 channels for Synamic sensor payload format // all payload goes out on LoRa FPort 1 @@ -100,5 +101,6 @@ class PayloadConvert { }; extern PayloadConvert payload; +extern uint8_t batt_level; #endif // _PAYLOAD_H_ diff --git a/include/rcommand.h b/include/rcommand.h index 4d59d75f4..ae4a62f2a 100644 --- a/include/rcommand.h +++ b/include/rcommand.h @@ -11,6 +11,7 @@ #include "cyclic.h" #include "timekeeper.h" #include "timesync.h" +#include "power.h" // maximum number of elements in rcommand interpreter queue #define RCMD_QUEUE_SIZE 5 diff --git a/include/reset.h b/include/reset.h index d55163b24..bffff1bc5 100644 --- a/include/reset.h +++ b/include/reset.h @@ -16,6 +16,15 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, const gpio_num_t wakeup_gpio = GPIO_NUM_MAX); unsigned long long uptime(void); +enum runmode_t { + RUNMODE_POWERCYCLE, + RUNMODE_NORMAL, + RUNMODE_WAKEUP, + RUNMODE_UPDATE, + RUNMODE_SLEEP, + RUNMODE_MAINTENANCE +}; + extern RTC_NOINIT_ATTR runmode_t RTC_runmode; extern RTC_NOINIT_ATTR uint32_t RTC_restarts; diff --git a/include/senddata.h b/include/senddata.h index f8666770c..cc00c7963 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -2,6 +2,7 @@ #define _SENDDATA_H #include +#include "libpax_helpers.h" #include "spislave.h" #include "mqttclient.h" #include "cyclic.h" diff --git a/include/sensor.h b/include/sensor.h index 322c78a70..06ba5fedc 100644 --- a/include/sensor.h +++ b/include/sensor.h @@ -1,6 +1,8 @@ #ifndef _SENSOR_H #define _SENSOR_H +#include "configmanager.h" + #define HAS_SENSORS (HAS_SENSOR_1 || HAS_SENSOR_2 || HAS_SENSOR_3) uint8_t sensor_mask(uint8_t sensor_no); diff --git a/include/timekeeper.h b/include/timekeeper.h index f94557469..35482d402 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -10,8 +10,15 @@ #include "if482.h" #include "dcf77.h" +enum timesource_t { _gps, _rtc, _lora, _set, _unsynced }; + extern const char timeSetSymbols[]; extern Ticker timesyncer; +extern timesource_t timeSource; +extern TaskHandle_t ClockTask; +extern Timezone myTZ; +extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC +extern hw_timer_t *ppsIRQ; void IRAM_ATTR CLOCKIRQ(void); void clock_init(void); diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 55c604dd2..e80b06fd3 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -17,6 +17,8 @@ static const char TAG[] = __FILE__; Preferences nvram; +configData_t cfg; // struct holds current device configuration + static const uint8_t cfgMagicBytes[] = {0x21, 0x76, 0x87, 0x32, 0xf4}; static const size_t cfgLen = sizeof(cfg), cfgLen2 = sizeof(cfgMagicBytes); static uint8_t buffer[cfgLen + cfgLen2]; diff --git a/src/main.cpp b/src/main.cpp index b20d5f34c..9337375c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -89,8 +89,6 @@ triggers pps 1 sec impulse // local Tag for logging static const char TAG[] = __FILE__; -configData_t cfg; // struct holds current device configuration -uint8_t batt_level = 0; // display value char clientId[20] = {0}; // unique ClientID void setup() { diff --git a/src/power.cpp b/src/power.cpp index 6453f1b2d..e918a350c 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -5,6 +5,8 @@ // Local logging tag static const char TAG[] = __FILE__; +uint8_t batt_level = 0; // display value + #ifdef BAT_MEASURE_ADC esp_adc_cal_characteristics_t *adc_characs = (esp_adc_cal_characteristics_t *)calloc( From 0b271f95b3d11ff70a82992cdc50ff8045355190 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 1 Apr 2021 12:17:46 +0200 Subject: [PATCH 36/68] libpax integration 6 (trigger sendcycle) --- README.md | 2 +- include/libpax_helpers.h | 6 ++++-- include/senddata.h | 1 - src/libpax_helpers.cpp | 5 +++-- src/main.cpp | 13 ++++++------- src/rcommand.cpp | 3 ++- src/reset.cpp | 2 +- src/senddata.cpp | 2 -- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 85e67dcac..57601bd06 100644 --- a/README.md +++ b/README.md @@ -443,7 +443,7 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/ 8 = reboot device to maintenance mode (local web server) 9 = reboot device to OTA update via Wifi mode -0x0A set LoRaWAN payload send cycle +0x0A set payload send cycle 0 ... 255 payload send cycle in seconds/2 e.g. 120 -> payload is transmitted each 240 seconds [default] diff --git a/include/libpax_helpers.h b/include/libpax_helpers.h index 597492269..e11958ec8 100644 --- a/include/libpax_helpers.h +++ b/include/libpax_helpers.h @@ -1,11 +1,13 @@ #ifndef _LIBPAX_HELPERS_H #define _LIBPAX_HELPERS_H -#include "globals.h" #include #include +#include "globals.h" +#include "senddata.h" +#include "configmanager.h" -void init_libpax(); +void init_libpax(void); extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values diff --git a/include/senddata.h b/include/senddata.h index cc00c7963..41cad9a33 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -16,7 +16,6 @@ #endif extern struct count_payload_t count_from_libpax; -extern Ticker sendTimer; void SendPayload(uint8_t port); void sendData(void); diff --git a/src/libpax_helpers.cpp b/src/libpax_helpers.cpp index 4cecebbda..3c25856eb 100644 --- a/src/libpax_helpers.cpp +++ b/src/libpax_helpers.cpp @@ -12,10 +12,11 @@ void process_count(void) { count_from_libpax.wifi_count, count_from_libpax.ble_count); libpax_macs_ble = count_from_libpax.ble_count; libpax_macs_wifi = count_from_libpax.wifi_count; + setSendIRQ(); } -void init_libpax() { - libpax_counter_init(process_count, &count_from_libpax, SENDCYCLE * 2 * 1000, +void init_libpax(void) { + libpax_counter_init(process_count, &count_from_libpax, cfg.sendcycle * 2 * 1000, 1); libpax_counter_start(); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9337375c1..00d768ce8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,11 +65,11 @@ MatrixDisplayIRQ-> esp32 timer 3 ButtonIRQ -> external GPIO PMUIRQ -> PMU chip GPIO -fired by software (Ticker.h) -TIMESYNC_IRQ -> setTimeSyncIRQ() -CYCLIC_IRQ -> setCyclicIRQ() -SENDCYCLE_IRQ -> setSendIRQ() -BME_IRQ -> setBMEIRQ() +fired by software +TIMESYNC_IRQ -> setTimeSyncIRQ() -> Ticker.h +CYCLIC_IRQ -> setCyclicIRQ() -> Ticker.h +SENDCYCLE_IRQ -> setSendIRQ() -> xTimer +BME_IRQ -> setBMEIRQ() -> Ticker.h ClockTask (Core 1), see timekeeper.cpp @@ -89,7 +89,7 @@ triggers pps 1 sec impulse // local Tag for logging static const char TAG[] = __FILE__; -char clientId[20] = {0}; // unique ClientID +char clientId[20] = {0}; // unique ClientID void setup() { @@ -484,7 +484,6 @@ void setup() { #endif // HAS_BUTTON // cyclic function interrupts - sendTimer.attach(cfg.sendcycle * 2, setSendIRQ); cyclicTimer.attach(HOMECYCLE, setCyclicIRQ); // only if we have a timesource we do timesync diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 32df12076..346be53c0 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -67,9 +67,10 @@ void set_rssi(uint8_t val[]) { void set_sendcycle(uint8_t val[]) { cfg.sendcycle = val[0]; // update send cycle interrupt [seconds / 2] - sendTimer.attach(cfg.sendcycle * 2, setSendIRQ); ESP_LOGI(TAG, "Remote command: set send cycle to %d seconds", cfg.sendcycle * 2); + libpax_counter_stop(); + init_libpax(); } void set_sleepcycle(uint8_t val[]) { diff --git a/src/reset.cpp b/src/reset.cpp index e7525ac37..d8dcf1532 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -102,7 +102,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { wakeup_gpio = GPIO_NUM_MAX; // stop further enqueuing of senddata and MAC processing - sendTimer.detach(); + libpax_counter_stop(); // switch off radio and other power consuming hardware #if (HAS_SDS011) diff --git a/src/senddata.cpp b/src/senddata.cpp index d76655264..a21ab7aea 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -1,8 +1,6 @@ // Basic Config #include "senddata.h" -Ticker sendTimer; - void setSendIRQ() { xTaskNotify(irqHandlerTask, SENDCYCLE_IRQ, eSetBits); } // put data to send in RTos Queues used for transmit over channels Lora and SPI From d098d6ada37931ac3625ebc58c634345a1b9d96e Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 6 Apr 2021 14:30:57 +0200 Subject: [PATCH 37/68] fix issue #781 --- src/reset.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reset.cpp b/src/reset.cpp index d8dcf1532..56fbf98d1 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -109,9 +109,6 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { sds011_sleep(); #endif - // halt interrupts accessing i2c bus - mask_user_IRQ(); - // wait a while (max 100 sec) to clear send queues ESP_LOGI(TAG, "Waiting until send queues are empty..."); for (i = 100; i > 0; i--) { @@ -159,6 +156,9 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { digitalWrite(EXT_POWER_SW, EXT_POWER_OFF); #endif + // halt interrupts accessing i2c bus + mask_user_IRQ(); + // shutdown i2c bus i2c_deinit(); From 4d7d92900ee1f1e3e7ecbd3d7c4c4b0c46dc81d2 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 6 Apr 2021 21:35:25 +0200 Subject: [PATCH 38/68] added Going-to-sleep display message --- include/display.h | 3 ++- src/display.cpp | 11 +++++++++-- src/reset.cpp | 7 ++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/display.h b/include/display.h index 338f62d58..67191c5bc 100644 --- a/include/display.h +++ b/include/display.h @@ -81,12 +81,13 @@ const uint8_t QR_SCALEFACTOR = (MY_DISPLAY_HEIGHT - 4) / 29; // 4px borderlines extern uint8_t DisplayIsOn, displaybuf[]; extern hw_timer_t *displayIRQ; -extern uint8_t volatile channel; // wifi channel rotation counter +extern uint8_t volatile channel; // wifi channel rotation counter void dp_setup(int contrast = 0); void dp_refresh(bool nextPage = false); void dp_init(bool verbose = false); void dp_shutdown(void); +void dp_message(const char *msg, int line, bool invers); void dp_drawPage(time_t t, bool nextpage); void dp_println(int lines = 1); void dp_printf(const char *format, ...); diff --git a/src/display.cpp b/src/display.cpp index 2656246bc..e535e4059 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -345,7 +345,7 @@ void dp_drawPage(time_t t, bool nextpage) { dp_println(); #endif // TIME_SYNC_INTERVAL - // line 7: LORA network status + // line 7: LMIC status // yyyyyyyyyyyyy xx SFab #if (HAS_LORA) @@ -355,7 +355,6 @@ void dp_drawPage(time_t t, bool nextpage) { dp_setFont(MY_FONT_SMALL, !cfg.adrmode); dp_printf("%-4s", getSfName(updr2rps(LMIC.datarate))); dp_setFont(MY_FONT_SMALL, 0); - dp_println(); #endif // HAS_LORA break; @@ -617,6 +616,14 @@ void dp_shutdown(void) { #endif } +// print static message on display +void dp_message(const char *msg, int line, bool invers) { + dp_setTextCursor(0, line); + dp_setFont(MY_FONT_NORMAL, invers ? 1 : 0); + dp_printf("%-16s", msg); + dp_dump(displaybuf); +} // dp_message + // ------------- QR code plotter ----------------- void dp_printqr(uint16_t offset_x, uint16_t offset_y, const char *Message) { diff --git a/src/reset.cpp b/src/reset.cpp index 56fbf98d1..58cc6d38a 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -97,6 +97,11 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { RTC_runmode = RUNMODE_SLEEP; int i; + // show message on display +#ifdef HAS_DISPLAY + dp_message("-GOING TO SLEEP-", 7, true); +#endif + // validate wake up pin, if we have if (!GPIO_IS_VALID_GPIO(wakeup_gpio)) wakeup_gpio = GPIO_NUM_MAX; @@ -117,7 +122,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { vTaskDelay(pdMS_TO_TICKS(1000)); } - // shutdown LMIC safely, waiting max 100 sec +/// wait until LMIC is idle #if (HAS_LORA) ESP_LOGI(TAG, "Waiting until LMIC is idle..."); for (i = 100; i > 0; i--) { From 8a8128ed857f42cc7aedf19629d00355b483c494 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 6 Apr 2021 22:45:34 +0200 Subject: [PATCH 39/68] preserve time-of-day during deep sleep --- src/reset.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/reset.cpp b/src/reset.cpp index 58cc6d38a..33a724169 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -15,6 +15,7 @@ RTC_NOINIT_ATTR uint32_t RTC_restarts; // RTC_DATA_ATTR -> keep values after a wakeup from sleep RTC_DATA_ATTR struct timeval RTC_sleep_start_time; RTC_DATA_ATTR unsigned long long RTC_millis = 0; +RTC_DATA_ATTR time_t RTC_time = 0; timeval sleep_stop_time; @@ -65,8 +66,12 @@ void do_after_reset(void) { sleep_time_ms = (sleep_stop_time.tv_sec - RTC_sleep_start_time.tv_sec) * 1000 + (sleep_stop_time.tv_usec - RTC_sleep_start_time.tv_usec) / 1000; - ESP_LOGI(TAG, "Time spent in deep sleep: %d ms", sleep_time_ms); RTC_millis += sleep_time_ms; // increment system monotonic time + ESP_LOGI(TAG, "Time spent in deep sleep: %d ms", sleep_time_ms); + + // set time + setMyTime(RTC_time + sleep_time_ms / 1000, sleep_time_ms % 1000, _set); + // set wakeup state, not if we have pending OTA update if (RTC_runmode == RUNMODE_SLEEP) RTC_runmode = RUNMODE_WAKEUP; @@ -184,6 +189,7 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { // time stamp sleep start time and save system monotonic time. Deep sleep. gettimeofday(&RTC_sleep_start_time, NULL); RTC_millis += millis(); + RTC_time = now(); ESP_LOGI(TAG, "Going to sleep, good bye."); esp_deep_sleep_start(); } From e5161be5afd5bdf71c011a734e48f6477162837e Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 6 Apr 2021 23:28:21 +0200 Subject: [PATCH 40/68] update readme.md (time sync) --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 57601bd06..479e9280e 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,7 @@ Paxcounter supports a battery friendly power saving mode. In this mode the devic # Time sync -Paxcounter can keep it's time-of-day synced with an external time source. Set *#define TIME_SYNC_INTERVAL* in paxcounter.conf to enable time sync. Supported external time sources are GPS, LORAWAN network time and LORAWAN application timeserver time. An on board DS3231 RTC is kept sycned as fallback time source. Time accuracy depends on board's time base which generates the pulse per second. Supported are GPS PPS, SQW output of RTC, and internal ESP32 hardware timer. Time base is selected by #defines in the board's hal file, see example in [**generic.h**](src/hal/generic.h). Bonus: If your LORAWAN network does not support network time, you can run a Node-Red timeserver application using the enclosed [**Timeserver code**](/src/Node-RED/Timeserver.json). Configure MQTT nodes in Node-Red for the LORAWAN application used by paxocunter device. +Paxcounter can keep a time-of-day synced with external or on board time sources. Set *#define TIME_SYNC_INTERVAL* in paxcounter.conf to enable time sync. Supported external time sources are GPS, LORAWAN network time and LORAWAN application timeserver time. Supported on board time sources are the RTC of ESP32 and a DS3231 RTC chip, both are kept sycned as fallback time sources. Time accuracy depends on board's time base which generates the pulse per second. Supported are GPS PPS, SQW output of RTC, and internal ESP32 hardware timer. Time base is selected by #defines in the board's hal file, see example in [**generic.h**](src/hal/generic.h). Bonus: If your LORAWAN network does not support network time, you can run a Node-Red timeserver application using the enclosed [**Timeserver code**](/src/Node-RED/Timeserver.json). Configure the MQTT nodes in Node-Red for the LORAWAN application used by your paxocunter device. Time can also be set without precision liability, by simple remote command, see section remote control. # Wall clock controller @@ -567,6 +567,7 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/ 0x01 = RTC 0x02 = LORA 0x03 = unsynched (never synched) + 0x04 = set (source unknown) bits 4..7 time status 0x00 = timeNotSet (never synched) From f3a70c16c81126663269482732b0665f00f0c229 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 6 Apr 2021 23:37:51 +0200 Subject: [PATCH 41/68] timekeeper.cpp: update comments time source --- src/timekeeper.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 9019fd3b7..8ce89be92 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -12,6 +12,7 @@ static const char TAG[] = __FILE__; // symbol to display current time source +// G = GPS / R = RTC / L = LORA / S = external Source / ? = unsynced const char timeSetSymbols[] = {'G', 'R', 'L', 'S', '?'}; // set Time Zone for user setting from paxcounter.conf From c9b099020374ef7c6624a4db5e7da248fa46bb1f Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 7 Apr 2021 00:27:40 +0200 Subject: [PATCH 42/68] globals.h: fix improper placed #endif --- include/globals.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/globals.h b/include/globals.h index b64790482..b7259ffd5 100644 --- a/include/globals.h +++ b/include/globals.h @@ -1,6 +1,5 @@ #ifndef _GLOBALS_H #define _GLOBALS_H -#endif // The mother of all embedded development... #include @@ -47,6 +46,7 @@ for (;;) \ ; \ } +#endif #define _seconds() millis() / 1000.0 @@ -118,4 +118,4 @@ extern std::array beacons; extern char clientId[20]; // unique clientID -#endif +#endif \ No newline at end of file From 5c5752805b2635fc5bae8af9d458b731db16ba18 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 7 Apr 2021 15:15:41 +0200 Subject: [PATCH 43/68] remove default argument for saveConfig() --- include/configmanager.h | 2 +- src/bmesensor.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/configmanager.h b/include/configmanager.h index e9c5e7c06..a8be749cc 100644 --- a/include/configmanager.h +++ b/include/configmanager.h @@ -7,7 +7,7 @@ extern configData_t cfg; -void saveConfig(bool erase = false); +void saveConfig(bool erase); bool loadConfig(void); void eraseConfig(void); int version_compare(const String v1, const String v2); diff --git a/src/bmesensor.cpp b/src/bmesensor.cpp index 6baa0f1c7..eada87ff2 100644 --- a/src/bmesensor.cpp +++ b/src/bmesensor.cpp @@ -197,7 +197,7 @@ void updateState(void) { memcpy(cfg.bsecstate, bsecstate_buffer, BSEC_MAX_STATE_BLOB_SIZE); cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] = BSEC_MAX_STATE_BLOB_SIZE; ESP_LOGI(TAG, "saving BSEC state to NVRAM"); - saveConfig(); + saveConfig(false); } } #endif From c00612ad76906c3fc67151c1f6dc5d83926f01b8 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 7 Apr 2021 20:20:31 +0200 Subject: [PATCH 44/68] 2nd fix issue #781 --- src/timekeeper.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 8ce89be92..194e64642 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -97,8 +97,8 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, vTaskDelay(pdMS_TO_TICKS(1000 - t_msec % 1000)); } - ESP_LOGI(TAG, "[%0.3f] UTC time: %d.%03d sec", _seconds(), - time_to_set, t_msec % 1000); + ESP_LOGI(TAG, "[%0.3f] UTC time: %d.%03d sec", _seconds(), time_to_set, + t_msec % 1000); // if we have got an external timesource, set RTC time and shift RTC_INT pulse // to top of second @@ -107,11 +107,11 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, set_rtctime(time_to_set); #endif -// if we have a software pps timer, shift it to top of second -#if (!defined GPS_INT && !defined RTC_INT) - timerWrite(ppsIRQ, 0); // reset pps timer - CLOCKIRQ(); // fire clock pps, this advances time 1 sec -#endif + // if we have a software pps timer, shift it to top of second + if (ppsIRQ != NULL) { + timerWrite(ppsIRQ, 0); // reset pps timer + CLOCKIRQ(); // fire clock pps, this advances time 1 sec + } setTime(time_to_set); // set the time on top of second @@ -122,8 +122,11 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, } else { timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ); time_t unix_sec_at_compilation = compiledUTC(); - ESP_LOGD(TAG, "[%0.3f] Failed to synchronise time from source %c | unix sec obtained from source: %d | unix sec at program compilation: %d", - _seconds(), timeSetSymbols[mytimesource], time_to_set, unix_sec_at_compilation); + ESP_LOGD(TAG, + "[%0.3f] Failed to synchronise time from source %c | unix sec " + "obtained from source: %d | unix sec at program compilation: %d", + _seconds(), timeSetSymbols[mytimesource], time_to_set, + unix_sec_at_compilation); } } From c2336397ce7cce9cea8e1e512955b41bb58925b2 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 7 Apr 2021 21:13:16 +0200 Subject: [PATCH 45/68] fix issue #786 --- include/timekeeper.h | 5 +++-- src/timekeeper.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/timekeeper.h b/include/timekeeper.h index 35482d402..ac85223fa 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -10,7 +10,7 @@ #include "if482.h" #include "dcf77.h" -enum timesource_t { _gps, _rtc, _lora, _set, _unsynced }; +enum timesource_t { _gps, _rtc, _lora, _unsynced, _set }; extern const char timeSetSymbols[]; extern Ticker timesyncer; @@ -28,7 +28,8 @@ void setTimeSyncIRQ(void); uint8_t timepulse_init(void); time_t timeIsValid(time_t const t); void calibrateTime(void); -void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t mytimesource); +void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, + timesource_t mytimesource); time_t compiledUTC(void); TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPins); diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 194e64642..f8a2ffe7b 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -13,7 +13,7 @@ static const char TAG[] = __FILE__; // symbol to display current time source // G = GPS / R = RTC / L = LORA / S = external Source / ? = unsynced -const char timeSetSymbols[] = {'G', 'R', 'L', 'S', '?'}; +const char timeSetSymbols[] = {'G', 'R', 'L', '?', 'S'}; // set Time Zone for user setting from paxcounter.conf TimeChangeRule myDST = DAYLIGHT_TIME; From 3268824eb5e03aca51f53d3e3ccb6e23576260ba Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 7 Apr 2021 21:21:06 +0200 Subject: [PATCH 46/68] GPS time sync only if GPS time is valid --- src/gpsread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 7801a4cec..79c0847dd 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -164,7 +164,7 @@ void gps_loop(void *pvParameters) { // (only) while device time is not set or unsynched, and we have a valid // GPS time, we trigger a device time update to poll time from GPS if ((timeSource == _unsynced || timeSource == _set) && - gpstime.isUpdated()) { + (gpstime.isUpdated() && gpstime.isValid() && gpstime.age() < 1000)) { now(); calibrateTime(); } From e299444a894d38f70be872f489a07be0890345cc Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 7 Apr 2021 21:29:53 +0200 Subject: [PATCH 47/68] change timesource symbol S to blank --- src/timekeeper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index f8a2ffe7b..48bc1e9a8 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -12,8 +12,8 @@ static const char TAG[] = __FILE__; // symbol to display current time source -// G = GPS / R = RTC / L = LORA / S = external Source / ? = unsynced -const char timeSetSymbols[] = {'G', 'R', 'L', '?', 'S'}; +// G = GPS / R = RTC / L = LORA / ? = unsynced / = sync unknown +const char timeSetSymbols[] = {'G', 'R', 'L', '?', ' '}; // set Time Zone for user setting from paxcounter.conf TimeChangeRule myDST = DAYLIGHT_TIME; From 0b85559358234f1f6a06e8754b76fb0c2211b2b0 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 8 Apr 2021 13:58:38 +0200 Subject: [PATCH 48/68] reset.cpp: skip libpax_counter_stop() before sleep --- src/reset.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/reset.cpp b/src/reset.cpp index 33a724169..6399dc954 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -111,10 +111,11 @@ void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { if (!GPIO_IS_VALID_GPIO(wakeup_gpio)) wakeup_gpio = GPIO_NUM_MAX; - // stop further enqueuing of senddata and MAC processing - libpax_counter_stop(); + // stop further enqueuing of senddata and MAC processing + // -> skipped, because shutting down bluedroid stack tends to crash + // libpax_counter_stop(); - // switch off radio and other power consuming hardware + // switch off any power consuming hardware #if (HAS_SDS011) sds011_sleep(); #endif From c62ba705676e6dfbdfacb738152f14fa3938a9bf Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Thu, 8 Apr 2021 18:19:39 +0200 Subject: [PATCH 49/68] fix timesync timesource debug output --- src/timekeeper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 48bc1e9a8..45f0d153e 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -117,8 +117,8 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timeSource = mytimesource; // set global variable timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ); - ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | source: %c", - _seconds(), timeSetSymbols[mytimesource]); + ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | timesource=%d", + _seconds(), mytimesource); } else { timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ); time_t unix_sec_at_compilation = compiledUTC(); From e67b449fdd0e709f5ede48a871c252d2b601f9d0 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 12 Apr 2021 13:15:17 +0200 Subject: [PATCH 50/68] v2.5.0 --- platformio_orig.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platformio_orig.ini b/platformio_orig.ini index 48d42c5c9..7817f5b42 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -49,7 +49,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.4.0 +release_version = 2.5.0 ; 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 @@ -64,7 +64,7 @@ display_library = ; set by build.py and taken from hal file lib_deps_lora = mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 lib_deps_display = - bitbank2/OneBitDisplay @ ^1.9.1 + bitbank2/OneBitDisplay @ ^1.10.0 bitbank2/BitBang_I2C @ ^2.1.3 ricmoo/QRCode @ ^0.0.1 bodmer/TFT_eSPI @ ^2.3.51 From fc1ac508efcd05b0f1a8d745941f7ab015c09698 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 12 Apr 2021 17:51:29 +0200 Subject: [PATCH 51/68] mqttclient.cpp: Set Ethernet hostname --- src/mqttclient.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index dd0770f8a..d7ddb4bfc 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -22,6 +22,7 @@ esp_err_t mqtt_init(void) { // setup network connection and MQTT client ETH.begin(); + ETH.setHostname(clientId); mqttClient.begin(MQTT_SERVER, MQTT_PORT, netClient); mqttClient.setKeepAlive(MQTT_KEEPALIVE); mqttClient.onMessageAdvanced(mqtt_callback); From ea7fa5d31f04aef93e715d8f72bff9f483e11121 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 12 Apr 2021 23:44:07 +0200 Subject: [PATCH 52/68] reset.cpp: bugfix reset vars in coldstart --- src/reset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reset.cpp b/src/reset.cpp index 6399dc954..f410968d1 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -33,7 +33,7 @@ void do_reset(bool warmstart) { LMIC_shutdown(); } #endif - RTC_runmode = RUNMODE_POWERCYCLE; + reset_rtc_vars(void); ESP_LOGI(TAG, "restarting device (coldstart)"); } esp_restart(); From 13c8a5f0be118a9902d0d1ed9fdee426b07a133a Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 12 Apr 2021 23:45:26 +0200 Subject: [PATCH 53/68] reset.cpp: bugfix coldstart (2) --- src/reset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reset.cpp b/src/reset.cpp index f410968d1..5dce2d200 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -33,7 +33,7 @@ void do_reset(bool warmstart) { LMIC_shutdown(); } #endif - reset_rtc_vars(void); + reset_rtc_vars(); ESP_LOGI(TAG, "restarting device (coldstart)"); } esp_restart(); From 44e5daaf029082a39166ebed3f7d8594cd94a542 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 13 Apr 2021 01:57:02 +0200 Subject: [PATCH 54/68] bugfix libpax countermode --- src/libpax_helpers.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libpax_helpers.cpp b/src/libpax_helpers.cpp index 3c25856eb..f75ca91f7 100644 --- a/src/libpax_helpers.cpp +++ b/src/libpax_helpers.cpp @@ -16,7 +16,7 @@ void process_count(void) { } void init_libpax(void) { - libpax_counter_init(process_count, &count_from_libpax, cfg.sendcycle * 2 * 1000, - 1); + libpax_counter_init(process_count, &count_from_libpax, + cfg.sendcycle * 2 * 1000, cfg.countermode); libpax_counter_start(); } \ No newline at end of file From 71aa55863baeeabe9754262a5e630685d9c4d6a3 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 13 Apr 2021 16:37:17 +0200 Subject: [PATCH 55/68] libpax integration fixes (sendtimer, rcommands) --- README.md | 6 +++--- include/main.h | 1 + include/senddata.h | 3 ++- src/main.cpp | 6 +++++- src/rcommand.cpp | 21 ++++++++++++++++++++- src/senddata.cpp | 20 +++++++++++++++++--- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 479e9280e..d6fd9d676 100644 --- a/README.md +++ b/README.md @@ -445,14 +445,14 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/ 0x0A set payload send cycle - 0 ... 255 payload send cycle in seconds/2 + 5 ... 255 payload send cycle in seconds/2 e.g. 120 -> payload is transmitted each 240 seconds [default] 0x0B set Wifi channel hopping interval timer 0 ... 255 duration for scanning a wifi channel in seconds/100 e.g. 50 -> each channel is scanned for 500 milliseconds [default] - 0 means no hopping, scanning on channel WIFI_CHANNEL_MIN only + 0 means no hopping, scanning on fixed single channel WIFI_CHANNEL_1 0x0C set Bluetooth channel switch interval timer @@ -524,7 +524,7 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/ 0x19 set sleep cycle - bytes 1..2 = device sleep cycle in seconds/10 (MSB) + bytes 1..2 = device sleep cycle in seconds/10 (MSB), 1 ... 255 e.g. {0x04, 0xB0} -> device sleeps 20 minutes after each send cycle [default = 0] 0x20 load device configuration diff --git a/include/main.h b/include/main.h index 8a46c6e4b..7530312fb 100644 --- a/include/main.h +++ b/include/main.h @@ -5,6 +5,7 @@ #include // needed for Wifi event handler #include // needed for timers #include // needed for coex version display +#include // needed for wifi init / deinit #include "globals.h" #include "reset.h" diff --git a/include/senddata.h b/include/senddata.h index 41cad9a33..5d707a769 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -22,6 +22,7 @@ void sendData(void); void checkSendQueues(void); void flushQueues(void); bool allQueuesEmtpy(void); -void setSendIRQ(void); +void setSendIRQ(TimerHandle_t xTimer = NULL); +void initSendDataTimer(uint8_t sendcycle); #endif // _SENDDATA_H_ diff --git a/src/main.cpp b/src/main.cpp index 00d768ce8..f1a977a89 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -281,8 +281,9 @@ void setup() { if (RTC_runmode == RUNMODE_MAINTENANCE) start_boot_menu(); +#if ((WIFICOUNTER) || (BLECOUNTER)) + // use libpax timer to trigger cyclic senddata ESP_LOGI(TAG, "Starting libpax..."); -#if (defined WIFICOUNTER || defined BLECOUNTER) struct libpax_config_t configuration; libpax_default_config(&configuration); @@ -304,6 +305,9 @@ void setup() { } else { init_libpax(); } +#else + // use stand alone timer to trigger cyclic senddata + initSendDataTimer(cfg.sendcycle * 2); #endif #if (BLECOUNTER) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 346be53c0..287d5c771 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -55,27 +55,38 @@ void set_reset(uint8_t val[]) { void set_rssi(uint8_t val[]) { cfg.rssilimit = val[0] * -1; +#if ((WIFICOUNTER) || (BLECOUNTER)) libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.wifi_rssi_threshold = cfg.rssilimit; libpax_update_config(¤t_config); init_libpax(); +#endif ESP_LOGI(TAG, "Remote command: set RSSI limit to %d", cfg.rssilimit); } void set_sendcycle(uint8_t val[]) { - cfg.sendcycle = val[0]; + if (val[0] < 5) + return; // update send cycle interrupt [seconds / 2] + cfg.sendcycle = val[0]; ESP_LOGI(TAG, "Remote command: set send cycle to %d seconds", cfg.sendcycle * 2); +#if ((WIFICOUNTER) || (BLECOUNTER)) libpax_counter_stop(); init_libpax(); +#else + // modify senddata timer + initSendDataTimer(cfg.sendcycle * 2); +#endif } void set_sleepcycle(uint8_t val[]) { // swap byte order from msb to lsb, note: this is a platform dependent hack uint16_t t = __builtin_bswap16(*(uint16_t *)(val)); + if (t == 0) + return; cfg.sleepcycle = t; ESP_LOGI(TAG, "Remote command: set sleep cycle to %d seconds", cfg.sleepcycle * 10); @@ -83,6 +94,7 @@ void set_sleepcycle(uint8_t val[]) { void set_wifichancycle(uint8_t val[]) { cfg.wifichancycle = val[0]; +#if (WIFICOUNTER) libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); @@ -100,16 +112,19 @@ void set_wifichancycle(uint8_t val[]) { current_config.wifi_channel_switch_interval = cfg.wifichancycle; libpax_update_config(¤t_config); init_libpax(); +#endif } void set_blescantime(uint8_t val[]) { cfg.blescantime = val[0]; +#if (BLECOUNTER) libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.blescantime = cfg.blescantime; libpax_update_config(¤t_config); init_libpax(); +#endif } void set_countmode(uint8_t val[]) { @@ -257,24 +272,28 @@ void set_loraadr(uint8_t val[]) { void set_blescan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val[0] ? "on" : "off"); cfg.blescan = val[0] ? 1 : 0; +#if (BLECOUNTER) libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.blecounter = cfg.blescan; libpax_update_config(¤t_config); init_libpax(); +#endif } void set_wifiscan(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: set WIFI scanner to %s", val[0] ? "on" : "off"); cfg.wifiscan = val[0] ? 1 : 0; +#if (WIFICOUNTER) libpax_counter_stop(); libpax_config_t current_config; libpax_get_current_config(¤t_config); current_config.wificounter = cfg.wifiscan; libpax_update_config(¤t_config); init_libpax(); +#endif } void set_wifiant(uint8_t val[]) { diff --git a/src/senddata.cpp b/src/senddata.cpp index a21ab7aea..5a3107615 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -1,7 +1,22 @@ // Basic Config #include "senddata.h" -void setSendIRQ() { xTaskNotify(irqHandlerTask, SENDCYCLE_IRQ, eSetBits); } +void setSendIRQ(TimerHandle_t xTimer) { + xTaskNotify(irqHandlerTask, SENDCYCLE_IRQ, eSetBits); +} + +void initSendDataTimer(uint8_t sendcycle) { + static TimerHandle_t SendDataTimer = NULL; + + if (SendDataTimer == NULL) { + SendDataTimer = + xTimerCreate("SendDataTimer", pdMS_TO_TICKS(sendcycle * 1000), pdTRUE, + (void *)0, setSendIRQ); + xTimerStart(SendDataTimer, 0); + } else { + xTimerChangePeriod(SendDataTimer, pdMS_TO_TICKS(sendcycle * 1000), 0); + } +} // put data to send in RTos Queues used for transmit over channels Lora and SPI void SendPayload(uint8_t port) { @@ -86,8 +101,7 @@ void sendData() { ESP_LOGI(TAG, "Sending libpax wifi count: %d", libpax_macs_wifi); payload.addCount(libpax_macs_wifi, MAC_SNIFF_WIFI); if (cfg.blescan) { - ESP_LOGI(TAG, "Sending libpax ble count: %d", - libpax_macs_ble); + ESP_LOGI(TAG, "Sending libpax ble count: %d", libpax_macs_ble); payload.addCount(libpax_macs_ble, MAC_SNIFF_BLE); } #endif From 423fff1fef9858c37258d09a94e2957b154fb525 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 26 Apr 2021 10:43:02 +0200 Subject: [PATCH 56/68] fix LOPY4 include error antenna_init() --- include/main.h | 1 + include/rcommand.h | 1 + 2 files changed, 2 insertions(+) diff --git a/include/main.h b/include/main.h index 7530312fb..b41c901a9 100644 --- a/include/main.h +++ b/include/main.h @@ -23,5 +23,6 @@ #include "boot.h" #include "libpax_helpers.h" #include "power.h" +#include "antenna.h" #endif \ No newline at end of file diff --git a/include/rcommand.h b/include/rcommand.h index ae4a62f2a..756895977 100644 --- a/include/rcommand.h +++ b/include/rcommand.h @@ -12,6 +12,7 @@ #include "timekeeper.h" #include "timesync.h" #include "power.h" +#include "antenna.h" // maximum number of elements in rcommand interpreter queue #define RCMD_QUEUE_SIZE 5 From 0f50fe3e3007b0d9045cfb3712e98c38b60fa0e2 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 3 May 2021 18:50:12 +0200 Subject: [PATCH 57/68] remove BintrayCertificates.h --- lib/BintrayClient/src/BintrayCertificates.h | 105 -------------------- lib/BintrayClient/src/BintrayClient.cpp | 1 - 2 files changed, 106 deletions(-) delete mode 100644 lib/BintrayClient/src/BintrayCertificates.h diff --git a/lib/BintrayClient/src/BintrayCertificates.h b/lib/BintrayClient/src/BintrayCertificates.h deleted file mode 100644 index 89854c84a..000000000 --- a/lib/BintrayClient/src/BintrayCertificates.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - Parts of this file - Copyright (c) 2014-present PlatformIO - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -**/ - -#ifndef BINTRAY_CERTIFICATES_H -#define BINTRAY_CERTIFICATES_H - -const char* BINTRAY_API_ROOT_CA = \ -"-----BEGIN CERTIFICATE-----\n" -"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" -"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" -"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" -"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" -"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" -"b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" -"9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" -"CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" -"nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" -"43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" -"T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" -"gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" -"BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" -"TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" -"DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" -"hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" -"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" -"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" -"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" -"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" -"-----END CERTIFICATE-----\n"; - -const char* BINTRAY_AKAMAI_ROOT_CA = \ -"-----BEGIN CERTIFICATE-----\n"\ -"MIIElDCCA3ygAwIBAgIQAf2j627KdciIQ4tyS8+8kTANBgkqhkiG9w0BAQsFADBh\n"\ -"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"\ -"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n"\ -"QTAeFw0xMzAzMDgxMjAwMDBaFw0yMzAzMDgxMjAwMDBaME0xCzAJBgNVBAYTAlVT\n"\ -"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxJzAlBgNVBAMTHkRpZ2lDZXJ0IFNIQTIg\n"\ -"U2VjdXJlIFNlcnZlciBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n"\ -"ANyuWJBNwcQwFZA1W248ghX1LFy949v/cUP6ZCWA1O4Yok3wZtAKc24RmDYXZK83\n"\ -"nf36QYSvx6+M/hpzTc8zl5CilodTgyu5pnVILR1WN3vaMTIa16yrBvSqXUu3R0bd\n"\ -"KpPDkC55gIDvEwRqFDu1m5K+wgdlTvza/P96rtxcflUxDOg5B6TXvi/TC2rSsd9f\n"\ -"/ld0Uzs1gN2ujkSYs58O09rg1/RrKatEp0tYhG2SS4HD2nOLEpdIkARFdRrdNzGX\n"\ -"kujNVA075ME/OV4uuPNcfhCOhkEAjUVmR7ChZc6gqikJTvOX6+guqw9ypzAO+sf0\n"\ -"/RR3w6RbKFfCs/mC/bdFWJsCAwEAAaOCAVowggFWMBIGA1UdEwEB/wQIMAYBAf8C\n"\ -"AQAwDgYDVR0PAQH/BAQDAgGGMDQGCCsGAQUFBwEBBCgwJjAkBggrBgEFBQcwAYYY\n"\ -"aHR0cDovL29jc3AuZGlnaWNlcnQuY29tMHsGA1UdHwR0MHIwN6A1oDOGMWh0dHA6\n"\ -"Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEdsb2JhbFJvb3RDQS5jcmwwN6A1\n"\ -"oDOGMWh0dHA6Ly9jcmw0LmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEdsb2JhbFJvb3RD\n"\ -"QS5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEWHGh0dHBzOi8v\n"\ -"d3d3LmRpZ2ljZXJ0LmNvbS9DUFMwHQYDVR0OBBYEFA+AYRyCMWHVLyjnjUY4tCzh\n"\ -"xtniMB8GA1UdIwQYMBaAFAPeUDVW0Uy7ZvCj4hsbw5eyPdFVMA0GCSqGSIb3DQEB\n"\ -"CwUAA4IBAQAjPt9L0jFCpbZ+QlwaRMxp0Wi0XUvgBCFsS+JtzLHgl4+mUwnNqipl\n"\ -"5TlPHoOlblyYoiQm5vuh7ZPHLgLGTUq/sELfeNqzqPlt/yGFUzZgTHbO7Djc1lGA\n"\ -"8MXW5dRNJ2Srm8c+cftIl7gzbckTB+6WohsYFfZcTEDts8Ls/3HB40f/1LkAtDdC\n"\ -"2iDJ6m6K7hQGrn2iWZiIqBtvLfTyyRRfJs8sjX7tN8Cp1Tm5gr8ZDOo0rwAhaPit\n"\ -"c+LJMto4JQtV05od8GiG7S5BNO98pVAdvzr508EIDObtHopYJeS4d60tbvVS3bR0\n"\ -"j6tJLp07kzQoH3jOlOrHvdPJbRzeXDLz\n"\ -"-----END CERTIFICATE-----\n"; - -const char* CLOUDFRONT_API_ROOT_CA = \ -"-----BEGIN CERTIFICATE-----\n"\ -"MIIE3zCCA8egAwIBAgIQYxgNOPuAl3ip0DWjFhj4QDANBgkqhkiG9w0BAQsFADCB\n"\ -"yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL\n"\ -"ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp\n"\ -"U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW\n"\ -"ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0\n"\ -"aG9yaXR5IC0gRzUwHhcNMTcxMTA2MDAwMDAwWhcNMjIxMTA1MjM1OTU5WjBhMQsw\n"\ -"CQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cu\n"\ -"ZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBHMjCC\n"\ -"ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALs3zTTce2vJsmiQrUp1/0a6\n"\ -"IQoIjfUZVMn7iNvzrvI6iZE8euarBhprz6wt6F4JJES6Ypp+1qOofuBUdSAFrFC3\n"\ -"nGMabDDc2h8Zsdce3v3X4MuUgzeu7B9DTt17LNK9LqUv5Km4rTrUmaS2JembawBg\n"\ -"kmD/TyFJGPdnkKthBpyP8rrptOmSMmu181foXRvNjB2rlQSVSfM1LZbjSW3dd+P7\n"\ -"SUu0rFUHqY+Vs7Qju0xtRfD2qbKVMLT9TFWMJ0pXFHyCnc1zktMWSgYMjFDRjx4J\n"\ -"vheh5iHK/YPlELyDpQrEZyj2cxQUPUZ2w4cUiSE0Ta8PRQymSaG6u5zFsTODKYUC\n"\ -"AwEAAaOCAScwggEjMB0GA1UdDgQWBBROIlQgGJXm427mD/r6uRLtBhePOTAPBgNV\n"\ -"HRMBAf8EBTADAQH/MF8GA1UdIARYMFYwVAYEVR0gADBMMCMGCCsGAQUFBwIBFhdo\n"\ -"dHRwczovL2Quc3ltY2IuY29tL2NwczAlBggrBgEFBQcCAjAZDBdodHRwczovL2Qu\n"\ -"c3ltY2IuY29tL3JwYTAvBgNVHR8EKDAmMCSgIqAghh5odHRwOi8vcy5zeW1jYi5j\n"\ -"b20vcGNhMy1nNS5jcmwwDgYDVR0PAQH/BAQDAgGGMC4GCCsGAQUFBwEBBCIwIDAe\n"\ -"BggrBgEFBQcwAYYSaHR0cDovL3Muc3ltY2QuY29tMB8GA1UdIwQYMBaAFH/TZafC\n"\ -"3ey78DAJ80M5+gKvMzEzMA0GCSqGSIb3DQEBCwUAA4IBAQBQ3dNWKSUBip6n5X1N\n"\ -"ua8bjKLSJzXlnescavPECMpFBlIIKH2mc6mL2Xr/wkSIBDrsqAO3sBcmoJN+n8V3\n"\ -"0O5JelrtEAFYSyRDXfu78ZlHn6kvV5/jPUFECEM/hdN0x8WdLpGjJMqfs0EG5qHj\n"\ -"+UaxpucWD445wea4zlK7hUR+MA8fq0Yd1HEKj4c8TcgaQIHMa4KHr448cQ69e3CP\n"\ -"ECRhRNg+RAKT2I7SlaVzLvaB/8yym2oMCEsoqiRT8dbXg35aKEYmmzn3O/mnB7bG\n"\ -"Ud/EUrkIf7FVamgYZd1fSzQeg1cHqf0ja6eHpvq2bTl+cWFHaq/84KlHe5Rh0Csm\n"\ -"pZzn\n"\ -"-----END CERTIFICATE-----\n"; - -#endif // BINTRAY_CERTIFICATES_H diff --git a/lib/BintrayClient/src/BintrayClient.cpp b/lib/BintrayClient/src/BintrayClient.cpp index 5c3012908..55af3f9fd 100644 --- a/lib/BintrayClient/src/BintrayClient.cpp +++ b/lib/BintrayClient/src/BintrayClient.cpp @@ -20,7 +20,6 @@ #include #include "BintrayClient.h" -#include "BintrayCertificates.h" BintrayClient::BintrayClient(const String &user, const String &repository, const String &package) : m_user(user), m_repo(repository), m_package(package), From 35f27ca4150d51fcece1c4a887354d62124e7827 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 3 May 2021 19:36:22 +0200 Subject: [PATCH 58/68] rcommand get_time patch timestamp --- src/rcommand.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 287d5c771..95a2733b0 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -381,8 +381,9 @@ void get_batt(uint8_t val[]) { void get_time(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: get time"); + time_t t = now(); payload.reset(); - payload.addTime(now()); + payload.addTime(t); payload.addByte(timeStatus() << 4 | timeSource); SendPayload(TIMEPORT); }; From ccf9e80c01d02efa982d96eeb22d63fc29b55110 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 3 May 2021 19:37:39 +0200 Subject: [PATCH 59/68] add COMPILETIME macro --- src/rtctime.cpp | 2 +- src/timekeeper.cpp | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 29ad18c6a..cb87c9ded 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -32,7 +32,7 @@ uint8_t rtc_init(void) { if (!Rtc.IsDateTimeValid() || !timeIsValid(t)) { ESP_LOGW(TAG, "RTC has no recent time, setting to compiled time"); Rtc.SetDateTime( - RtcDateTime(compiledUTC() - SECS_YR_2000)); // epoch -> sec2000 + RtcDateTime(_COMPILETIME - SECS_YR_2000)); // epoch -> sec2000 } #endif diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 45f0d153e..885716d5b 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -8,6 +8,8 @@ #endif #endif +#define _COMPILETIME myTZ.toUTC(RtcDateTime(__DATE__, __TIME__).Epoch32Time()) + // Local logging tag static const char TAG[] = __FILE__; @@ -121,12 +123,11 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, _seconds(), mytimesource); } else { timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ); - time_t unix_sec_at_compilation = compiledUTC(); ESP_LOGD(TAG, "[%0.3f] Failed to synchronise time from source %c | unix sec " "obtained from source: %d | unix sec at program compilation: %d", _seconds(), timeSetSymbols[mytimesource], time_to_set, - unix_sec_at_compilation); + _COMPILETIME); } } @@ -215,13 +216,7 @@ void IRAM_ATTR CLOCKIRQ(void) { // helper function to check plausibility of a time time_t timeIsValid(time_t const t) { // is it a time in the past? we use compile date to guess - return (t >= compiledUTC() ? t : 0); -} - -// helper function to convert compile time to UTC time -time_t compiledUTC(void) { - static time_t t = myTZ.toUTC(RtcDateTime(__DATE__, __TIME__).Epoch32Time()); - return t; + return (t >= _COMPILETIME ? t : 0); } // helper function to calculate serial transmit time From de2e5765998a1359e3970450550b6bbcec4c7ce1 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 3 May 2021 19:39:02 +0200 Subject: [PATCH 60/68] remove local microtime lib --- lib/microTime/.gitkeep | 1 - lib/microTime/DateStrings.cpp | 99 ----- lib/microTime/Readme.md | 164 -------- lib/microTime/docs/issue_template.md | 64 ---- .../SyncArduinoClock/SyncArduinoClock.pde | 78 ---- .../Processing/SyncArduinoClock/readme.txt | 9 - .../TimeArduinoDue/TimeArduinoDue.ino | 71 ---- lib/microTime/examples/TimeGPS/TimeGPS.ino | 87 ----- lib/microTime/examples/TimeNTP/TimeNTP.ino | 135 ------- .../TimeNTP_ENC28J60/TimeNTP_ENC28J60.ino | 168 --------- .../TimeNTP_ESP8266WiFi.ino | 156 -------- lib/microTime/examples/TimeRTC/TimeRTC.ino | 55 --- .../examples/TimeRTCLog/TimeRTCLog.ino | 109 ------ .../examples/TimeRTCSet/TimeRTCSet.ino | 80 ---- .../examples/TimeSerial/TimeSerial.ino | 81 ---- .../TimeSerialDateStrings.ino | 108 ------ .../examples/TimeTeensy3/TimeTeensy3.ino | 78 ---- lib/microTime/keywords.txt | 35 -- lib/microTime/library.json | 26 -- lib/microTime/library.properties | 10 - lib/microTime/src/TimeLib.h | 185 --------- lib/microTime/src/microTime.cpp | 353 ------------------ lib/microTime/src/microTime.h | 1 - platformio_orig.ini | 3 +- 24 files changed, 2 insertions(+), 2154 deletions(-) delete mode 100644 lib/microTime/.gitkeep delete mode 100644 lib/microTime/DateStrings.cpp delete mode 100644 lib/microTime/Readme.md delete mode 100644 lib/microTime/docs/issue_template.md delete mode 100644 lib/microTime/examples/Processing/SyncArduinoClock/SyncArduinoClock.pde delete mode 100644 lib/microTime/examples/Processing/SyncArduinoClock/readme.txt delete mode 100644 lib/microTime/examples/TimeArduinoDue/TimeArduinoDue.ino delete mode 100644 lib/microTime/examples/TimeGPS/TimeGPS.ino delete mode 100644 lib/microTime/examples/TimeNTP/TimeNTP.ino delete mode 100644 lib/microTime/examples/TimeNTP_ENC28J60/TimeNTP_ENC28J60.ino delete mode 100644 lib/microTime/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino delete mode 100644 lib/microTime/examples/TimeRTC/TimeRTC.ino delete mode 100644 lib/microTime/examples/TimeRTCLog/TimeRTCLog.ino delete mode 100644 lib/microTime/examples/TimeRTCSet/TimeRTCSet.ino delete mode 100644 lib/microTime/examples/TimeSerial/TimeSerial.ino delete mode 100644 lib/microTime/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino delete mode 100644 lib/microTime/examples/TimeTeensy3/TimeTeensy3.ino delete mode 100644 lib/microTime/keywords.txt delete mode 100644 lib/microTime/library.json delete mode 100644 lib/microTime/library.properties delete mode 100644 lib/microTime/src/TimeLib.h delete mode 100644 lib/microTime/src/microTime.cpp delete mode 100644 lib/microTime/src/microTime.h diff --git a/lib/microTime/.gitkeep b/lib/microTime/.gitkeep deleted file mode 100644 index 8b1378917..000000000 --- a/lib/microTime/.gitkeep +++ /dev/null @@ -1 +0,0 @@ - diff --git a/lib/microTime/DateStrings.cpp b/lib/microTime/DateStrings.cpp deleted file mode 100644 index 3f0f007fb..000000000 --- a/lib/microTime/DateStrings.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* DateStrings.cpp - * Definitions for date strings for use with the Time library - * - * Updated for Arduino 1.5.7 18 July 2014 - * - * No memory is consumed in the sketch if your code does not call any of the string methods - * You can change the text of the strings, make sure the short strings are each exactly 3 characters - * the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in TimeLib.h - * - */ - -#if defined(__AVR__) -#include -#else -// for compatiblity with Arduino Due and Teensy 3.0 and maybe others? -#define PROGMEM -#define PGM_P const char * -#define pgm_read_byte(addr) (*(const unsigned char *)(addr)) -#define pgm_read_word(addr) (*(const unsigned char **)(addr)) -#ifndef strcpy_P -#define strcpy_P(dest, src) strcpy((dest), (src)) -#endif -#endif -#include // for strcpy_P or strcpy -#include "microTimeLib.h" - -// the short strings for each day or month must be exactly dt_SHORT_STR_LEN -#define dt_SHORT_STR_LEN 3 // the length of short strings - -static char buffer[dt_MAX_STRING_LEN+1]; // must be big enough for longest string and the terminating null - -const char monthStr0[] PROGMEM = ""; -const char monthStr1[] PROGMEM = "January"; -const char monthStr2[] PROGMEM = "February"; -const char monthStr3[] PROGMEM = "March"; -const char monthStr4[] PROGMEM = "April"; -const char monthStr5[] PROGMEM = "May"; -const char monthStr6[] PROGMEM = "June"; -const char monthStr7[] PROGMEM = "July"; -const char monthStr8[] PROGMEM = "August"; -const char monthStr9[] PROGMEM = "September"; -const char monthStr10[] PROGMEM = "October"; -const char monthStr11[] PROGMEM = "November"; -const char monthStr12[] PROGMEM = "December"; - -const PROGMEM char * const PROGMEM monthNames_P[] = -{ - monthStr0,monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6, - monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12 -}; - -const char monthShortNames_P[] PROGMEM = "ErrJanFebMarAprMayJunJulAugSepOctNovDec"; - -const char dayStr0[] PROGMEM = "Err"; -const char dayStr1[] PROGMEM = "Sunday"; -const char dayStr2[] PROGMEM = "Monday"; -const char dayStr3[] PROGMEM = "Tuesday"; -const char dayStr4[] PROGMEM = "Wednesday"; -const char dayStr5[] PROGMEM = "Thursday"; -const char dayStr6[] PROGMEM = "Friday"; -const char dayStr7[] PROGMEM = "Saturday"; - -const PROGMEM char * const PROGMEM dayNames_P[] = -{ - dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7 -}; - -const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThuFriSat"; - -/* functions to return date strings */ - -char* monthStr(uint8_t month) -{ - strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month]))); - return buffer; -} - -char* monthShortStr(uint8_t month) -{ - for (int i=0; i < dt_SHORT_STR_LEN; i++) - buffer[i] = pgm_read_byte(&(monthShortNames_P[i+ (month*dt_SHORT_STR_LEN)])); - buffer[dt_SHORT_STR_LEN] = 0; - return buffer; -} - -char* dayStr(uint8_t day) -{ - strcpy_P(buffer, (PGM_P)pgm_read_word(&(dayNames_P[day]))); - return buffer; -} - -char* dayShortStr(uint8_t day) -{ - uint8_t index = day*dt_SHORT_STR_LEN; - for (int i=0; i < dt_SHORT_STR_LEN; i++) - buffer[i] = pgm_read_byte(&(dayShortNames_P[index + i])); - buffer[dt_SHORT_STR_LEN] = 0; - return buffer; -} diff --git a/lib/microTime/Readme.md b/lib/microTime/Readme.md deleted file mode 100644 index af0ee32a5..000000000 --- a/lib/microTime/Readme.md +++ /dev/null @@ -1,164 +0,0 @@ -# Arduino Time Library - -Time is a library that provides timekeeping functionality for Arduino. - -The code is derived from the Playground DateTime library but is updated -to provide an API that is more flexible and easier to use. - -A primary goal was to enable date and time functionality that can be used with -a variety of external time sources with minimum differences required in sketch logic. - -Example sketches illustrate how similar sketch code can be used with: a Real Time Clock, -internet NTP time service, GPS time data, and Serial time messages from a computer -for time synchronization. - -## Functionality - -The functions available in the library include - -```c -hour(); // the hour now (0-23) -minute(); // the minute now (0-59) -second(); // the second now (0-59) -day(); // the day now (1-31) -weekday(); // day of the week (1-7), Sunday is day 1 -month(); // the month now (1-12) -year(); // the full four digit year: (2009, 2010 etc) -``` - -there are also functions to return the hour in 12-hour format - -```c -hourFormat12(); // the hour now in 12 hour format -isAM(); // returns true if time now is AM -isPM(); // returns true if time now is PM -``` - -The time and date functions can take an optional parameter for the time. This prevents -errors if the time rolls over between elements. For example, if a new minute begins -between getting the minute and second, the values will be inconsistent. Using the -following functions eliminates this problem - -```c -time_t t = now(); // store the current time in time variable t -hour(t); // returns the hour for the given time t -minute(t); // returns the minute for the given time t -second(t); // returns the second for the given time t -day(t); // the day for the given time t -weekday(t); // day of the week for the given time t -month(t); // the month for the given time t -year(t); // the year for the given time t -``` - -There are also two functions that return the number of milliseconds left-over. Care -should be taken when using this value since there are no functions to set the time -with sub-second accuracy and the value may jump when the time is synchronized. -However, it is always consistent with the current time. To access these functions, -you have to `#define TIMELIB_ENABLE_MILLIS` in your sketch. - -```c -time_t t = now(uint32_t& m) // store the current time in time variable t and milliseconds in m -millisecond(); // the millisecond now (0-999) -``` - -Functions for managing the timer services are: - -```c -setTime(t); // set the system time to the give time t -setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr - // (2010 or 10 sets year to 2010) -adjustTime(adjustment); // adjust system time by adding the adjustment value (in seconds) -timeStatus(); // indicates if time has been set and recently synchronized - // returns one of the following enumerations: -timeNotSet // the time has never been set, the clock started on Jan 1, 1970 -timeNeedsSync // the time had been set but a sync attempt did not succeed -timeSet // the time is set and is synced -``` - -Time and Date values are not valid if the status is timeNotSet. Otherwise, values can be used but -the returned time may have drifted if the status is timeNeedsSync. - -```c -setSyncProvider(getTimeFunction); // set the external time provider -setSyncInterval(interval); // set the number of seconds between re-sync -``` - -There are many convenience macros in the `time.h` file for time constants and conversion -of time units. - -To use the library, copy the download to the Library directory. - -## Examples - -The Time directory contains the Time library and some example sketches -illustrating how the library can be used with various time sources: - -- `TimeSerial.pde` shows Arduino as a clock without external hardware. - It is synchronized by time messages sent over the serial port. - A companion Processing sketch will automatically provide these messages - if it is running and connected to the Arduino serial port. - -- `TimeSerialDateStrings.pde` adds day and month name strings to the sketch above - Short (3 characters) and long strings are available to print the days of - the week and names of the months. - -- `TimeRTC` uses a DS1307 real-time clock to provide time synchronization. - A basic RTC library named DS1307RTC is included in the download. - To run this sketch the DS1307RTC library must be installed. - -- `TimeRTCSet` is similar to the above and adds the ability to set the Real Time Clock - -- `TimeRTCLog` demonstrates how to calculate the difference between times. - It is a very simple logger application that monitors events on digital pins - and prints (to the serial port) the time of an event and the time period since - the previous event. - -- `TimeNTP` uses the Arduino Ethernet shield to access time using the internet NTP time service. - The NTP protocol uses UDP and the UdpBytewise library is required, see: - http://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/ - -- `TimeGPS` gets time from a GPS - This requires the TinyGPS library from Mikal Hart: - http://arduiniana.org/libraries/TinyGPS - -## Differences - -Differences between this code and the playground DateTime library -although the Time library is based on the DateTime codebase, the API has changed. -Changes in the Time library API: - -- time elements are functions returning `int` (they are variables in DateTime) -- Years start from 1970 -- days of the week and months start from 1 (they start from 0 in DateTime) -- DateStrings do not require a separate library -- time elements can be accessed non-atomically (in DateTime they are always atomic) -- function added to automatically sync time with external source -- `localTime` and `maketime` parameters changed, `localTime` renamed to `breakTime` - -## Technical notes: - -Internal system time is based on the standard Unix `time_t`. -The value is the number of seconds since Jan 1, 1970. -System time begins at zero when the sketch starts. - -The internal time can be automatically synchronized at regular intervals to an external time source. -This is enabled by calling the `setSyncProvider(provider)` function - the provider argument is -the address of a function that returns the current time as a `time_t`. -See the sketches in the examples directory for usage. - -The default interval for re-syncing the time is 5 minutes but can be changed by calling the -`setSyncInterval(interval)` method to set the number of seconds between re-sync attempts. - -The Time library defines a structure for holding time elements that is a compact version of the C tm structure. -All the members of the Arduino tm structure are bytes and the year is offset from 1970. -Convenience macros provide conversion to and from the Arduino format. - -Low level functions to convert between system time and individual time elements are provided: - -```c -breakTime(time, &tm); // break time_t into elements stored in tm struct -makeTime(&tm); // return time_t from elements stored in tm struct -``` - -The DS1307RTC library included in the download provides an example of how a time provider -can use the low-level functions to interface with the Time library. diff --git a/lib/microTime/docs/issue_template.md b/lib/microTime/docs/issue_template.md deleted file mode 100644 index 06109925c..000000000 --- a/lib/microTime/docs/issue_template.md +++ /dev/null @@ -1,64 +0,0 @@ -Please use this form only to report code defects or bugs. - -For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using. - -Arduino: forum.arduino.cc -Teensy: forum.pjrc.com -ESP8266: www.esp8266.com -ESP32: www.esp32.com -Adafruit Feather/Metro/Trinket: forums.adafruit.com -Particle Photon: community.particle.io - -If you are experiencing trouble but not certain of the cause, or need help using this code, ask on the appropriate forum. This is not the place to ask for support or help, even directly related to this code. Only use this form you are certain you have discovered a defect in this code! - -Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software. - - ------------------------------ Remove above ----------------------------- - - - -### Description - -Describe your problem. - - - -### Steps To Reproduce Problem - -Please give detailed instructions needed for anyone to attempt to reproduce the problem. - - - -### Hardware & Software - -Board -Shields / modules used -Arduino IDE version -Teensyduino version (if using Teensy) -Version info & package name (from Tools > Boards > Board Manager) -Operating system & version -Any other software or hardware? - - -### Arduino Sketch - -```cpp -// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem) -#include - -// libraries: give links/details so anyone can compile your code for the same result - -void setup() { -} - -void loop() { -} -``` - - -### Errors or Incorrect Output - -If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears. - - diff --git a/lib/microTime/examples/Processing/SyncArduinoClock/SyncArduinoClock.pde b/lib/microTime/examples/Processing/SyncArduinoClock/SyncArduinoClock.pde deleted file mode 100644 index 4313be33c..000000000 --- a/lib/microTime/examples/Processing/SyncArduinoClock/SyncArduinoClock.pde +++ /dev/null @@ -1,78 +0,0 @@ -/** - * SyncArduinoClock. - * - * portIndex must be set to the port connected to the Arduino - * - * The current time is sent in response to request message from Arduino - * or by clicking the display window - * - * The time message is 11 ASCII text characters; a header (the letter 'T') - * followed by the ten digit system time (unix time) - */ - - -import processing.serial.*; -import java.util.Date; -import java.util.Calendar; -import java.util.GregorianCalendar; - -public static final short portIndex = 0; // select the com port, 0 is the first port -public static final String TIME_HEADER = "T"; //header for arduino serial time message -public static final char TIME_REQUEST = 7; // ASCII bell character -public static final char LF = 10; // ASCII linefeed -public static final char CR = 13; // ASCII linefeed -Serial myPort; // Create object from Serial class - -void setup() { - size(200, 200); - println(Serial.list()); - println(" Connecting to -> " + Serial.list()[portIndex]); - myPort = new Serial(this,Serial.list()[portIndex], 9600); - println(getTimeNow()); -} - -void draw() -{ - textSize(20); - textAlign(CENTER); - fill(0); - text("Click to send\nTime Sync", 0, 75, 200, 175); - if ( myPort.available() > 0) { // If data is available, - char val = char(myPort.read()); // read it and store it in val - if(val == TIME_REQUEST){ - long t = getTimeNow(); - sendTimeMessage(TIME_HEADER, t); - } - else - { - if(val == LF) - ; //igonore - else if(val == CR) - println(); - else - print(val); // echo everying but time request - } - } -} - -void mousePressed() { - sendTimeMessage( TIME_HEADER, getTimeNow()); -} - - -void sendTimeMessage(String header, long time) { - String timeStr = String.valueOf(time); - myPort.write(header); // send header and time to arduino - myPort.write(timeStr); - myPort.write('\n'); -} - -long getTimeNow(){ - // java time is in ms, we want secs - Date d = new Date(); - Calendar cal = new GregorianCalendar(); - long current = d.getTime()/1000; - long timezone = cal.get(cal.ZONE_OFFSET)/1000; - long daylight = cal.get(cal.DST_OFFSET)/1000; - return current + timezone + daylight; -} diff --git a/lib/microTime/examples/Processing/SyncArduinoClock/readme.txt b/lib/microTime/examples/Processing/SyncArduinoClock/readme.txt deleted file mode 100644 index da9721d7b..000000000 --- a/lib/microTime/examples/Processing/SyncArduinoClock/readme.txt +++ /dev/null @@ -1,9 +0,0 @@ -SyncArduinoClock is a Processing sketch that responds to Arduino requests for -time synchronization messages. - -The portIndex must be set the Serial port connected to Arduino. - -Download TimeSerial.pde onto Arduino and you should see the time -message displayed when you run SyncArduinoClock in Processing. -The Arduino time is set from the time on your computer through the -Processing sketch. diff --git a/lib/microTime/examples/TimeArduinoDue/TimeArduinoDue.ino b/lib/microTime/examples/TimeArduinoDue/TimeArduinoDue.ino deleted file mode 100644 index f0a9a95df..000000000 --- a/lib/microTime/examples/TimeArduinoDue/TimeArduinoDue.ino +++ /dev/null @@ -1,71 +0,0 @@ -/* - * TimeRTC.pde - * example code illustrating Time library with Real Time Clock. - * - * This example requires Markus Lange's Arduino Due RTC Library - * https://github.com/MarkusLange/Arduino-Due-RTC-Library - */ - -#include -#include - -// Select the Slowclock source -//RTC_clock rtc_clock(RC); -RTC_clock rtc_clock(XTAL); - -void setup() { - Serial.begin(9600); - rtc_clock.init(); - if (rtc_clock.date_already_set() == 0) { - // Unfortunately, the Arduino Due hardware does not seem to - // be designed to maintain the RTC clock state when the - // board resets. Markus described it thusly: "Uhh the Due - // does reset with the NRSTB pin. This resets the full chip - // with all backup regions including RTC, RTT and SC. Only - // if the reset is done with the NRST pin will these regions - // stay with their old values." - rtc_clock.set_time(__TIME__); - rtc_clock.set_date(__DATE__); - // However, this might work on other unofficial SAM3X boards - // with different reset circuitry than Arduino Due? - } - setSyncProvider(getArduinoDueTime); - if(timeStatus()!= timeSet) - Serial.println("Unable to sync with the RTC"); - else - Serial.println("RTC has set the system time"); -} - -time_t getArduinoDueTime() -{ - return rtc_clock.unixtime(); -} - -void loop() -{ - digitalClockDisplay(); - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - diff --git a/lib/microTime/examples/TimeGPS/TimeGPS.ino b/lib/microTime/examples/TimeGPS/TimeGPS.ino deleted file mode 100644 index fea969886..000000000 --- a/lib/microTime/examples/TimeGPS/TimeGPS.ino +++ /dev/null @@ -1,87 +0,0 @@ -/* - * TimeGPS.pde - * example code illustrating time synced from a GPS - * - */ - -#include -#include // http://arduiniana.org/libraries/TinyGPS/ -#include -// TinyGPS and SoftwareSerial libraries are the work of Mikal Hart - -SoftwareSerial SerialGPS = SoftwareSerial(10, 11); // receive on pin 10 -TinyGPS gps; - -// To use a hardware serial port, which is far more efficient than -// SoftwareSerial, uncomment this line and remove SoftwareSerial -//#define SerialGPS Serial1 - -// Offset hours from gps time (UTC) -const int offset = 1; // Central European Time -//const int offset = -5; // Eastern Standard Time (USA) -//const int offset = -4; // Eastern Daylight Time (USA) -//const int offset = -8; // Pacific Standard Time (USA) -//const int offset = -7; // Pacific Daylight Time (USA) - -// Ideally, it should be possible to learn the time zone -// based on the GPS position data. However, that would -// require a complex library, probably incorporating some -// sort of database using Eric Muller's time zone shape -// maps, at http://efele.net/maps/tz/ - -time_t prevDisplay = 0; // when the digital clock was displayed - -void setup() -{ - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - SerialGPS.begin(4800); - Serial.println("Waiting for GPS time ... "); -} - -void loop() -{ - while (SerialGPS.available()) { - if (gps.encode(SerialGPS.read())) { // process gps messages - // when TinyGPS reports new data... - unsigned long age; - int Year; - byte Month, Day, Hour, Minute, Second; - gps.crack_datetime(&Year, &Month, &Day, &Hour, &Minute, &Second, NULL, &age); - if (age < 500) { - // set the Time to the latest GPS reading - setTime(Hour, Minute, Second, Day, Month, Year); - adjustTime(offset * SECS_PER_HOUR); - } - } - } - if (timeStatus()!= timeNotSet) { - if (now() != prevDisplay) { //update the display only if the time has changed - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits) { - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - diff --git a/lib/microTime/examples/TimeNTP/TimeNTP.ino b/lib/microTime/examples/TimeNTP/TimeNTP.ino deleted file mode 100644 index 17a908f82..000000000 --- a/lib/microTime/examples/TimeNTP/TimeNTP.ino +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Time_NTP.pde - * Example showing time sync to NTP time source - * - * This sketch uses the Ethernet library - */ - -#include -#include -#include -#include - -byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -// NTP Servers: -IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov -// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov -// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov - - -const int timeZone = 1; // Central European Time -//const int timeZone = -5; // Eastern Standard Time (USA) -//const int timeZone = -4; // Eastern Daylight Time (USA) -//const int timeZone = -8; // Pacific Standard Time (USA) -//const int timeZone = -7; // Pacific Daylight Time (USA) - - -EthernetUDP Udp; -unsigned int localPort = 8888; // local port to listen for UDP packets - -void setup() -{ - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - delay(250); - Serial.println("TimeNTP Example"); - if (Ethernet.begin(mac) == 0) { - // no point in carrying on, so do nothing forevermore: - while (1) { - Serial.println("Failed to configure Ethernet using DHCP"); - delay(10000); - } - } - Serial.print("IP number assigned by DHCP is "); - Serial.println(Ethernet.localIP()); - Udp.begin(localPort); - Serial.println("waiting for sync"); - setSyncProvider(getNtpTime); -} - -time_t prevDisplay = 0; // when the digital clock was displayed - -void loop() -{ - if (timeStatus() != timeNotSet) { - if (now() != prevDisplay) { //update the display only if time has changed - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/*-------- NTP code ----------*/ - -const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message -byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets - -time_t getNtpTime() -{ - while (Udp.parsePacket() > 0) ; // discard any previously received packets - Serial.println("Transmit NTP Request"); - sendNTPpacket(timeServer); - uint32_t beginWait = millis(); - while (millis() - beginWait < 1500) { - int size = Udp.parsePacket(); - if (size >= NTP_PACKET_SIZE) { - Serial.println("Receive NTP Response"); - Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer - unsigned long secsSince1900; - // convert four bytes starting at location 40 to a long integer - secsSince1900 = (unsigned long)packetBuffer[40] << 24; - secsSince1900 |= (unsigned long)packetBuffer[41] << 16; - secsSince1900 |= (unsigned long)packetBuffer[42] << 8; - secsSince1900 |= (unsigned long)packetBuffer[43]; - return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; - } - } - Serial.println("No NTP Response :-("); - return 0; // return 0 if unable to get the time -} - -// send an NTP request to the time server at the given address -void sendNTPpacket(IPAddress &address) -{ - // set all bytes in the buffer to 0 - memset(packetBuffer, 0, NTP_PACKET_SIZE); - // Initialize values needed to form NTP request - // (see URL above for details on the packets) - packetBuffer[0] = 0b11100011; // LI, Version, Mode - packetBuffer[1] = 0; // Stratum, or type of clock - packetBuffer[2] = 6; // Polling Interval - packetBuffer[3] = 0xEC; // Peer Clock Precision - // 8 bytes of zero for Root Delay & Root Dispersion - packetBuffer[12] = 49; - packetBuffer[13] = 0x4E; - packetBuffer[14] = 49; - packetBuffer[15] = 52; - // all NTP fields have been given values, now - // you can send a packet requesting a timestamp: - Udp.beginPacket(address, 123); //NTP requests are to port 123 - Udp.write(packetBuffer, NTP_PACKET_SIZE); - Udp.endPacket(); -} - diff --git a/lib/microTime/examples/TimeNTP_ENC28J60/TimeNTP_ENC28J60.ino b/lib/microTime/examples/TimeNTP_ENC28J60/TimeNTP_ENC28J60.ino deleted file mode 100644 index b6ad79637..000000000 --- a/lib/microTime/examples/TimeNTP_ENC28J60/TimeNTP_ENC28J60.ino +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Time_NTP.pde - * Example showing time sync to NTP time source - * - * Also shows how to handle DST automatically. - * - * This sketch uses the EtherCard library: - * http://jeelabs.org/pub/docs/ethercard/ - */ - -#include -#include - -byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; - -// NTP Server -const char timeServer[] PROGMEM = "pool.ntp.org"; - -const int utcOffset = 1; // Central European Time -//const int utcOffset = -5; // Eastern Standard Time (USA) -//const int utcOffset = -4; // Eastern Daylight Time (USA) -//const int utcOffset = -8; // Pacific Standard Time (USA) -//const int utcOffset = -7; // Pacific Daylight Time (USA) - -// Packet buffer, must be big enough to packet and payload -#define BUFFER_SIZE 550 -byte Ethernet::buffer[BUFFER_SIZE]; - -const unsigned int remotePort = 123; - -void setup() -{ - Serial.begin(9600); - - while (!Serial) // Needed for Leonardo only - ; - delay(250); - - Serial.println("TimeNTP_ENC28J60 Example"); - - if (ether.begin(BUFFER_SIZE, mac) == 0) { - // no point in carrying on, so do nothing forevermore: - while (1) { - Serial.println("Failed to access Ethernet controller"); - delay(10000); - } - } - - if (!ether.dhcpSetup()) { - // no point in carrying on, so do nothing forevermore: - while (1) { - Serial.println("Failed to configure Ethernet using DHCP"); - delay(10000); - } - } - - ether.printIp("IP number assigned by DHCP is ", ether.myip); - - Serial.println("waiting for sync"); - //setSyncProvider(getNtpTime); // Use this for GMT time - setSyncProvider(getDstCorrectedTime); // Use this for local, DST-corrected time -} - -time_t prevDisplay = 0; // when the digital clock was displayed - -void loop() -{ - if (timeStatus() != timeNotSet) { - if (now() != prevDisplay) { //update the display only if time has changed - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/*-------- NTP code ----------*/ - -// SyncProvider that returns UTC time -time_t getNtpTime() -{ - // Send request - Serial.println("Transmit NTP Request"); - if (!ether.dnsLookup(timeServer)) { - Serial.println("DNS failed"); - return 0; // return 0 if unable to get the time - } else { - //ether.printIp("SRV: ", ether.hisip); - ether.ntpRequest(ether.hisip, remotePort); - - // Wait for reply - uint32_t beginWait = millis(); - while (millis() - beginWait < 1500) { - word len = ether.packetReceive(); - ether.packetLoop(len); - - unsigned long secsSince1900 = 0L; - if (len > 0 && ether.ntpProcessAnswer(&secsSince1900, remotePort)) { - Serial.println("Receive NTP Response"); - return secsSince1900 - 2208988800UL; - } - } - - Serial.println("No NTP Response :-("); - return 0; - } -} - -/* Alternative SyncProvider that automatically handles Daylight Saving Time (DST) periods, - * at least in Europe, see below. - */ -time_t getDstCorrectedTime (void) { - time_t t = getNtpTime (); - - if (t > 0) { - TimeElements tm; - breakTime (t, tm); - t += (utcOffset + dstOffset (tm.Day, tm.Month, tm.Year + 1970, tm.Hour)) * SECS_PER_HOUR; - } - - return t; -} - -/* This function returns the DST offset for the current UTC time. - * This is valid for the EU, for other places see - * http://www.webexhibits.org/daylightsaving/i.html - * - * Results have been checked for 2012-2030 (but should work since - * 1996 to 2099) against the following references: - * - http://www.uniquevisitor.it/magazine/ora-legale-italia.php - * - http://www.calendario-365.it/ora-legale-orario-invernale.html - */ -byte dstOffset (byte d, byte m, unsigned int y, byte h) { - // Day in March that DST starts on, at 1 am - byte dstOn = (31 - (5 * y / 4 + 4) % 7); - - // Day in October that DST ends on, at 2 am - byte dstOff = (31 - (5 * y / 4 + 1) % 7); - - if ((m > 3 && m < 10) || - (m == 3 && (d > dstOn || (d == dstOn && h >= 1))) || - (m == 10 && (d < dstOff || (d == dstOff && h <= 1)))) - return 1; - else - return 0; -} - diff --git a/lib/microTime/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino b/lib/microTime/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino deleted file mode 100644 index 1aeb2d7ba..000000000 --- a/lib/microTime/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino +++ /dev/null @@ -1,156 +0,0 @@ -/* - * TimeNTP_ESP8266WiFi.ino - * Example showing time sync to NTP time source - * - * This sketch uses the ESP8266WiFi library - */ - -#include -#include -#include - -const char ssid[] = "*************"; // your network SSID (name) -const char pass[] = "********"; // your network password - -// NTP Servers: -static const char ntpServerName[] = "us.pool.ntp.org"; -//static const char ntpServerName[] = "time.nist.gov"; -//static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov"; -//static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov"; -//static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov"; - -const int timeZone = 1; // Central European Time -//const int timeZone = -5; // Eastern Standard Time (USA) -//const int timeZone = -4; // Eastern Daylight Time (USA) -//const int timeZone = -8; // Pacific Standard Time (USA) -//const int timeZone = -7; // Pacific Daylight Time (USA) - - -WiFiUDP Udp; -unsigned int localPort = 8888; // local port to listen for UDP packets - -time_t getNtpTime(); -void digitalClockDisplay(); -void printDigits(int digits); -void sendNTPpacket(IPAddress &address); - -void setup() -{ - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - delay(250); - Serial.println("TimeNTP Example"); - Serial.print("Connecting to "); - Serial.println(ssid); - WiFi.begin(ssid, pass); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - - Serial.print("IP number assigned by DHCP is "); - Serial.println(WiFi.localIP()); - Serial.println("Starting UDP"); - Udp.begin(localPort); - Serial.print("Local port: "); - Serial.println(Udp.localPort()); - Serial.println("waiting for sync"); - setSyncProvider(getNtpTime); - setSyncInterval(300); -} - -time_t prevDisplay = 0; // when the digital clock was displayed - -void loop() -{ - if (timeStatus() != timeNotSet) { - if (now() != prevDisplay) { //update the display only if time has changed - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void digitalClockDisplay() -{ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print("."); - Serial.print(month()); - Serial.print("."); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits) -{ - // utility for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if (digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/*-------- NTP code ----------*/ - -const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message -byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets - -time_t getNtpTime() -{ - IPAddress ntpServerIP; // NTP server's ip address - - while (Udp.parsePacket() > 0) ; // discard any previously received packets - Serial.println("Transmit NTP Request"); - // get a random server from the pool - WiFi.hostByName(ntpServerName, ntpServerIP); - Serial.print(ntpServerName); - Serial.print(": "); - Serial.println(ntpServerIP); - sendNTPpacket(ntpServerIP); - uint32_t beginWait = millis(); - while (millis() - beginWait < 1500) { - int size = Udp.parsePacket(); - if (size >= NTP_PACKET_SIZE) { - Serial.println("Receive NTP Response"); - Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer - unsigned long secsSince1900; - // convert four bytes starting at location 40 to a long integer - secsSince1900 = (unsigned long)packetBuffer[40] << 24; - secsSince1900 |= (unsigned long)packetBuffer[41] << 16; - secsSince1900 |= (unsigned long)packetBuffer[42] << 8; - secsSince1900 |= (unsigned long)packetBuffer[43]; - return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; - } - } - Serial.println("No NTP Response :-("); - return 0; // return 0 if unable to get the time -} - -// send an NTP request to the time server at the given address -void sendNTPpacket(IPAddress &address) -{ - // set all bytes in the buffer to 0 - memset(packetBuffer, 0, NTP_PACKET_SIZE); - // Initialize values needed to form NTP request - // (see URL above for details on the packets) - packetBuffer[0] = 0b11100011; // LI, Version, Mode - packetBuffer[1] = 0; // Stratum, or type of clock - packetBuffer[2] = 6; // Polling Interval - packetBuffer[3] = 0xEC; // Peer Clock Precision - // 8 bytes of zero for Root Delay & Root Dispersion - packetBuffer[12] = 49; - packetBuffer[13] = 0x4E; - packetBuffer[14] = 49; - packetBuffer[15] = 52; - // all NTP fields have been given values, now - // you can send a packet requesting a timestamp: - Udp.beginPacket(address, 123); //NTP requests are to port 123 - Udp.write(packetBuffer, NTP_PACKET_SIZE); - Udp.endPacket(); -} diff --git a/lib/microTime/examples/TimeRTC/TimeRTC.ino b/lib/microTime/examples/TimeRTC/TimeRTC.ino deleted file mode 100644 index fa10ff6f5..000000000 --- a/lib/microTime/examples/TimeRTC/TimeRTC.ino +++ /dev/null @@ -1,55 +0,0 @@ -/* - * TimeRTC.pde - * example code illustrating Time library with Real Time Clock. - * - */ - -#include -#include -#include // a basic DS1307 library that returns time as a time_t - -void setup() { - Serial.begin(9600); - while (!Serial) ; // wait until Arduino Serial Monitor opens - setSyncProvider(RTC.get); // the function to get the time from the RTC - if(timeStatus()!= timeSet) - Serial.println("Unable to sync with the RTC"); - else - Serial.println("RTC has set the system time"); -} - -void loop() -{ - if (timeStatus() == timeSet) { - digitalClockDisplay(); - } else { - Serial.println("The time has not been set. Please run the Time"); - Serial.println("TimeRTCSet example, or DS1307RTC SetTime example."); - Serial.println(); - delay(4000); - } - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - diff --git a/lib/microTime/examples/TimeRTCLog/TimeRTCLog.ino b/lib/microTime/examples/TimeRTCLog/TimeRTCLog.ino deleted file mode 100644 index 42fc3e4bd..000000000 --- a/lib/microTime/examples/TimeRTCLog/TimeRTCLog.ino +++ /dev/null @@ -1,109 +0,0 @@ -/* - * TimeRTCLogger.ino - * example code illustrating adding and subtracting Time. - * - * this sketch logs pin state change events - * the time of the event and time since the previous event is calculated and sent to the serial port. - */ - -#include -#include -#include // a basic DS1307 library that returns time as a time_t - -const int nbrInputPins = 6; // monitor 6 digital pins -const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor -boolean state[nbrInputPins] ; // the state of the monitored pins -time_t prevEventTime[nbrInputPins] ; // the time of the previous event - -void setup() { - Serial.begin(9600); - setSyncProvider(RTC.get); // the function to sync the time from the RTC - for (int i=0; i < nbrInputPins; i++) { - pinMode( inputPins[i], INPUT); - // uncomment these lines if pull-up resistors are wanted - // pinMode( inputPins[i], INPUT_PULLUP); - // state[i] = HIGH; - } -} - -void loop() -{ - for (int i=0; i < nbrInputPins; i++) { - boolean val = digitalRead(inputPins[i]); - if (val != state[i]) { - time_t duration = 0; // the time since the previous event - state[i] = val; - time_t timeNow = now(); - if (prevEventTime[i] > 0) { - // if this was not the first state change, calculate the time from the previous change - duration = timeNow - prevEventTime[i]; - } - logEvent(inputPins[i], val, timeNow, duration ); // log the event - prevEventTime[i] = timeNow; // store the time for this event - } - } -} - -void logEvent( int pin, boolean state, time_t timeNow, time_t duration) -{ - Serial.print("Pin "); - Serial.print(pin); - if (state == HIGH) { - Serial.print(" went High at "); - } else { - Serial.print(" went Low at "); - } - showTime(timeNow); - if (duration > 0) { - // only display duration if greater than 0 - Serial.print(", Duration was "); - showDuration(duration); - } - Serial.println(); -} - - -void showTime(time_t t) -{ - // display the given time - Serial.print(hour(t)); - printDigits(minute(t)); - printDigits(second(t)); - Serial.print(" "); - Serial.print(day(t)); - Serial.print(" "); - Serial.print(month(t)); - Serial.print(" "); - Serial.print(year(t)); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -void showDuration(time_t duration) -{ - // prints the duration in days, hours, minutes and seconds - if (duration >= SECS_PER_DAY) { - Serial.print(duration / SECS_PER_DAY); - Serial.print(" day(s) "); - duration = duration % SECS_PER_DAY; - } - if (duration >= SECS_PER_HOUR) { - Serial.print(duration / SECS_PER_HOUR); - Serial.print(" hour(s) "); - duration = duration % SECS_PER_HOUR; - } - if (duration >= SECS_PER_MIN) { - Serial.print(duration / SECS_PER_MIN); - Serial.print(" minute(s) "); - duration = duration % SECS_PER_MIN; - } - Serial.print(duration); - Serial.print(" second(s) "); -} - diff --git a/lib/microTime/examples/TimeRTCSet/TimeRTCSet.ino b/lib/microTime/examples/TimeRTCSet/TimeRTCSet.ino deleted file mode 100644 index 49c49f374..000000000 --- a/lib/microTime/examples/TimeRTCSet/TimeRTCSet.ino +++ /dev/null @@ -1,80 +0,0 @@ -/* - * TimeRTCSet.pde - * example code illustrating Time library with Real Time Clock. - * - * RTC clock is set in response to serial port time message - * A Processing example sketch to set the time is included in the download - * On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone) - */ - -#include -#include -#include // a basic DS1307 library that returns time as a time_t - - -void setup() { - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - setSyncProvider(RTC.get); // the function to get the time from the RTC - if (timeStatus() != timeSet) - Serial.println("Unable to sync with the RTC"); - else - Serial.println("RTC has set the system time"); -} - -void loop() -{ - if (Serial.available()) { - time_t t = processSyncMessage(); - if (t != 0) { - RTC.set(t); // set the RTC and the system time to the received value - setTime(t); - } - } - digitalClockDisplay(); - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/* code to process time sync messages from the serial port */ -#define TIME_HEADER "T" // Header tag for serial time sync message - -unsigned long processSyncMessage() { - unsigned long pctime = 0L; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - - if(Serial.find(TIME_HEADER)) { - pctime = Serial.parseInt(); - return pctime; - if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013) - pctime = 0L; // return 0 to indicate that the time is not valid - } - } - return pctime; -} - - - - - diff --git a/lib/microTime/examples/TimeSerial/TimeSerial.ino b/lib/microTime/examples/TimeSerial/TimeSerial.ino deleted file mode 100644 index 07e609fde..000000000 --- a/lib/microTime/examples/TimeSerial/TimeSerial.ino +++ /dev/null @@ -1,81 +0,0 @@ -/* - * TimeSerial.pde - * example code illustrating Time library set through serial port messages. - * - * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970) - * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013 - T1357041600 - * - * A Processing example sketch to automatically send the messages is included in the download - * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone) - */ - -#include - -#define TIME_HEADER "T" // Header tag for serial time sync message -#define TIME_REQUEST 7 // ASCII bell character requests a time sync message - -void setup() { - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - pinMode(13, OUTPUT); - setSyncProvider( requestSync); //set function to call when sync required - Serial.println("Waiting for sync message"); -} - -void loop(){ - if (Serial.available()) { - processSyncMessage(); - } - if (timeStatus()!= timeNotSet) { - digitalClockDisplay(); - } - if (timeStatus() == timeSet) { - digitalWrite(13, HIGH); // LED on if synced - } else { - digitalWrite(13, LOW); // LED off if needs refresh - } - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - - -void processSyncMessage() { - unsigned long pctime; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - - if(Serial.find(TIME_HEADER)) { - pctime = Serial.parseInt(); - if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013) - setTime(pctime); // Sync Arduino clock to the time received on the serial port - } - } -} - -time_t requestSync() -{ - Serial.write(TIME_REQUEST); - return 0; // the time will be sent later in response to serial mesg -} - diff --git a/lib/microTime/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino b/lib/microTime/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino deleted file mode 100644 index 95d2568c1..000000000 --- a/lib/microTime/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino +++ /dev/null @@ -1,108 +0,0 @@ -/* - * TimeSerialDateStrings.pde - * example code illustrating Time library date strings - * - * This sketch adds date string functionality to TimeSerial sketch - * Also shows how to handle different messages - * - * A message starting with a time header sets the time - * A Processing example sketch to automatically send the messages is inclided in the download - * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone) - * - * A message starting with a format header sets the date format - - * send: Fs\n for short date format - * send: Fl\n for long date format - */ - -#include - -// single character message tags -#define TIME_HEADER 'T' // Header tag for serial time sync message -#define FORMAT_HEADER 'F' // Header tag indicating a date format message -#define FORMAT_SHORT 's' // short month and day strings -#define FORMAT_LONG 'l' // (lower case l) long month and day strings - -#define TIME_REQUEST 7 // ASCII bell character requests a time sync message - -static boolean isLongFormat = true; - -void setup() { - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - setSyncProvider( requestSync); //set function to call when sync required - Serial.println("Waiting for sync message"); -} - -void loop(){ - if (Serial.available() > 1) { // wait for at least two characters - char c = Serial.read(); - if( c == TIME_HEADER) { - processSyncMessage(); - } - else if( c== FORMAT_HEADER) { - processFormatMessage(); - } - } - if (timeStatus()!= timeNotSet) { - digitalClockDisplay(); - } - delay(1000); -} - -void digitalClockDisplay() { - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - if(isLongFormat) - Serial.print(dayStr(weekday())); - else - Serial.print(dayShortStr(weekday())); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - if(isLongFormat) - Serial.print(monthStr(month())); - else - Serial.print(monthShortStr(month())); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits) { - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -void processFormatMessage() { - char c = Serial.read(); - if( c == FORMAT_LONG){ - isLongFormat = true; - Serial.println(F("Setting long format")); - } - else if( c == FORMAT_SHORT) { - isLongFormat = false; - Serial.println(F("Setting short format")); - } -} - -void processSyncMessage() { - unsigned long pctime; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - paul, perhaps we define in time.h? - - pctime = Serial.parseInt(); - if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013) - setTime(pctime); // Sync Arduino clock to the time received on the serial port - } -} - -time_t requestSync() { - Serial.write(TIME_REQUEST); - return 0; // the time will be sent later in response to serial mesg -} diff --git a/lib/microTime/examples/TimeTeensy3/TimeTeensy3.ino b/lib/microTime/examples/TimeTeensy3/TimeTeensy3.ino deleted file mode 100644 index f68dd8cc7..000000000 --- a/lib/microTime/examples/TimeTeensy3/TimeTeensy3.ino +++ /dev/null @@ -1,78 +0,0 @@ -/* - * TimeRTC.pde - * example code illustrating Time library with Real Time Clock. - * - */ - -#include - -void setup() { - // set the Time library to use Teensy 3.0's RTC to keep time - setSyncProvider(getTeensy3Time); - - Serial.begin(115200); - while (!Serial); // Wait for Arduino Serial Monitor to open - delay(100); - if (timeStatus()!= timeSet) { - Serial.println("Unable to sync with the RTC"); - } else { - Serial.println("RTC has set the system time"); - } -} - -void loop() { - if (Serial.available()) { - time_t t = processSyncMessage(); - if (t != 0) { - Teensy3Clock.set(t); // set the RTC - setTime(t); - } - } - digitalClockDisplay(); - delay(1000); -} - -void digitalClockDisplay() { - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -time_t getTeensy3Time() -{ - return Teensy3Clock.get(); -} - -/* code to process time sync messages from the serial port */ -#define TIME_HEADER "T" // Header tag for serial time sync message - -unsigned long processSyncMessage() { - unsigned long pctime = 0L; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - - if(Serial.find(TIME_HEADER)) { - pctime = Serial.parseInt(); - return pctime; - if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013) - pctime = 0L; // return 0 to indicate that the time is not valid - } - } - return pctime; -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - diff --git a/lib/microTime/keywords.txt b/lib/microTime/keywords.txt deleted file mode 100644 index 2c4a2e523..000000000 --- a/lib/microTime/keywords.txt +++ /dev/null @@ -1,35 +0,0 @@ -####################################### -# Syntax Coloring Map For Time -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### -time_t KEYWORD1 -####################################### -# Methods and Functions (KEYWORD2) -####################################### -now KEYWORD2 -millisecond KEYWORD2 -second KEYWORD2 -minute KEYWORD2 -hour KEYWORD2 -day KEYWORD2 -month KEYWORD2 -year KEYWORD2 -isAM KEYWORD2 -isPM KEYWORD2 -weekday KEYWORD2 -setTime KEYWORD2 -adjustTime KEYWORD2 -setSyncProvider KEYWORD2 -setSyncInterval KEYWORD2 -timeStatus KEYWORD2 -TimeLib KEYWORD2 -####################################### -# Instances (KEYWORD2) -####################################### - -####################################### -# Constants (LITERAL1) -####################################### diff --git a/lib/microTime/library.json b/lib/microTime/library.json deleted file mode 100644 index 6603650f1..000000000 --- a/lib/microTime/library.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "Time", - "description": "Time keeping library", - "keywords": "Time, date, hour, minute, second, day, week, month, year, RTC", - "authors": [ - { - "name": "Michael Margolis" - }, - { - "name": "Paul Stoffregen", - "email": "paul@pjrc.com", - "url": "http://www.pjrc.com", - "maintainer": true - } - ], - "repository": { - "type": "git", - "url": "https://github.com/PaulStoffregen/Time" - }, - "version": "1.5", - "homepage": "http://playground.arduino.cc/Code/Time", - "frameworks": "Arduino", - "examples": [ - "examples/*/*.ino" - ] -} diff --git a/lib/microTime/library.properties b/lib/microTime/library.properties deleted file mode 100644 index 49b1e2a16..000000000 --- a/lib/microTime/library.properties +++ /dev/null @@ -1,10 +0,0 @@ -name=Time -version=1.5 -author=Michael Margolis -maintainer=Paul Stoffregen -sentence=Timekeeping functionality for Arduino -paragraph=Date and Time functions, with provisions to synchronize to external time sources like GPS and NTP (Internet). This library is often used together with TimeAlarms and DS1307RTC. -category=Timing -url=http://playground.arduino.cc/code/time -architectures=* - diff --git a/lib/microTime/src/TimeLib.h b/lib/microTime/src/TimeLib.h deleted file mode 100644 index 2e3a7f44e..000000000 --- a/lib/microTime/src/TimeLib.h +++ /dev/null @@ -1,185 +0,0 @@ -/* - time.h - low level time and date functions -*/ - -/* - July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this) - - fixed daysToTime_t macro (thanks maniacbug) -*/ - -#ifndef _Time_h -#ifdef __cplusplus -#define _Time_h - -#include -#ifndef __AVR__ -#include // for __time_t_defined, but avr libc lacks sys/types.h -#endif - -#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc -typedef unsigned long time_t; -#endif - -#define usePPS -#define TIMELIB_ENABLE_MILLIS - -// This ugly hack allows us to define C++ overloaded functions, when included -// from within an extern "C", as newlib's sys/stat.h does. Actually it is -// intended to include "time.h" from the C library (on ARM, but AVR does not -// have that file at all). On Mac and Windows, the compiler will find this -// "Time.h" instead of the C library "time.h", so we may cause other weird -// and unpredictable effects by conflicting with the C library header "time.h", -// but at least this hack lets us define C++ functions as intended. Hopefully -// nothing too terrible will result from overriding the C library header?! -extern "C++" { -typedef enum { timeNotSet, timeNeedsSync, timeSet } timeStatus_t; - -typedef enum { - dowInvalid, - dowSunday, - dowMonday, - dowTuesday, - dowWednesday, - dowThursday, - dowFriday, - dowSaturday -} timeDayOfWeek_t; - -typedef enum { - tmSecond, - tmMinute, - tmHour, - tmWday, - tmDay, - tmMonth, - tmYear, - tmNbrFields -} tmByteFields; - -typedef struct { - uint8_t Second; - uint8_t Minute; - uint8_t Hour; - uint8_t Wday; // day of week, sunday is day 1 - uint8_t Day; - uint8_t Month; - uint8_t Year; // offset from 1970; -} tmElements_t, TimeElements, *tmElementsPtr_t; - -// convenience macros to convert to and from tm years -#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year -#define CalendarYrToTm(Y) ((Y)-1970) -#define tmYearToY2k(Y) ((Y)-30) // offset is from 2000 -#define y2kYearToTm(Y) ((Y) + 30) - -typedef time_t (*getExternalTime)(); -// typedef void (*setExternalTime)(const time_t); // not used in this version - -/*==============================================================================*/ -/* Useful Constants */ -#define SECS_PER_MIN ((time_t)(60UL)) -#define SECS_PER_HOUR ((time_t)(3600UL)) -#define SECS_PER_DAY ((time_t)(SECS_PER_HOUR * 24UL)) -#define DAYS_PER_WEEK ((time_t)(7UL)) -#define SECS_PER_WEEK ((time_t)(SECS_PER_DAY * DAYS_PER_WEEK)) -#define SECS_PER_YEAR \ - ((time_t)(SECS_PER_DAY * 365UL)) // TODO: ought to handle leap years -#define SECS_YR_2000 ((time_t)(946684800UL)) // the time at the start of y2k - -/* Useful Macros for getting elapsed time */ -#define numberOfSeconds(_time_) ((_time_) % SECS_PER_MIN) -#define numberOfMinutes(_time_) (((_time_) / SECS_PER_MIN) % SECS_PER_MIN) -#define numberOfHours(_time_) (((_time_) % SECS_PER_DAY) / SECS_PER_HOUR) -#define dayOfWeek(_time_) \ - ((((_time_) / SECS_PER_DAY + 4) % DAYS_PER_WEEK) + 1) // 1 = Sunday -#define elapsedDays(_time_) \ - ((_time_) / SECS_PER_DAY) // this is number of days since Jan 1 1970 -#define elapsedSecsToday(_time_) \ - ((_time_) % SECS_PER_DAY) // the number of seconds since last midnight -// The following macros are used in calculating alarms and assume the clock is -// set to a date later than Jan 1 1971 Always set the correct time before -// settting alarms -#define previousMidnight(_time_) \ - (((_time_) / SECS_PER_DAY) * \ - SECS_PER_DAY) // time at the start of the given day -#define nextMidnight(_time_) \ - (previousMidnight(_time_) + SECS_PER_DAY) // time at the end of the given day -#define elapsedSecsThisWeek(_time_) \ - (elapsedSecsToday(_time_) + \ - ((dayOfWeek(_time_) - 1) * SECS_PER_DAY)) // note that week starts on day 1 -#define previousSunday(_time_) \ - ((_time_)-elapsedSecsThisWeek( \ - _time_)) // time at the start of the week for the given time -#define nextSunday(_time_) \ - (previousSunday(_time_) + \ - SECS_PER_WEEK) // time at the end of the week for the given time - -/* Useful Macros for converting elapsed time to a time_t */ -#define minutesToTime_t ((M))((M)*SECS_PER_MIN) -#define hoursToTime_t ((H))((H)*SECS_PER_HOUR) -#define daysToTime_t ((D))((D)*SECS_PER_DAY) // fixed on Jul 22 2011 -#define weeksToTime_t ((W))((W)*SECS_PER_WEEK) - -/*============================================================================*/ -/* time and date functions */ -int hour(); // the hour now -int hour(time_t t); // the hour for the given time -int hourFormat12(); // the hour now in 12 hour format -int hourFormat12(time_t t); // the hour for the given time in 12 hour format -uint8_t isAM(); // returns true if time now is AM -uint8_t isAM(time_t t); // returns true the given time is AM -uint8_t isPM(); // returns true if time now is PM -uint8_t isPM(time_t t); // returns true the given time is PM -int minute(); // the minute now -int minute(time_t t); // the minute for the given time -int second(); // the second now -int second(time_t t); // the second for the given time -#ifdef TIMELIB_ENABLE_MILLIS -int millisecond(); // the millisecond now -int microsecond(); -#endif -int day(); // the day now -int day(time_t t); // the day for the given time -int weekday(); // the weekday now (Sunday is day 1) -int weekday(time_t t); // the weekday for the given time -int month(); // the month now (Jan is month 1) -int month(time_t t); // the month for the given time -int year(); // the full four digit year: (2009, 2010 etc) -int year(time_t t); // the year for the given time - -time_t now(); // return the current time as seconds since Jan 1 1970 -#ifdef TIMELIB_ENABLE_MILLIS -time_t now(uint32_t &sysTimeMicros); // return the current time as seconds and - // microseconds since Jan 1 1970 - -#endif -#ifdef usePPS -void IRAM_ATTR SyncToPPS(); -#endif -void setTime(time_t t); -void setTime(int hr, int min, int sec, int day, int month, int yr); -void adjustTime(long adjustment); - -/* date strings */ -#define dt_MAX_STRING_LEN \ - 9 // length of longest date string (excluding terminating null) -char *monthStr(uint8_t month); -char *dayStr(uint8_t day); -char *monthShortStr(uint8_t month); -char *dayShortStr(uint8_t day); - -/* time sync functions */ -timeStatus_t -timeStatus(); // indicates if time has been set and recently synchronized -void setSyncProvider( - getExternalTime getTimeFunction); // identify the external time provider -void setSyncInterval( - time_t interval); // set the number of seconds between re-sync - -/* low level functions to convert to and from system time */ -void breakTime(time_t time, tmElements_t &tm); // break time_t into elements -time_t makeTime(const tmElements_t &tm); // convert time elements into time_t - -} // extern "C++" -#endif // __cplusplus -#endif /* _Time_h */ diff --git a/lib/microTime/src/microTime.cpp b/lib/microTime/src/microTime.cpp deleted file mode 100644 index 055c8cf73..000000000 --- a/lib/microTime/src/microTime.cpp +++ /dev/null @@ -1,353 +0,0 @@ -/* - time.c - low level time and date functions - Copyright (c) Michael Margolis 2009-2014 - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - 1.0 6 Jan 2010 - initial release - 1.1 12 Feb 2010 - fixed leap year calculation error - 1.2 1 Nov 2010 - fixed setTime bug (thanks to Korman for this) - 1.3 24 Mar 2012 - many edits by Paul Stoffregen: fixed timeStatus() to update - status, updated examples for Arduino 1.0, fixed ARM - compatibility issues, added TimeArduinoDue and TimeTeensy3 - examples, add error checking and messages to RTC examples, - add examples to DS1307RTC library. - 1.4 5 Sep 2014 - compatibility with Arduino 1.5.7 -*/ - -#include - -#define TIMELIB_ENABLE_MILLIS -#define usePPS - -#include "TimeLib.h" - -// Convert days since epoch to week day. Sunday is day 1. -#define DAYS_TO_WDAY(x) (((x) + 4) % 7) + 1 - -static tmElements_t cacheElements; // a cache of time elements -static time_t cacheTime; // the time the cache was updated -static uint32_t syncInterval = - 300; // time sync will be attempted after this many seconds - -void refreshCache(time_t t) { - if (t != cacheTime) { - breakTime(t, cacheElements); - cacheTime = t; - } -} - -int hour() { // the hour now - return hour(now()); -} - -int hour(time_t t) { // the hour for the given time - refreshCache(t); - return cacheElements.Hour; -} - -int hourFormat12() { // the hour now in 12 hour format - return hourFormat12(now()); -} - -int hourFormat12(time_t t) { // the hour for the given time in 12 hour format - refreshCache(t); - if (cacheElements.Hour == 0) - return 12; // 12 midnight - else if (cacheElements.Hour > 12) - return cacheElements.Hour - 12; - else - return cacheElements.Hour; -} - -uint8_t isAM() { // returns true if time now is AM - return !isPM(now()); -} - -uint8_t isAM(time_t t) { // returns true if given time is AM - return !isPM(t); -} - -uint8_t isPM() { // returns true if PM - return isPM(now()); -} - -uint8_t isPM(time_t t) { // returns true if PM - return (hour(t) >= 12); -} - -int minute() { return minute(now()); } - -int minute(time_t t) { // the minute for the given time - refreshCache(t); - return cacheElements.Minute; -} - -int second() { return second(now()); } - -int second(time_t t) { // the second for the given time - refreshCache(t); - return cacheElements.Second; -} - -int millisecond() { - uint32_t ms; - now(ms); - ms = ms / 1000; - return (int)ms; -} - -int microsecond() { - uint32_t us; - now(us); - return (int)us; -} - -int day() { return (day(now())); } - -int day(time_t t) { // the day for the given time (0-6) - refreshCache(t); - return cacheElements.Day; -} - -int weekday() { // Sunday is day 1 - return weekday(now()); -} - -int weekday(time_t t) { - refreshCache(t); - return cacheElements.Wday; -} - -int month() { return month(now()); } - -int month(time_t t) { // the month for the given time - refreshCache(t); - return cacheElements.Month; -} - -int year() { // as in Processing, the full four digit year: (2009, 2010 etc) - return year(now()); -} - -int year(time_t t) { // the year for the given time - refreshCache(t); - return tmYearToCalendar(cacheElements.Year); -} - -/*============================================================================*/ -/* functions to convert to and from system time */ -/* These are for interfacing with time serivces and are not normally needed in a - * sketch */ - -// leap year calulator expects year argument as years offset from 1970 -#define LEAP_YEAR(Y) \ - (((1970 + (Y)) > 0) && !((1970 + (Y)) % 4) && \ - (((1970 + (Y)) % 100) || !((1970 + (Y)) % 400))) -#define daysInYear(year) ((time_t)(LEAP_YEAR(year) ? 366 : 365)) - -static const uint8_t monthDays[] = { - 31, 28, 31, 30, 31, 30, 31, - 31, 30, 31, 30, 31}; // API starts months from 1, this array starts from 0 - -void breakTime(time_t time, tmElements_t &tm) { - // break the given time_t into time components - // this is a more compact version of the C library localtime function - // note that year is offset from 1970 !!! - - uint8_t period; - time_t length; - - tm.Second = time % 60; - time /= 60; // now it is minutes - tm.Minute = time % 60; - time /= 60; // now it is hours - tm.Hour = time % 24; - time /= 24; // now it is days since 1 Jan 1970 - - // if the number of days since epoch matches cacheTime, then can take date - // elements from cacheElements and avoid expensive calculation. - if (time == (cacheTime / SECS_PER_DAY)) { - if (&tm != &cacheElements) { // check whether tm is actually cacheElements - tm.Wday = cacheElements.Wday; - tm.Day = cacheElements.Day; - tm.Month = cacheElements.Month; - tm.Year = cacheElements.Year; - } - return; - } - - tm.Wday = DAYS_TO_WDAY(time); - - period = 0; - while (time >= (length = daysInYear(period))) { - time -= length; - period++; - } - tm.Year = period; // year is offset from 1970 - // time is now days since 1 Jan of the year - - bool leap_year = LEAP_YEAR(period); - period = 0; - while (period < 12 && - time >= (length = monthDays[period] + (leap_year && period == 1))) { - time -= length; - period++; - } - tm.Month = period + 1; // jan is month 1 - // time is now days since the 1st day of the month - - tm.Day = time + 1; // day of month -} - -time_t makeTime(const tmElements_t &tm) { - // assemble time elements into time_t - // note year argument is offset from 1970 (see macros in time.h to convert to - // other formats) previous version used full four digit year (or digits since - // 2000),i.e. 2009 was 2009 or 9 - - int i; - uint32_t seconds; - - // seconds from 1970 till 1 jan 00:00:00 of the given year - seconds = SECS_PER_DAY * (365 * tm.Year); - for (i = 0; i < tm.Year; i++) { - if (LEAP_YEAR(i)) { - seconds += SECS_PER_DAY; // add extra days for leap years - } - } - - // add days for this year, months start from 1 - for (i = 1; i < tm.Month; i++) { - if ((i == 2) && LEAP_YEAR(tm.Year)) { - seconds += SECS_PER_DAY * 29; - } else { - seconds += SECS_PER_DAY * monthDays[i - 1]; // monthDay array starts from - // 0 - } - } - seconds += (tm.Day - 1) * SECS_PER_DAY; - seconds += tm.Hour * SECS_PER_HOUR; - seconds += tm.Minute * SECS_PER_MIN; - seconds += tm.Second; - return (time_t)seconds; -} -/*=====================================================*/ -/* Low level system time functions */ - -static time_t sysTime = 0; -static uint32_t prevMicros = 0; -static time_t nextSyncTime = 0; -static timeStatus_t Status = timeNotSet; - -getExternalTime getTimePtr; // pointer to external sync function -// setExternalTime setTimePtr; // not used in this version - -#ifdef TIME_DRIFT_INFO // define this to get drift data -time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync -#endif - -#ifdef usePPS -void IRAM_ATTR SyncToPPS() { - sysTime++; - prevMicros = micros(); -} -#endif - -time_t now() { - uint32_t sysTimeMicros; - return now(sysTimeMicros); -} - -time_t now(uint32_t &sysTimeMicros) { - // calculate number of seconds passed since last call to now() - while ((sysTimeMicros = micros() - prevMicros) >= 1000000) { - // micros() and prevMicros are both unsigned ints thus the subtraction will - // always result in a positive difference. This is OK since it corrects for - // wrap-around and micros() is monotonic. - sysTime++; - prevMicros += 1000000; -#ifdef TIME_DRIFT_INFO - sysUnsyncedTime++; // this can be compared to the synced time to measure - // long term drift -#endif - } - if (nextSyncTime <= sysTime) { - if (getTimePtr != 0) { - time_t t = getTimePtr(); - - if (t != 0) { - setTime(t); - } else { - nextSyncTime = sysTime + syncInterval; - Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync; - } - } - } - return sysTime; -} - -void setTime(time_t t) { -#ifdef TIME_DRIFT_INFO - if (sysUnsyncedTime == 0) - sysUnsyncedTime = t; // store the time of the first call to set a valid Time -#endif - - sysTime = t; - nextSyncTime = t + (time_t)syncInterval; - Status = timeSet; -#ifndef usePPS - prevMicros = - micros(); // restart counting from now (thanks to Korman for this fix) -#endif -} - -void setTime(int hr, int min, int sec, int dy, int mnth, int yr) { - // year can be given as full four digit year or two digts (2010 or 10 for - // 2010); it is converted to years since 1970 - if (yr > 99) - yr = CalendarYrToTm(yr); - else - yr = tmYearToY2k(yr); - cacheElements.Year = yr; - cacheElements.Month = mnth; - cacheElements.Day = dy; - cacheElements.Hour = hr; - cacheElements.Minute = min; - cacheElements.Second = sec; - cacheTime = makeTime(cacheElements); - cacheElements.Wday = DAYS_TO_WDAY(cacheTime / SECS_PER_DAY); - setTime(cacheTime); -} - -void adjustTime(long adjustment) { sysTime += adjustment; } - -// indicates if time has been set and recently synchronized -timeStatus_t timeStatus() { - now(); // required to actually update the status - return Status; -} - -void setSyncProvider(getExternalTime getTimeFunction) { - getTimePtr = getTimeFunction; - nextSyncTime = sysTime; - now(); // this will sync the clock -} - -void setSyncInterval( - time_t interval) { // set the number of seconds between re-sync - syncInterval = (uint32_t)interval; - nextSyncTime = sysTime + syncInterval; -} diff --git a/lib/microTime/src/microTime.h b/lib/microTime/src/microTime.h deleted file mode 100644 index a79b0801e..000000000 --- a/lib/microTime/src/microTime.h +++ /dev/null @@ -1 +0,0 @@ -#include "TimeLib.h" diff --git a/platformio_orig.ini b/platformio_orig.ini index 7817f5b42..0bc050686 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -84,12 +84,13 @@ lib_deps_basic = https://github.com/SukkoPera/Arduino-Rokkit-Hash.git bblanchon/ArduinoJson @ ^6 jchristensen/Timezone @ ^1.2.4 + https://github.com/cyberman54/microTime.git makuna/RTC @ ^2.3.5 spacehuhn/SimpleButton lewisxhe/AXP202X_Library @ ^1.1.3 geeksville/esp32-micro-sdcard @ ^0.1.1 256dpi/MQTT @ ^2.4.7 - https://github.com/dbSuS/libpax#v0.1.2 + https://github.com/cyberman54/libpax.git lib_deps_all = ${common.lib_deps_basic} ${common.lib_deps_lora} From 49aba50df1b10ee4e2ba1523e36bf1ed1ad47930 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Fri, 7 May 2021 19:41:36 +0200 Subject: [PATCH 61/68] remove beacon monitormode --- README.md | 23 ++++------------------- include/beacon_array.h | 9 --------- include/globals.h | 6 +----- include/main.h | 1 - include/payload.h | 1 - platformio_orig.ini | 2 +- src/TTN/packed_decoder.js | 9 ++------- src/TTN/plain_decoder.js | 6 ------ src/TTNv3/packed_decodeUplink.js | 9 ++------- src/TTNv3/plain_decodeUplink.js | 6 ------ src/configmanager.cpp | 7 +++---- src/hal/ttgobeam10.h | 2 +- src/ledmatrixdisplay.cpp | 9 ++++----- src/paxcounter_orig.conf | 2 +- src/payload.cpp | 28 ++-------------------------- src/rcommand.cpp | 17 +---------------- src/sensor.cpp | 2 +- 17 files changed, 23 insertions(+), 116 deletions(-) delete mode 100644 include/beacon_array.h diff --git a/README.md b/README.md index d6fd9d676..1f52f7962 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,6 @@ Paxcounter generates identifiers for sniffed Wifi or Bluetooth MAC adresses and - Quick blink (20ms on each 1/5 second): joining LoRaWAN network in progress or pending - Small blink (10ms on each 1/2 second): LoRaWAN data transmit in progress or pending - Long blink (200ms on each 2 seconds): LoRaWAN stack error -- Single long flash (2sec): Known beacon detected **RGB LED:** @@ -164,7 +163,6 @@ Paxcounter generates identifiers for sniffed Wifi or Bluetooth MAC adresses and - Pink: LORAWAN MAC transmit in progress - Blue: LoRaWAN data transmit in progress or pending - Red: LoRaWAN stack error -- White: Known Beacon detected # Display @@ -190,7 +188,7 @@ Output of sensor and peripheral data is internally switched by a bitmask registe | Bit | Sensordata | Default | --- | ------------- | ------- | 0 | Paxcounter | on -| 1 | Beacon alarm | on +| 1 | unused | off | 2 | BME280/680 | on | 3 | GPS* | on | 4 | User sensor 1 | on @@ -320,7 +318,7 @@ Hereafter described is the default *plain* format, which uses MSB bit numbering. byte 14: count randomizated MACs only (0=disabled, 1=enabled) [default 1] byte 15: RGB LED luminosity (0..100 %) [default 30] byte 16: 0 (reserved) - byte 17: Beacon proximity alarm mode (1=on, 0=off) [default 0] + byte 17: 0 (reserved) bytes 18-28: Software version (ASCII format, terminating with zero) @@ -336,10 +334,7 @@ Hereafter described is the default *plain* format, which uses MSB bit numbering. byte 1: static value 0x01 -**Port #6:** Beacon proximity alarm - - byte 1: Beacon RSSI reception level - byte 2: Beacon identifier (0..255) +**Port #6:** (unused) **Port #7:** Environmental sensor data (only if device has feature BME) @@ -474,16 +469,6 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/ 0 ... 100 percentage of luminosity (100% = full light) e.g. 50 -> 50% of luminosity [default] -0x11 set beacon proximity alarm mode on/off - - 0 = Beacon monitor mode off [default] - 1 = Beacon monitor mode on, enables proximity alarm if test beacons are seen - -0x12 set or reset a beacon MAC for proximity alarm - - byte 1 = beacon ID (0..255) - bytes 2..7 = beacon MAC with 6 digits (e.g. MAC 80:ab:00:01:02:03 -> 0x80ab00010203) - 0x13 set user sensor mode byte 1 = user sensor number (1..3) @@ -493,7 +478,7 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/ byte 1 = sensor data payload mask (0..255, meaning of bits see below) 0x01 = COUNT_DATA - 0x02 = ALARM_DATA + 0x02 = RESERVED_DATA 0x04 = MEMS_DATA 0x08 = GPS_DATA 0x10 = SENSOR_1_DATA (also ENS counter) diff --git a/include/beacon_array.h b/include/beacon_array.h deleted file mode 100644 index c94aeca21..000000000 --- a/include/beacon_array.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _BEACON_ARRAY_H -#define _BEACON_ARRAY_H - -std::array::iterator it; - -std::array beacons = {0x0000010203040506, 0x0000aabbccddeeff, - 0x0000112233445566}; - -#endif \ No newline at end of file diff --git a/include/globals.h b/include/globals.h index b7259ffd5..95816f418 100644 --- a/include/globals.h +++ b/include/globals.h @@ -21,7 +21,7 @@ // bits in payloadmask for filtering payload data #define COUNT_DATA _bit(0) -#define ALARM_DATA _bit(1) +#define RESERVED_DATA _bit(1) #define MEMS_DATA _bit(2) #define GPS_DATA _bit(3) #define SENSOR1_DATA _bit(4) @@ -72,7 +72,6 @@ typedef struct __attribute__((packed)) { uint8_t wifiscan; // 0=disabled, 1=enabled uint8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4) uint8_t rgblum; // RGB Led luminosity (0..100%) - uint8_t monitormode; // 0=disabled, 1=enabled uint8_t payloadmask; // bitswitches for payload data uint8_t enscount; // 0=disabled 1= enabled @@ -113,9 +112,6 @@ typedef struct { float pm25; } sdsStatus_t; -extern std::array::iterator it; -extern std::array beacons; - extern char clientId[20]; // unique clientID #endif \ No newline at end of file diff --git a/include/main.h b/include/main.h index b41c901a9..8d8b43acd 100644 --- a/include/main.h +++ b/include/main.h @@ -12,7 +12,6 @@ #include "i2c.h" #include "configmanager.h" #include "cyclic.h" -#include "beacon_array.h" #include "ota.h" #include "irqhandler.h" #include "spislave.h" diff --git a/include/payload.h b/include/payload.h index cfa58720e..e8376bf60 100644 --- a/include/payload.h +++ b/include/payload.h @@ -52,7 +52,6 @@ class PayloadConvert { void addConfig(configData_t value); void addStatus(uint16_t voltage, uint64_t uptime, float cputemp, uint32_t mem, uint8_t reset0, uint32_t restarts); - void addAlarm(int8_t rssi, uint8_t message); void addVoltage(uint16_t value); void addGPS(gpsStatus_t value); void addBME(bmeStatus_t value); diff --git a/platformio_orig.ini b/platformio_orig.ini index 0bc050686..6542c2c51 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -49,7 +49,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.5.0 +release_version = 2.6.0 ; 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/TTN/packed_decoder.js b/src/TTN/packed_decoder.js index 09461db80..f338e6dfc 100644 --- a/src/TTN/packed_decoder.js +++ b/src/TTN/packed_decoder.js @@ -62,11 +62,6 @@ function Decoder(bytes, port) { return decode(bytes, [uint8], ['button']); } - if (port === 6) { - // beacon proximity alarm - return decode(bytes, [int8, uint8], ['rssi', 'beacon']); - } - if (port === 7) { // BME680 sensor data return decode(bytes, [float, pressure, ufloat, ufloat], ['temperature', 'pressure', 'humidity', 'air']); @@ -248,7 +243,7 @@ var bitmap1 = function (byte) { } var i = bytesToInt(byte); var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean); - return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'reserved', 'alarm'] + return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'reserved', 'reserved'] .reduce(function (obj, pos, index) { obj[pos] = +bm[index]; return obj; @@ -262,7 +257,7 @@ var bitmap2 = function (byte) { } var i = bytesToInt(byte); var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean); - return ['battery', 'sensor3', 'sensor2', 'sensor1', 'gps', 'bme', 'alarm', 'counter'] + return ['battery', 'sensor3', 'sensor2', 'sensor1', 'gps', 'bme', 'reserved', 'counter'] .reduce(function (obj, pos, index) { obj[pos] = +bm[index]; return obj; diff --git a/src/TTN/plain_decoder.js b/src/TTN/plain_decoder.js index 1cb42e59f..104b7d1be 100644 --- a/src/TTN/plain_decoder.js +++ b/src/TTN/plain_decoder.js @@ -49,12 +49,6 @@ function Decoder(bytes, port) { decoded.button = bytes[i++]; } - if (port === 6) { - var i = 0; - decoded.rssi = bytes[i++]; - decoded.beacon = bytes[i++]; - } - if (port === 7) { var i = 0; decoded.temperature = ((bytes[i++] << 8) | bytes[i++]); diff --git a/src/TTNv3/packed_decodeUplink.js b/src/TTNv3/packed_decodeUplink.js index 0b1c36274..a010f3dca 100644 --- a/src/TTNv3/packed_decodeUplink.js +++ b/src/TTNv3/packed_decodeUplink.js @@ -70,11 +70,6 @@ function decodeUplink(input) { data = decode(input.bytes, [uint8], ['button']); } - if (input.fPort === 6) { - // beacon proximity alarm - data = decode(input.bytes, [int8, uint8], ['rssi', 'beacon']); - } - if (input.fPort === 7) { // BME680 sensor data data = decode(input.bytes, [float, pressure, ufloat, ufloat], ['temperature', 'pressure', 'humidity', 'air']); @@ -263,7 +258,7 @@ var bitmap1 = function (byte) { } var i = bytesToInt(byte); var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean); - return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'reserved', 'alarm'] + return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'reserved', 'reserved'] .reduce(function (obj, pos, index) { obj[pos] = +bm[index]; return obj; @@ -277,7 +272,7 @@ var bitmap2 = function (byte) { } var i = bytesToInt(byte); var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean); - return ['battery', 'sensor3', 'sensor2', 'sensor1', 'gps', 'bme', 'alarm', 'counter'] + return ['battery', 'sensor3', 'sensor2', 'sensor1', 'gps', 'bme', 'reserved', 'counter'] .reduce(function (obj, pos, index) { obj[pos] = +bm[index]; return obj; diff --git a/src/TTNv3/plain_decodeUplink.js b/src/TTNv3/plain_decodeUplink.js index 291e19451..d19feb48e 100644 --- a/src/TTNv3/plain_decodeUplink.js +++ b/src/TTNv3/plain_decodeUplink.js @@ -56,12 +56,6 @@ function decodeUplink(input) { data.button = input.bytes[i++]; } - if (input.fPort === 6) { - var i = 0; - data.rssi = input.bytes[i++]; - data.beacon = input.bytes[i++]; - } - if (input.fPort === 7) { var i = 0; data.temperature = ((input.bytes[i++] << 8) | input.bytes[i++]); diff --git a/src/configmanager.cpp b/src/configmanager.cpp index e80b06fd3..ef594ff6e 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -8,9 +8,9 @@ static const char TAG[] = __FILE__; // default settings for device data to be sent #define PAYLOADMASK \ - ((GPS_DATA | ALARM_DATA | MEMS_DATA | COUNT_DATA | SENSOR1_DATA | \ - SENSOR2_DATA | SENSOR3_DATA) & \ - (~BATT_DATA)) + ((GPS_DATA | MEMS_DATA | COUNT_DATA | SENSOR1_DATA | SENSOR2_DATA | \ + SENSOR3_DATA) & \ + (~BATT_DATA) & (~RESERVED_DATA)) // namespace for device runtime preferences #define DEVCONFIG "paxcntcfg" @@ -54,7 +54,6 @@ static void defaultConfig(configData_t *myconfig) { myconfig->wifiscan = WIFICOUNTER; // 0=disabled, 1=enabled myconfig->wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4) myconfig->rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%) - myconfig->monitormode = 0; // 0=disabled, 1=enabled myconfig->payloadmask = PAYLOADMASK; // payloads as defined in default myconfig->enscount = COUNT_ENS; // 0=disabled, 1=enabled diff --git a/src/hal/ttgobeam10.h b/src/hal/ttgobeam10.h index b014051b0..0fc5e8fef 100644 --- a/src/hal/ttgobeam10.h +++ b/src/hal/ttgobeam10.h @@ -20,7 +20,7 @@ User, long press -> send a button message Reset -> reset device */ -//#define HAS_DISPLAY 1 +#define HAS_DISPLAY 1 #define MY_DISPLAY_SDA SDA #define MY_DISPLAY_SCL SCL #define MY_DISPLAY_RST NOT_A_PIN diff --git a/src/ledmatrixdisplay.cpp b/src/ledmatrixdisplay.cpp index a927cb353..61b4fc89d 100644 --- a/src/ledmatrixdisplay.cpp +++ b/src/ledmatrixdisplay.cpp @@ -76,12 +76,11 @@ void refreshTheMatrixDisplay(bool nextPage) { case 0: - if (cfg.countermode == 1) + // update counter values from libpax + libpax_counter_count(&count_from_libpax); - // update counter values from libpax - libpax_counter_count(&count_from_libpax); - - { // cumulative counter mode -> display total number of pax + if (cfg.countermode == 1) { + // cumulative counter mode -> display total number of pax if (ulLastNumMacs != count_from_libpax.pax) { ulLastNumMacs = count_from_libpax.pax; matrix.clear(); diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index a21ace1fc..b1235d11e 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -101,7 +101,7 @@ #define CONFIGPORT 3 // config query results #define GPSPORT 4 // gps - NOTE: set to 1 to send combined GPS+COUNTERPORT payload #define BUTTONPORT 5 // button pressed signal -#define BEACONPORT 6 // beacon alarms +#define RESERVEDPORT 6 // reserved (unused) #define BMEPORT 7 // BME680 sensor #define BATTPORT 8 // battery voltage #define TIMEPORT 9 // time query and response diff --git a/src/payload.cpp b/src/payload.cpp index 740a250f3..3e2854e3b 100644 --- a/src/payload.cpp +++ b/src/payload.cpp @@ -28,11 +28,6 @@ void PayloadConvert::addCount(uint16_t value, uint8_t snifftype) { buffer[cursor++] = lowByte(value); } -void PayloadConvert::addAlarm(int8_t rssi, uint8_t msg) { - buffer[cursor++] = rssi; - buffer[cursor++] = msg; -} - void PayloadConvert::addVoltage(uint16_t value) { buffer[cursor++] = highByte(value); buffer[cursor++] = lowByte(value); @@ -55,7 +50,7 @@ void PayloadConvert::addConfig(configData_t value) { buffer[cursor++] = 0; // reserved buffer[cursor++] = value.rgblum; buffer[cursor++] = value.payloadmask; - buffer[cursor++] = value.monitormode; + buffer[cursor++] = 0; // reserved memcpy(buffer + cursor, value.version, 10); cursor += 10; } @@ -168,11 +163,6 @@ void PayloadConvert::addCount(uint16_t value, uint8_t snifftype) { writeUint16(value); } -void PayloadConvert::addAlarm(int8_t rssi, uint8_t msg) { - writeUint8(rssi); - writeUint8(msg); -} - void PayloadConvert::addVoltage(uint16_t value) { writeUint16(value); } void PayloadConvert::addConfig(configData_t value) { @@ -185,8 +175,7 @@ void PayloadConvert::addConfig(configData_t value) { writeUint8(value.rgblum); writeBitmap(value.adrmode ? true : false, value.screensaver ? true : false, value.screenon ? true : false, value.countermode ? true : false, - value.blescan ? true : false, value.wifiant ? true : false, 0, - value.monitormode ? true : false); + value.blescan ? true : false, value.wifiant ? true : false, 0, 0); writeUint8(value.payloadmask); writeVersion(value.version); } @@ -373,19 +362,6 @@ void PayloadConvert::addCount(uint16_t value, uint8_t snifftype) { } } -void PayloadConvert::addAlarm(int8_t rssi, uint8_t msg) { -#if (PAYLOAD_ENCODER == 3) - buffer[cursor++] = LPP_ALARM_CHANNEL; -#endif - buffer[cursor++] = LPP_PRESENCE; - buffer[cursor++] = msg; -#if (PAYLOAD_ENCODER == 3) - buffer[cursor++] = LPP_MSG_CHANNEL; -#endif - buffer[cursor++] = LPP_ANALOG_INPUT; - buffer[cursor++] = rssi; -} - void PayloadConvert::addVoltage(uint16_t value) { uint16_t volt = value / 10; #if (PAYLOAD_ENCODER == 3) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 95a2733b0..3c9d2d75f 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -223,20 +223,6 @@ uint64_t macConvert(uint8_t *paddr) { return (__builtin_bswap64(*mac) >> 16); } -void set_beacon(uint8_t val[]) { - uint8_t id = val[0]; // use first parameter as beacon storage id - memmove(val, val + 1, 6); // strip off storage id - beacons[id] = macConvert(val); // store beacon MAC in array - ESP_LOGI(TAG, "Remote command: set beacon ID#%d", id); - // printKey("MAC", val, 6, false); // show beacon MAC -} - -void set_monitor(uint8_t val[]) { - ESP_LOGI(TAG, "Remote command: set beacon monitor mode to %s", - val ? "on" : "off"); - cfg.monitormode = val[0] ? 1 : 0; -} - void set_loradr(uint8_t val[]) { #if (HAS_LORA) if (validDR(val[0])) { @@ -436,8 +422,7 @@ static const cmd_t table[] = { {0x09, set_reset, 1}, {0x0a, set_sendcycle, 1}, {0x0b, set_wifichancycle, 1}, {0x0c, set_blescantime, 1}, {0x0e, set_blescan, 1}, {0x0f, set_wifiant, 1}, - {0x10, set_rgblum, 1}, {0x11, set_monitor, 1}, - {0x12, set_beacon, 7}, {0x13, set_sensor, 2}, + {0x10, set_rgblum, 1}, {0x13, set_sensor, 2}, {0x14, set_payloadmask, 1}, {0x15, set_bme, 1}, {0x16, set_batt, 1}, {0x17, set_wifiscan, 1}, {0x18, set_enscount, 1}, {0x19, set_sleepcycle, 2}, diff --git a/src/sensor.cpp b/src/sensor.cpp index 60c148881..21f212f53 100644 --- a/src/sensor.cpp +++ b/src/sensor.cpp @@ -37,7 +37,7 @@ uint8_t sensor_mask(uint8_t sensor_no) { case 6: return (uint8_t)MEMS_DATA; case 7: - return (uint8_t)ALARM_DATA; + return (uint8_t)RESERVED_DATA; default: return 0; } From a284855f91d20c2f6a778572c89fdbaee4dd8362 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Fri, 7 May 2021 19:43:27 +0200 Subject: [PATCH 62/68] readme.md: add link to dutch DPA decision --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1f52f7962..958b4abbc 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ If option *BOOTMENU* is defined in `paxcounter.conf`, the ESP32 board will try t (e.g. UK citizens may want to check [Data Protection Act 1998](https://ico.org.uk/media/1560691/wi-fi-location-analytics-guidance.pdf) and [GDPR 2018](https://ico.org.uk/for-organisations/guide-to-the-general-data-protection-regulation-gdpr/key-definitions/)) -(e.g. Citizens in the the Netherlands may want to read [this article](https://www.ivir.nl/publicaties/download/PrivacyInformatie_2016_6.pdf) and [this article](https://autoriteitpersoonsgegevens.nl/nl/nieuws/europese-privacytoezichthouders-publiceren-opinie-eprivacyverordening)) +(e.g. Citizens in the the Netherlands and EU may want to read [this article](https://www.ivir.nl/publicaties/download/PrivacyInformatie_2016_6.pdf) and [this article](https://autoriteitpersoonsgegevens.nl/nl/nieuws/europese-privacytoezichthouders-publiceren-opinie-eprivacyverordening)) and [this decision](https://edpb.europa.eu/news/national-news/2021/dutch-dpa-fines-municipality-wi-fi-tracking_en) Note: If you use this software you do this at your own risk. That means that you alone - not the authors of this software - are responsible for the legal compliance of an application using this or build from this software and/or usage of a device created using this software. You should take special care and get prior legal advice if you plan metering passengers in public areas and/or publish data drawn from doing so. From a17e4a6894a4170aa3137f1bf7e9331712e5d0af Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Fri, 7 May 2021 19:50:22 +0200 Subject: [PATCH 63/68] bugfix set_countmode --- src/rcommand.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 3c9d2d75f..ca01f990e 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -147,6 +147,10 @@ void set_countmode(uint8_t val[]) { "Remote command: set counter mode called with invalid parameter(s)"); return; } +#if ((WIFICOUNTER) || (BLECOUNTER)) + libpax_counter_stop(); + init_libpax(); // re-inits counter mode from cfg.countermode +#endif } void set_screensaver(uint8_t val[]) { From 2f3b2e6db26a50d86f78cd0fc30067323ce2b0a1 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 8 May 2021 12:25:14 +0200 Subject: [PATCH 64/68] platformio_orig.ini: remove BSEC linker path --- platformio_orig.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/platformio_orig.ini b/platformio_orig.ini index 6542c2c51..44cbf0e7c 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -111,7 +111,6 @@ build_flags_basic = '-D LIBPAX_BLE' '-D LIBPAX_ARDUINO' build_flags_sensors = - -Llib/Bosch-BSEC/src/esp32/ -lalgobsec build_flags_all = ${common.build_flags_basic} From c6347216b485571d8c75bb4caf5fa92d9b9cb5fd Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 8 May 2021 12:37:54 +0200 Subject: [PATCH 65/68] remove build_flags_sensors --- README.md | 2 +- platformio_orig.ini | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 958b4abbc..94b1e70fa 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ by pressing the button of the device. # Sensors and Peripherals -You can add up to 3 user defined sensors. Insert your sensor's payload scheme in [*sensor.cpp*](src/sensor.cpp). Bosch BMP180 / BME280 / BME680 environment sensors are supported, to activate configure BME in board's hal file before build. If you need Bosch's proprietary BSEC libraray (e.g. to get indoor air quality value from BME680) further enable *build_flags_sensors*, which comes on the price of reduced RAM and increased build size. Furthermore, SDS011, RTC DS3231, generic serial NMEA GPS, I2C LoPy GPS are supported, and to be configured in board's hal file. See [*generic.h*](src/hal/generic.h) for all options and for proper configuration of BME280/BME680. +You can add up to 3 user defined sensors. Insert your sensor's payload scheme in [*sensor.cpp*](src/sensor.cpp). Bosch BMP180 / BME280 / BME680 environment sensors are supported, to activate configure BME in board's hal file before build. Furthermore, SDS011, RTC DS3231, generic serial NMEA GPS, I2C LoPy GPS are supported, and to be configured in board's hal file. See [*generic.h*](src/hal/generic.h) for all options and for proper configuration of BME280/BME680. Output of user sensor data can be switched by user remote control command 0x14 sent to Port 2. diff --git a/platformio_orig.ini b/platformio_orig.ini index 44cbf0e7c..c76c1b96b 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -110,11 +110,8 @@ build_flags_basic = '-D LIBPAX_WIFI' '-D LIBPAX_BLE' '-D LIBPAX_ARDUINO' -build_flags_sensors = - -lalgobsec build_flags_all = ${common.build_flags_basic} - ${common.build_flags_sensors} -mfix-esp32-psram-cache-issue [env] From 85f1b746784406623506aa4d04757ec1d4209963 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 8 May 2021 14:25:14 +0200 Subject: [PATCH 66/68] libpax includes code sanitizations --- include/cyclic.h | 1 - include/libpax_helpers.h | 1 - include/senddata.h | 1 - 3 files changed, 3 deletions(-) diff --git a/include/cyclic.h b/include/cyclic.h index 94619a147..d53b10e2e 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -1,7 +1,6 @@ #ifndef _CYCLIC_H #define _CYCLIC_H -#include #include "globals.h" #include "senddata.h" #include "rcommand.h" diff --git a/include/libpax_helpers.h b/include/libpax_helpers.h index e11958ec8..74be26a48 100644 --- a/include/libpax_helpers.h +++ b/include/libpax_helpers.h @@ -3,7 +3,6 @@ #include #include -#include "globals.h" #include "senddata.h" #include "configmanager.h" diff --git a/include/senddata.h b/include/senddata.h index 5d707a769..4ae830182 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -1,7 +1,6 @@ #ifndef _SENDDATA_H #define _SENDDATA_H -#include #include "libpax_helpers.h" #include "spislave.h" #include "mqttclient.h" From 3e00fecb28f2ab12f2876c243202d3aeb5f9d2a8 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 9 May 2021 00:02:41 +0200 Subject: [PATCH 67/68] migration to ezTime lib --- include/dcf77.h | 1 + include/display.h | 2 +- include/globals.h | 3 +-- include/if482.h | 3 ++- include/mobaserial.h | 1 + include/timekeeper.h | 1 - platformio_orig.ini | 3 +-- src/dcf77.cpp | 28 +++++++++++++--------------- src/display.cpp | 16 ++++------------ src/gpsread.cpp | 16 ++++++++-------- src/if482.cpp | 15 ++++++--------- src/ledmatrixdisplay.cpp | 9 +++------ src/main.cpp | 5 +++++ src/mobaserial.cpp | 11 +++++------ src/paxcounter_orig.conf | 5 +---- src/timekeeper.cpp | 21 +++++++++------------ src/timesync.cpp | 13 +++++++------ 17 files changed, 68 insertions(+), 85 deletions(-) diff --git a/include/dcf77.h b/include/dcf77.h index 4159d0401..964389ee0 100644 --- a/include/dcf77.h +++ b/include/dcf77.h @@ -2,6 +2,7 @@ #define _DCF77_H #include "globals.h" +#include "timekeeper.h" #define DCF77_FRAME_SIZE (60) #define DCF77_PULSE_LENGTH (100) diff --git a/include/display.h b/include/display.h index 67191c5bc..2b00aa448 100644 --- a/include/display.h +++ b/include/display.h @@ -88,7 +88,7 @@ void dp_refresh(bool nextPage = false); void dp_init(bool verbose = false); void dp_shutdown(void); void dp_message(const char *msg, int line, bool invers); -void dp_drawPage(time_t t, bool nextpage); +void dp_drawPage(bool nextpage); void dp_println(int lines = 1); void dp_printf(const char *format, ...); void dp_setFont(int font, int inv = 0); diff --git a/include/globals.h b/include/globals.h index 95816f418..6ef437ed5 100644 --- a/include/globals.h +++ b/include/globals.h @@ -5,8 +5,7 @@ #include // Time functions -#include "microTime.h" -#include +#include #include #include diff --git a/include/if482.h b/include/if482.h index 4f6447e7a..c1826b8b2 100644 --- a/include/if482.h +++ b/include/if482.h @@ -2,10 +2,11 @@ #define _IF482_H #include "globals.h" +#include "timekeeper.h" #define IF482_FRAME_SIZE (17) #define IF482_SYNC_FIXUP (10) // calibration to fixup processing time [milliseconds] -String IRAM_ATTR IF482_Frame(time_t tt); +String IRAM_ATTR IF482_Frame(time_t t); #endif \ No newline at end of file diff --git a/include/mobaserial.h b/include/mobaserial.h index c3b47ab87..a9d568979 100644 --- a/include/mobaserial.h +++ b/include/mobaserial.h @@ -2,6 +2,7 @@ #define _MOBALINE_H #include "globals.h" +#include "timekeeper.h" #include "dcf77.h" #define MOBALINE_FRAME_SIZE (33) diff --git a/include/timekeeper.h b/include/timekeeper.h index ac85223fa..2fc2c879b 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -3,7 +3,6 @@ #include "globals.h" #include "rtctime.h" -#include "TimeLib.h" #include "irqhandler.h" #include "timesync.h" #include "gpsread.h" diff --git a/platformio_orig.ini b/platformio_orig.ini index c76c1b96b..b3885347b 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -83,8 +83,7 @@ lib_deps_sensors = lib_deps_basic = https://github.com/SukkoPera/Arduino-Rokkit-Hash.git bblanchon/ArduinoJson @ ^6 - jchristensen/Timezone @ ^1.2.4 - https://github.com/cyberman54/microTime.git + m5ez/ezTime @ ^0.8.3 makuna/RTC @ ^2.3.5 spacehuhn/SimpleButton lewisxhe/AXP202X_Library @ ^1.1.3 diff --git a/src/dcf77.cpp b/src/dcf77.cpp index 459245284..e579b2168 100644 --- a/src/dcf77.cpp +++ b/src/dcf77.cpp @@ -20,11 +20,9 @@ static const char TAG[] = __FILE__; void DCF77_Pulse(time_t t, uint8_t const *DCFpulse) { TickType_t startTime = xTaskGetTickCount(); - uint8_t sec = second(t); + uint8_t sec = myTZ.second(t); - t = myTZ.toLocal(now()); - ESP_LOGD(TAG, "[%02d:%02d:%02d.%03d] DCF second %d", hour(t), minute(t), - second(t), millisecond(), sec); + ESP_LOGD(TAG, "[%s] DCF second: %d", myTZ.dateTime("H:i:s.v").c_str(), sec); // induce a DCF Pulse for (uint8_t pulse = 0; pulse <= 2; pulse++) { @@ -53,7 +51,7 @@ void DCF77_Pulse(time_t t, uint8_t const *DCFpulse) { } // for } // DCF77_Pulse() -uint8_t *IRAM_ATTR DCF77_Frame(time_t const tt) { +uint8_t *IRAM_ATTR DCF77_Frame(time_t const t) { // array of dcf pulses for one minute, secs 0..16 and 20 are never touched, so // we keep them statically to avoid same recalculation every minute @@ -64,35 +62,35 @@ uint8_t *IRAM_ATTR DCF77_Frame(time_t const tt) { dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_1}; uint8_t Parity; - time_t t = myTZ.toLocal(tt); // convert to local time // ENCODE DST CHANGE ANNOUNCEMENT (Sec 16) DCFpulse[16] = dcf_0; // not yet implemented // ENCODE DAYLIGHTSAVING (secs 17..18) - DCFpulse[17] = myTZ.locIsDST(t) ? dcf_1 : dcf_0; - DCFpulse[18] = myTZ.locIsDST(t) ? dcf_0 : dcf_1; + DCFpulse[17] = myTZ.isDST(t) ? dcf_1 : dcf_0; + DCFpulse[18] = myTZ.isDST(t) ? dcf_0 : dcf_1; // ENCODE MINUTE (secs 21..28) - Parity = dec2bcd(minute(t), 21, 27, DCFpulse); + Parity = dec2bcd(myTZ.minute(t), 21, 27, DCFpulse); DCFpulse[28] = setParityBit(Parity); // ENCODE HOUR (secs 29..35) - Parity = dec2bcd(hour(t), 29, 34, DCFpulse); + Parity = dec2bcd(myTZ.hour(t), 29, 34, DCFpulse); DCFpulse[35] = setParityBit(Parity); // ENCODE DATE (secs 36..58) - Parity = dec2bcd(day(t), 36, 41, DCFpulse); - Parity += dec2bcd((weekday(t) - 1) ? (weekday(t) - 1) : 7, 42, 44, DCFpulse); - Parity += dec2bcd(month(t), 45, 49, DCFpulse); - Parity += dec2bcd(year(t) - 2000, 50, 57, DCFpulse); + Parity = dec2bcd(myTZ.day(t), 36, 41, DCFpulse); + Parity += dec2bcd((myTZ.weekday(t) - 1) ? (myTZ.weekday(t) - 1) : 7, 42, 44, + DCFpulse); + Parity += dec2bcd(myTZ.month(t), 45, 49, DCFpulse); + Parity += dec2bcd(myTZ.year(t) - 2000, 50, 57, DCFpulse); DCFpulse[58] = setParityBit(Parity); // ENCODE MARK (sec 59) DCFpulse[59] = dcf_Z; // !! missing code here for leap second !! // timestamp this frame with it's minute - DCFpulse[60] = minute(t); + DCFpulse[60] = myTZ.minute(t); return DCFpulse; diff --git a/src/display.cpp b/src/display.cpp index e535e4059..34046274c 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -39,9 +39,6 @@ MY_FONT_STRETCHED: 16x32px = 8 chars / line // local Tag for logging static const char TAG[] = __FILE__; -// helper array for converting month values to text -const char *printmonth[] = {"xxx", "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; uint8_t DisplayIsOn = 0; uint8_t displaybuf[MY_DISPLAY_WIDTH * MY_DISPLAY_HEIGHT / 8] = {0}; static uint8_t plotbuf[MY_DISPLAY_WIDTH * MY_DISPLAY_HEIGHT / 8] = {0}; @@ -191,9 +188,6 @@ void dp_refresh(bool nextPage) { if (!DisplayIsOn && (DisplayIsOn == cfg.screenon)) return; - const time_t t = - myTZ.toLocal(now()); // note: call now() here *before* locking mutex! - // block i2c bus access if (!I2C_MUTEX_LOCK()) ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", _seconds()); @@ -212,7 +206,7 @@ void dp_refresh(bool nextPage) { } #endif - dp_drawPage(t, nextPage); + dp_drawPage(nextPage); dp_dump(displaybuf); I2C_MUTEX_UNLOCK(); // release i2c bus access @@ -220,7 +214,7 @@ void dp_refresh(bool nextPage) { } // mutex } // refreshDisplay() -void dp_drawPage(time_t t, bool nextpage) { +void dp_drawPage(bool nextpage) { // write display content to display buffer // nextpage = true -> flip 1 page @@ -330,9 +324,7 @@ void dp_drawPage(time_t t, bool nextpage) { #if (TIME_SYNC_INTERVAL) timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource]; TimePulseTick = false; - - dp_printf("%02d.%3s %4d", day(t), printmonth[month(t)], year(t)); - dp_printf(" %02d:%02d:%02d", hour(t), minute(t), second(t)); + dp_printf("%s", myTZ.dateTime("d.M Y H:i:s").c_str()); // display inverse timeState if clock controller is enabled #if (defined HAS_DCF77) || (defined HAS_IF482) @@ -450,7 +442,7 @@ void dp_drawPage(time_t t, bool nextpage) { dp_setFont(MY_FONT_LARGE); dp_setTextCursor(0, 4); - dp_printf("%02d:%02d:%02d", hour(t), minute(t), second(t)); + dp_printf("%s", myTZ.dateTime("H:i:s").c_str()); break; // ---------- page 5: pax graph ---------- diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 79c0847dd..a8f966355 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -111,16 +111,16 @@ time_t get_gpstime(uint16_t *msec) { uint32_t zdatime = atof(gpstime.value()); // convert time to maketime format and make time - tm.Second = zdatime % 100; // second - tm.Minute = (zdatime / 100) % 100; // minute - tm.Hour = zdatime / 10000; // hour - tm.Day = atoi(gpsday.value()); // day - tm.Month = atoi(gpsmonth.value()); // month - tm.Year = CalendarYrToTm(atoi(gpsyear.value())); // year offset from 1970 + tm.Second = zdatime % 100; // second + tm.Minute = (zdatime / 100) % 100; // minute + tm.Hour = zdatime / 10000; // hour + tm.Day = atoi(gpsday.value()); // day + tm.Month = atoi(gpsmonth.value()); // month + tm.Year = atoi(gpsyear.value()) - 1970; // year offset from 1970 t = makeTime(tm); - ESP_LOGD(TAG, "GPS time/date = %02d:%02d:%02d / %02d.%02d.%2d", tm.Hour, - tm.Minute, tm.Second, tm.Day, tm.Month, tm.Year + 1970); + ESP_LOGD(TAG, "GPS date/time: %s", + UTC.dateTime(t, "d.M Y H:i:s T").c_str()); // add protocol delay with millisecond precision t += delay_ms / 1000 - 1; // whole seconds diff --git a/src/if482.cpp b/src/if482.cpp index 7511a821c..afbc16cc1 100644 --- a/src/if482.cpp +++ b/src/if482.cpp @@ -14,7 +14,7 @@ IF482.cpp depends on code in RTCTIME.cpp. /* IF482 Generator to control clocks with IF482 telegram input (e.g. BÜRK BU190) - + Example IF482 telegram: "OAL160806F170400" @@ -84,9 +84,8 @@ not evaluated by model BU-190, use "F" instead for this model // Local logging tag static const char TAG[] = __FILE__; -String IRAM_ATTR IF482_Frame(time_t printTime) { +String IRAM_ATTR IF482_Frame(time_t t) { - time_t t = myTZ.toLocal(printTime); char mon, out[IF482_FRAME_SIZE + 1]; switch (timeStatus()) { // indicates if time has been set and recently synced @@ -102,13 +101,11 @@ String IRAM_ATTR IF482_Frame(time_t printTime) { } // switch // generate IF482 telegram - snprintf(out, sizeof(out), "O%cL%02u%02u%02u%1u%02u%02u%02u\r", mon, - year(t) - 2000, month(t), day(t), weekday(t), hour(t), minute(t), - second(t)); + snprintf(out, sizeof(out), "O%cL%s\r", mon, myTZ.dateTime(t, UTC_TIME, "ymdwHis").c_str()); + + ESP_LOGD(TAG, "[%s] IF482 date/time: %s", myTZ.dateTime("H:i:s.v").c_str(), + out); - t = myTZ.toLocal(now()); - ESP_LOGD(TAG, "[%02d:%02d:%02d.%03d] IF482 = %s", hour(t), minute(t), - second(t), millisecond(), out); return out; } diff --git a/src/ledmatrixdisplay.cpp b/src/ledmatrixdisplay.cpp index 61b4fc89d..2abd38aa0 100644 --- a/src/ledmatrixdisplay.cpp +++ b/src/ledmatrixdisplay.cpp @@ -12,7 +12,7 @@ static const char TAG[] = __FILE__; uint8_t MatrixDisplayIsOn = 0; static uint8_t displaybuf[LED_MATRIX_WIDTH * LED_MATRIX_HEIGHT / 8] = {0}; static unsigned long ulLastNumMacs = 0; -static time_t ulLastTime = myTZ.toLocal(now()); +static time_t ulLastTime = now(); hw_timer_t *matrixDisplayIRQ = NULL; @@ -47,7 +47,6 @@ void init_matrix_display(bool reverse) { void refreshTheMatrixDisplay(bool nextPage) { static uint8_t DisplayPage = 0, col = 0, row = 0; uint8_t level; - char buff[16]; // if Matrixdisplay is switched off we don't refresh it to relax cpu if (!MatrixDisplayIsOn && (MatrixDisplayIsOn == cfg.screenon)) @@ -117,13 +116,11 @@ void refreshTheMatrixDisplay(bool nextPage) { case 1: - const time_t t = myTZ.toLocal(now()); + const time_t t = now(); if (ulLastTime != t) { ulLastTime = t; matrix.clear(); - snprintf(buff, sizeof(buff), "%02d:%02d:%02d", hour(t), minute(t), - second(t)); - DrawNumber(String(buff)); + DrawNumber(myTZ.dateTime("H:i:s").c_str()); } break; diff --git a/src/main.cpp b/src/main.cpp index f1a977a89..fd3648586 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -118,6 +118,11 @@ void setup() { // load device configuration from NVRAM and set runmode do_after_reset(); + // set time zone to user value from paxcounter.conf +#ifdef TIME_SYNC_TIMEZONE + myTZ.setPosix(TIME_SYNC_TIMEZONE); +#endif + // hash 6 byte device MAC to 4 byte clientID uint8_t mac[6]; esp_eth_get_mac(mac); diff --git a/src/mobaserial.cpp b/src/mobaserial.cpp index 7c330e15e..bffdea099 100644 --- a/src/mobaserial.cpp +++ b/src/mobaserial.cpp @@ -18,11 +18,10 @@ static const char TAG[] = __FILE__; void MOBALINE_Pulse(time_t t, uint8_t const *DCFpulse) { TickType_t startTime = xTaskGetTickCount(); - uint8_t sec = second(t); + uint8_t sec = myTZ.second(t); - t = myTZ.toLocal(now()); - ESP_LOGD(TAG, "[%02d:%02d:%02d.%03d] MOBALINE bit %d", hour(t), minute(t), - second(t), millisecond(), sec); + ESP_LOGD(TAG, "[%s] MOBALINE sec: %d", myTZ.dateTime("H:i:s.v").c_str(), + sec); // induce 3 pulses for (uint8_t pulse = 0; pulse <= 3; pulse++) { @@ -68,13 +67,13 @@ uint8_t *IRAM_ATTR MOBALINE_Frame(time_t const tt) { static uint8_t DCFpulse[DCF77_FRAME_SIZE + 1]; - time_t t = myTZ.toLocal(tt); // convert to local time + time_t t = myTZ.tzTime(tt); // convert to local time // ENCODE HEAD (bit 0)) DCFpulse[0] = dcf_Z; // not yet implemented // ENCODE DAYLIGHTSAVING (bit 1) - DCFpulse[1] = myTZ.locIsDST(t) ? dcf_1 : dcf_0; + DCFpulse[1] = myTZ.isDST(t) ? dcf_1 : dcf_0; // ENCODE DATE (bits 2..20) dec2bcd(false, year(t) - 2000, 2, 9, DCFpulse); diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index b1235d11e..988cbb1ce 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -88,10 +88,7 @@ #define TIME_SYNC_CYCLE 60 // delay between two time samples [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 -#define DAYLIGHT_TIME {"CEST", Last, Sun, Mar, 2, 120} // Central European Summer Time -#define STANDARD_TIME {"CET ", Last, Sun, Oct, 3, 60} // Central European Standard Time +#define TIME_SYNC_TIMEZONE "CET-1CEST,M3.4.0/2,M10.4.0/3" // Timezone in POSIX format (example shows Germany/Berlin) // Ports on which the device sends and listenes on LoRaWAN and SPI #define COUNTERPORT 1 // counts diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 885716d5b..031259a0c 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -8,7 +8,7 @@ #endif #endif -#define _COMPILETIME myTZ.toUTC(RtcDateTime(__DATE__, __TIME__).Epoch32Time()) +#define _COMPILETIME compileTime() // Local logging tag static const char TAG[] = __FILE__; @@ -17,10 +17,8 @@ static const char TAG[] = __FILE__; // G = GPS / R = RTC / L = LORA / ? = unsynced / = sync unknown const char timeSetSymbols[] = {'G', 'R', 'L', '?', ' '}; -// set Time Zone for user setting from paxcounter.conf -TimeChangeRule myDST = DAYLIGHT_TIME; -TimeChangeRule mySTD = STANDARD_TIME; -Timezone myTZ(myDST, mySTD); +// set Time Zone +Timezone myTZ; bool volatile TimePulseTick = false; timesource_t timeSource = _unsynced; @@ -115,7 +113,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, CLOCKIRQ(); // fire clock pps, this advances time 1 sec } - setTime(time_to_set); // set the time on top of second + UTC.setTime(time_to_set); // set the time on top of second timeSource = mytimesource; // set global variable timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ); @@ -173,7 +171,6 @@ uint8_t timepulse_init() { } // timepulse_init void timepulse_start(void) { - #ifdef GPS_INT // start external clock gps pps line attachInterrupt(digitalPinToInterrupt(GPS_INT), CLOCKIRQ, RISING); #elif defined RTC_INT // start external clock rtc @@ -193,7 +190,7 @@ void IRAM_ATTR CLOCKIRQ(void) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; - SyncToPPS(); // advance systime, see microTime.h + // syncToPPS(); // currently not used // advance wall clock, if we have #if (defined HAS_IF482 || defined HAS_DCF77) @@ -213,10 +210,10 @@ void IRAM_ATTR CLOCKIRQ(void) { portYIELD_FROM_ISR(); } -// helper function to check plausibility of a time +// helper function to check plausibility of a given epoch time time_t timeIsValid(time_t const t) { // is it a time in the past? we use compile date to guess - return (t >= _COMPILETIME ? t : 0); + return (t < myTZ.tzTime(_COMPILETIME) ? 0 : t); } // helper function to calculate serial transmit time @@ -301,7 +298,7 @@ void clock_loop(void *taskparameter) { // ClockTask t = time_t(printtime); // new adjusted UTC time seconds // send IF482 telegram - IF482.print(IF482_Frame(t + 1)); // note: telegram is for *next* second + IF482.print(IF482_Frame(t + 2)); // note: telegram is for *next* second #elif defined HAS_DCF77 @@ -310,7 +307,7 @@ void clock_loop(void *taskparameter) { // ClockTask if (minute(nextmin(t)) == // do we still have a recent frame? DCFpulse[DCF77_FRAME_SIZE]) // (timepulses could be missed!) - DCF77_Pulse(t, DCFpulse); // then output current second's pulse + DCF77_Pulse(t + 1, DCFpulse); // then output next second's pulse // else we have no recent frame, thus suppressing clock output diff --git a/src/timesync.cpp b/src/timesync.cpp index 55a0d82fd..56bb52a7d 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -97,8 +97,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) { // wait until a timestamp was received if (xTaskNotifyWait(0x00, ULONG_MAX, &rcv_seqNo, pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == pdFALSE) { - ESP_LOGW(TAG, "[d%0.3f] Timesync aborted: timed out", - _seconds()); + ESP_LOGW(TAG, "[d%0.3f] Timesync aborted: timed out", _seconds()); goto Fail; // no timestamp received before timeout } @@ -143,6 +142,9 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) { time_offset_ms += TIME_SYNC_FIXUP; time_offset_ms %= 1000; + ESP_LOGD(TAG, "LORA date/time: %s", + myTZ.dateTime(time_offset_sec, "d.M Y H:i:s T").c_str()); + setMyTime(time_offset_sec, time_offset_ms, _lora); // send timesync end char to show timesync was successful @@ -166,8 +168,8 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) { // store incoming timestamps void timesync_store(uint32_t timestamp, timesync_t timestamp_type) { - ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", _seconds(), - time_sync_seqNo, sample_idx, timestamp_type, timestamp); + ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", _seconds(), time_sync_seqNo, + sample_idx, timestamp_type, timestamp); timesync_timestamp[sample_idx][timestamp_type] = timestamp; } @@ -232,8 +234,7 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) { lmic_time_reference_t lmicTime; if (flag != 1) { - ESP_LOGW(TAG, "[%0.3f] Network did not answer time request", - _seconds()); + ESP_LOGW(TAG, "[%0.3f] Network did not answer time request", _seconds()); goto Exit; } From 67c0313e8fadeaa1e364b78d9b0a501c2781e07d Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 29 May 2021 17:01:08 +0200 Subject: [PATCH 68/68] v3.0.0 --- README.md | 2 +- platformio_orig.ini | 21 ++++++++++++--------- src/paxcounter_orig.conf | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 94b1e70fa..ed4dc286e 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,7 @@ Paxcounter can be used to sync a wall clock which has a DCF77 or IF482 time tele This describes how to set up a mobile PaxCounter:
Follow all steps so far for preparing the device, selecting the packed payload format. In `paxcounter.conf` set PAYLOAD_OPENSENSEBOX to 1. Register a new sensebox on https://opensensemap.org/. In the sensor configuration select "TheThingsNetwork" and set decoding profile to "LoRa serialization". Enter your TTN Application and Device ID. Setup decoding option using `[{"decoder":"latLng"},{"decoder":"uint16",sensor_id":"yoursensorid"}]` -# Covid-19 Exposure Notification System beacon detection +# Covid-19 Exposure Notification System beacon detection (currently NOT working with v3.0.x, use v2.4.x for this feature) Bluetooth low energy service UUID 0xFD6F, used by Google/Apple COVID-19 Exposure Notification System, can be monitored and counted. By comparing with the total number of observed devices this gives an indication how many people staying in proximity are using Apps for tracing COVID-19 exposures, e.g. in Germany the "Corona Warn App". To achive best results with this funcion, use following settings in `paxcounter.conf`: diff --git a/platformio_orig.ini b/platformio_orig.ini index b3885347b..5d00503b9 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -1,5 +1,4 @@ ; PlatformIO Project Configuration File -; NOTE: PlatformIO v5 is needed! ; ; Please visit documentation for the other options and examples ; http://docs.platformio.org/page/projectconf.html @@ -41,7 +40,7 @@ halfile = ttgobeam10.h [platformio] ; upload firmware to board with usb cable default_envs = usb -; upload firmware to pax.express repository +; upload firmware to a paxexpress repository ;default_envs = ota ; use latest versions of libraries ;default_envs = dev @@ -49,7 +48,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.6.0 +release_version = 3.0.0 ; 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 @@ -62,12 +61,13 @@ monitor_speed = 115200 upload_speed = 115200 ; set by build.py and taken from hal file display_library = ; set by build.py and taken from hal file lib_deps_lora = - mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 + ;mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 + https://github.com/mcci-catena/arduino-lmic.git lib_deps_display = bitbank2/OneBitDisplay @ ^1.10.0 bitbank2/BitBang_I2C @ ^2.1.3 ricmoo/QRCode @ ^0.0.1 - bodmer/TFT_eSPI @ ^2.3.51 + bodmer/TFT_eSPI @ ^2.3.58 lib_deps_ledmatrix = seeed-studio/Ultrathin_LED_Matrix @ ^1.0.0 lib_deps_rgbled = @@ -77,18 +77,18 @@ lib_deps_gps = lib_deps_sensors = adafruit/Adafruit Unified Sensor @ ^1.1.4 adafruit/Adafruit BME280 Library @ ^2.1.1 - adafruit/Adafruit BMP085 Library @ ^1.1.0 + adafruit/Adafruit BMP085 Library @ ^1.2.0 boschsensortec/BSEC Software Library @ 1.6.1480 https://github.com/ricki-z/SDS011.git lib_deps_basic = https://github.com/SukkoPera/Arduino-Rokkit-Hash.git bblanchon/ArduinoJson @ ^6 - m5ez/ezTime @ ^0.8.3 + ropg/ezTime @ ^0.8.3 makuna/RTC @ ^2.3.5 spacehuhn/SimpleButton lewisxhe/AXP202X_Library @ ^1.1.3 geeksville/esp32-micro-sdcard @ ^0.1.1 - 256dpi/MQTT @ ^2.4.7 + 256dpi/MQTT @ ^2.4.8 https://github.com/cyberman54/libpax.git lib_deps_all = ${common.lib_deps_basic} @@ -119,7 +119,7 @@ framework = arduino board = esp32dev board_build.partitions = min_spiffs.csv upload_speed = ${common.upload_speed} -;upload_port = COM3 +;upload_port = COM12 platform = ${common.platform_espressif32} lib_deps = ${common.lib_deps_all} build_flags = ${common.build_flags_all} @@ -138,3 +138,6 @@ upload_protocol = esptool upload_protocol = esptool build_type = debug platform = https://github.com/platformio/platform-espressif32.git#develop +platform_packages = + ; use upstream Git version + framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git \ No newline at end of file diff --git a/src/paxcounter_orig.conf b/src/paxcounter_orig.conf index 988cbb1ce..fec83f51a 100644 --- a/src/paxcounter_orig.conf +++ b/src/paxcounter_orig.conf @@ -28,7 +28,7 @@ #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle // Corona Exposure Notification Service(ENS) counter -#define COUNT_ENS 1 // count found number of devices which advertise Exposure Notification Service +#define COUNT_ENS 0 // count found number of devices which advertise Exposure Notification Service // set to 1 if you want to enable this function [default=0] /* Note: guide for setting bluetooth parameters