From 43cf6b7fe5968de683f127bf01f96bf8a443e609 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Mon, 19 Aug 2019 21:48:11 +0200 Subject: [PATCH 1/6] ledmatrix.cpp small code sanitization --- include/ledmatrixdisplay.h | 2 +- src/ledmatrixdisplay.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/ledmatrixdisplay.h b/include/ledmatrixdisplay.h index d5878f71..b7ec5a3b 100644 --- a/include/ledmatrixdisplay.h +++ b/include/ledmatrixdisplay.h @@ -13,6 +13,6 @@ void refreshTheMatrixDisplay(bool nextPage = false); void DrawNumber(String strNum, uint8_t iDotPos = 0); uint8_t GetCharFromFont(char cChar); uint8_t GetCharWidth(char cChar); -void ScrollLeft(uint8_t *buf, uint16_t cols, uint16_t rows); +void ScrollLeft(uint8_t *buf, const uint16_t cols, const uint16_t rows); #endif \ No newline at end of file diff --git a/src/ledmatrixdisplay.cpp b/src/ledmatrixdisplay.cpp index ffdad8c0..881f57fe 100644 --- a/src/ledmatrixdisplay.cpp +++ b/src/ledmatrixdisplay.cpp @@ -204,13 +204,14 @@ uint8_t GetCharWidth(char cChar) { return CharDescriptor.width; } -void ScrollLeft(uint8_t *buf, uint16_t cols, uint16_t rows) { +void ScrollLeft(uint8_t *buf, const uint16_t cols, const uint16_t rows) { uint32_t i, k, idx; + const uint32_t x = cols / 8; for (k = 0; k < rows; k++) { // scroll a line with x bytes one dot to the left - for (i = 0; i < cols / 8 - 1; ++i) { - idx = i + k * cols / 8; + for (i = 0; i < x - 1; ++i) { + idx = i + k * x; buf[idx] = (buf[idx] << 1) | ((buf[idx + 1] >> 7) & 1); } buf[idx + 1] <<= 1; From bbca15a76e2a11e1dcf093421abf72fc66cae555 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Sun, 25 Aug 2019 17:18:29 +0200 Subject: [PATCH 2/6] rcommand.cpp: set payload mask & set BME data --- README.md | 11 ++++++++++- src/bmesensor.cpp | 4 +++- src/rcommand.cpp | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c4993e5b..cbdaab95 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ If you're using a device with OLED display, or if you add such one to the I2C bu You can add up to 3 user defined sensors. Insert sensor's payload scheme in [*sensor.cpp*](src/sensor.cpp). Bosch BME280 / BME680 environment sensors are supported. Enable flag *lib_deps_sensors* for your board in [*platformio.ini*](src/platformio.ini) and 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. 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 0x13 sent to Port 2. +Output of user sensor data can be switched by user remote control command 0x14 sent to Port 2. Output of sensor and peripheral data is internally switched by a bitmask register. Default mask (0xFF) can be tailored by editing *cfg.payloadmask* initialization value in [*configmanager.cpp*](src/configmanager.cpp) following this scheme: @@ -383,6 +383,15 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. byte 1 = user sensor number (1..3) byte 2 = sensor mode (0 = disabled / 1 = enabled [default]) +0x14 set payload mask + + byte 1 = sensor data payload mask (0..255, meaning of bits see above) + +0x15 set BME data on/off + + 0 = BME data off + 1 = BME data on, sends BME data on port 7 [default] + 0x80 get device configuration Device answers with it's current configuration on Port 3. diff --git a/src/bmesensor.cpp b/src/bmesensor.cpp index 78298798..c6b77fc4 100644 --- a/src/bmesensor.cpp +++ b/src/bmesensor.cpp @@ -143,7 +143,9 @@ int checkIaqSensorStatus(void) { // store current BME sensor data in struct void bme_storedata(bmeStatus_t *bme_store) { - if (I2C_MUTEX_LOCK()) { // block i2c bus access + + if ((cfg.payloadmask && MEMS_DATA) & + (I2C_MUTEX_LOCK())) { // block i2c bus access #ifdef HAS_BME680 if (iaqSensor.run()) { // if new data is available diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 415283a9..be00f21e 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -131,6 +131,20 @@ void set_gps(uint8_t val[]) { } } +void set_bme(uint8_t val[]) { + ESP_LOGI(TAG, "Remote command: set BME mode to %s", val[0] ? "on" : "off"); + if (val[0]) { + cfg.payloadmask |= (uint8_t)MEMS_DATA; // set bit in mask + } else { + cfg.payloadmask &= ~(uint8_t)MEMS_DATA; // clear bit in mask + } +} + +void set_payloadmask(uint8_t val[]) { + ESP_LOGI(TAG, "Remote command: set payload mask to %X", val[0]); + cfg.payloadmask = val[0]; +} + void set_sensor(uint8_t val[]) { #if (HAS_SENSORS) switch (val[0]) { // check if valid sensor number 1...4 @@ -307,7 +321,8 @@ cmd_t table[] = { {0x0d, set_vendorfilter, 1, false}, {0x0e, set_blescan, 1, true}, {0x0f, set_wifiant, 1, true}, {0x10, set_rgblum, 1, true}, {0x11, set_monitor, 1, true}, {0x12, set_beacon, 7, false}, - {0x13, set_sensor, 2, true}, {0x80, get_config, 0, false}, + {0x13, set_sensor, 2, true}, {0x14, set_payloadmask, 1, true}, + {0x15, set_bme, 1, true}, {0x80, get_config, 0, false}, {0x81, get_status, 0, false}, {0x84, get_gps, 0, false}, {0x85, get_bme, 0, false}, {0x86, get_time, 0, false}, {0x87, set_time, 0, false}, {0x99, set_flush, 0, false}}; From 829ab0ea0551ead6abf64f37c3c051499bc9b193 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Wed, 28 Aug 2019 10:48:39 +0200 Subject: [PATCH 3/6] bugfix message queuing to LMIC while LMIC is busy --- include/globals.h | 7 ++++--- include/lorawan.h | 2 +- include/spislave.h | 2 +- src/lorawan.cpp | 29 +++++++++++++---------------- src/senddata.cpp | 8 +++++--- src/spislave.cpp | 4 +++- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/include/globals.h b/include/globals.h index e8e8bb48..a112e481 100644 --- a/include/globals.h +++ b/include/globals.h @@ -45,6 +45,9 @@ #define I2C_MUTEX_LOCK() (xSemaphoreTake(I2Caccess, pdMS_TO_TICKS(10)) == pdTRUE) #define I2C_MUTEX_UNLOCK() (xSemaphoreGive(I2Caccess)) +enum sendprio_t { prio_low, prio_normal, prio_high }; +enum timesource_t { _gps, _rtc, _lora, _unsynced }; + // Struct holding devices's runtime configuration typedef struct { uint8_t lorasf; // 7-12, lora spreadfactor @@ -73,6 +76,7 @@ typedef struct { typedef struct { uint8_t MessageSize; uint8_t MessagePort; + sendprio_t MessagePrio; uint8_t Message[PAYLOAD_BUFFER_SIZE]; } MessageBuffer_t; @@ -95,9 +99,6 @@ typedef struct { float gas; // raw gas sensor signal } bmeStatus_t; -enum sendprio_t { prio_low, prio_normal, prio_high }; -enum timesource_t { _gps, _rtc, _lora, _unsynced }; - extern std::set, Mallocator> macs; extern std::array::iterator it; extern std::array beacons; diff --git a/include/lorawan.h b/include/lorawan.h index b3331751..5762297c 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -35,7 +35,7 @@ void os_getDevEui(u1_t *buf); void showLoraKeys(void); void switch_lora(uint8_t sf, uint8_t tx); void lora_send(osjob_t *job); -void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio); +void lora_enqueuedata(MessageBuffer_t *message); void lora_queuereset(void); #if (TIME_SYNC_LORAWAN) void user_request_network_time_callback(void *pVoidUserUTCTime, diff --git a/include/spislave.h b/include/spislave.h index 44c58455..0d85a09e 100644 --- a/include/spislave.h +++ b/include/spislave.h @@ -28,7 +28,7 @@ licenses. Refer to LICENSE.txt file in repository for more details. esp_err_t spi_init(); -void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio); +void spi_enqueuedata(MessageBuffer_t *message); void spi_queuereset(); #endif // _SPISLAVE_H diff --git a/src/lorawan.cpp b/src/lorawan.cpp index e82ac1f5..3974c7f4 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -391,21 +391,16 @@ void switch_lora(uint8_t sf, uint8_t tx) { void lora_send(osjob_t *job) { MessageBuffer_t SendBuffer; - // Check if there is a pending TX/RX job running, if yes don't eat data - // since it cannot be sent right now - if ((LMIC.opmode & (OP_JOINING | OP_REJOIN | OP_TXDATA | OP_POLL)) != 0) { - // waiting for LoRa getting ready - } else { - if (xQueueReceive(LoraSendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) { - // SendBuffer now filled with next payload from queue - if (!LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, - SendBuffer.MessageSize, (cfg.countermode & 0x02))) { - ESP_LOGI(TAG, "%d byte(s) sent to LoRa", SendBuffer.MessageSize); - } else { - ESP_LOGE(TAG, "could not send %d byte(s) to LoRa", - SendBuffer.MessageSize); - } - // sprintf(display_line7, "PACKET QUEUED"); + if (xQueueReceive(LoraSendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) { + // SendBuffer now filled with next payload from queue + if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, + SendBuffer.MessageSize, + (cfg.countermode & 0x02)) == 0) { + ESP_LOGI(TAG, "%d byte(s) sent to LoRa", SendBuffer.MessageSize); + } else { + lora_enqueuedata(&SendBuffer); + // ESP_LOGE(TAG, "could not send %d byte(s) to LoRa, rescheduled", + // SendBuffer.MessageSize); } } // reschedule job every 0,5 - 1 sec. including a bit of random to prevent @@ -441,10 +436,12 @@ esp_err_t lora_stack_init() { return ESP_OK; // continue main program } -void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { +void lora_enqueuedata(MessageBuffer_t *message) { // enqueue message in LORA send queue BaseType_t ret; MessageBuffer_t DummyBuffer; + sendprio_t prio = message->MessagePrio; + switch (prio) { case prio_high: // clear space in queue if full, then fallthrough to normal diff --git a/src/senddata.cpp b/src/senddata.cpp index fd5df963..95577b24 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -10,9 +10,11 @@ void sendcycle() { // put data to send in RTos Queues used for transmit over channels Lora and SPI void SendPayload(uint8_t port, sendprio_t prio) { - MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, Message[] + MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, MessagePrio, Message[] SendBuffer.MessageSize = payload.getSize(); + SendBuffer.MessagePrio = prio; + switch (PAYLOAD_ENCODER) { case 1: // plain -> no mapping case 2: // packed -> no mapping @@ -42,10 +44,10 @@ void SendPayload(uint8_t port, sendprio_t prio) { // enqueue message in device's send queues #if (HAS_LORA) - lora_enqueuedata(&SendBuffer, prio); + lora_enqueuedata(&SendBuffer); #endif #ifdef HAS_SPI - spi_enqueuedata(&SendBuffer, prio); + spi_enqueuedata(&SendBuffer); #endif } // SendPayload diff --git a/src/spislave.cpp b/src/spislave.cpp index 9801058b..32bad108 100644 --- a/src/spislave.cpp +++ b/src/spislave.cpp @@ -147,10 +147,12 @@ esp_err_t spi_init() { return ret; } -void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { +void spi_enqueuedata(MessageBuffer_t *message) { // enqueue message in SPI send queue BaseType_t ret; MessageBuffer_t DummyBuffer; + sendprio_t prio = message->MessagePrio; + switch (prio) { case prio_high: // clear space in queue if full, then fallthrough to normal From 1c5edbb6a4906ddb1c901d89a9e947e1a6e4ca16 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Wed, 28 Aug 2019 10:49:25 +0200 Subject: [PATCH 4/6] esp_wifi_start (needed for next ESP-IDF release) --- src/wifiscan.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index 85fdf6a1..12ec9c82 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -57,7 +57,7 @@ void wifi_sniffer_init(void) { wificfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM wificfg.wifi_task_core_id = 0; // we want wifi task running on core 0 - //wifi_promiscuous_filter_t filter = { + // wifi_promiscuous_filter_t filter = { // .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // only MGMT frames // .filter_mask = WIFI_PROMIS_FILTER_MASK_ALL}; // we use all frames @@ -71,9 +71,8 @@ void wifi_sniffer_init(void) { ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL)); - ESP_ERROR_CHECK(esp_wifi_stop()); - ESP_ERROR_CHECK( - esp_wifi_set_promiscuous_filter(&filter)); // set frame filter + ESP_ERROR_CHECK(esp_wifi_start()); // channel switch throws error without + ESP_ERROR_CHECK(esp_wifi_set_promiscuous_filter(&filter)); // set frame filter ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode From 78c8ed3e9753df51ecbb6bed4a8fdddca1428ee0 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Wed, 28 Aug 2019 21:55:50 +0200 Subject: [PATCH 5/6] refactoring LMIC send queueing --- src/lorawan.cpp | 47 ++++++++++++++++++++++++++++------------------- src/senddata.cpp | 2 +- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 3974c7f4..fefb5db0 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -248,6 +248,11 @@ void onEvent(ev_t ev) { : PSTR("TX COMPLETE")); sprintf(display_line6, " "); // clear previous lmic status + // schedule next transmission with some random delay to prevent systematic + // collisions + os_setTimedCallback(&sendjob, os_getTime() + ms2osticks(random(500)), + lora_send); + if (LMIC.dataLen) { // did we receive payload data -> display info ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d", LMIC.dataLen, LMIC.rssi, LMIC.snr / 4); @@ -391,22 +396,22 @@ void switch_lora(uint8_t sf, uint8_t tx) { void lora_send(osjob_t *job) { MessageBuffer_t SendBuffer; - if (xQueueReceive(LoraSendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) { - // SendBuffer now filled with next payload from queue - if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, - SendBuffer.MessageSize, - (cfg.countermode & 0x02)) == 0) { - ESP_LOGI(TAG, "%d byte(s) sent to LoRa", SendBuffer.MessageSize); - } else { - lora_enqueuedata(&SendBuffer); - // ESP_LOGE(TAG, "could not send %d byte(s) to LoRa, rescheduled", - // SendBuffer.MessageSize); - } + + // Check if there is not a current TX/RX job running + if (LMIC.opmode & OP_TXRXPEND) { + ESP_LOGE(TAG, "LMIC busy, data not sent and lost"); + return; + } + + // fetch next payload to send from queue or wait until new payload shows up in + // queue + if (xQueueReceive(LoraSendQueue, &SendBuffer, portMAX_DELAY) == pdTRUE) { + if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, + SendBuffer.MessageSize, (cfg.countermode & 0x02)) == 0) + ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize); + else + lora_enqueuedata(&SendBuffer); // re-enqueue unsent message } - // reschedule job every 0,5 - 1 sec. including a bit of random to prevent - // systematic collisions - os_setTimedCallback(job, os_getTime() + 500 + ms2osticks(random(500)), - lora_send); } esp_err_t lora_stack_init() { @@ -438,15 +443,17 @@ esp_err_t lora_stack_init() { void lora_enqueuedata(MessageBuffer_t *message) { // enqueue message in LORA send queue - BaseType_t ret; + BaseType_t ret = pdFALSE; MessageBuffer_t DummyBuffer; sendprio_t prio = message->MessagePrio; switch (prio) { case prio_high: - // clear space in queue if full, then fallthrough to normal - if (uxQueueSpacesAvailable(LoraSendQueue) == 0) + // clear some space in queue if full, then fallthrough to prio_normal + if (uxQueueSpacesAvailable(LoraSendQueue) == 0) { xQueueReceive(LoraSendQueue, &DummyBuffer, (TickType_t)0); + ESP_LOGW(TAG, "LORA sendqueue purged, data is lost"); + } case prio_normal: ret = xQueueSendToFront(LoraSendQueue, (void *)message, (TickType_t)0); break; @@ -455,7 +462,9 @@ void lora_enqueuedata(MessageBuffer_t *message) { ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); break; } - if (ret != pdTRUE) + if (ret == pdTRUE) + ESP_LOGD(TAG, "LORA sendqueue data enqueued"); + else ESP_LOGW(TAG, "LORA sendqueue is full"); } diff --git a/src/senddata.cpp b/src/senddata.cpp index 95577b24..65d656c5 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -40,7 +40,7 @@ void SendPayload(uint8_t port, sendprio_t prio) { default: SendBuffer.MessagePort = port; } - memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize()); + memcpy(SendBuffer.Message, payload.getBuffer(), SendBuffer.MessageSize); // enqueue message in device's send queues #if (HAS_LORA) From 9e42de67bdca5e5094fd1694dd3d18afb990297b Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Wed, 28 Aug 2019 22:04:24 +0200 Subject: [PATCH 6/6] v1.7.974 --- platformio.ini | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/platformio.ini b/platformio.ini index 33c863ce..75905821 100644 --- a/platformio.ini +++ b/platformio.ini @@ -42,7 +42,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 = 1.7.93 +release_version = 1.7.974 ; 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 @@ -61,7 +61,7 @@ lib_deps_display = lib_deps_matrix_display = https://github.com/Seeed-Studio/Ultrathin_LED_Matrix.git lib_deps_rgbled = - SmartLeds@>=1.1.5 + SmartLeds@>=1.1.6 lib_deps_gps = 1655@>=1.0.2 ;TinyGPSPlus by Mikal Hart lib_deps_sensors = @@ -114,7 +114,7 @@ upload_protocol = custom upload_protocol = esptool [env:dev] -upload_protocol = esptool +upload_protocol = custom build_type = debug platform = https://github.com/platformio/platform-espressif32.git#develop platform_packages =