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/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/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/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/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 = 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/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; diff --git a/src/lorawan.cpp b/src/lorawan.cpp index e82ac1f5..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,27 +396,22 @@ 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"); - } + + // 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() { @@ -441,15 +441,19 @@ 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; + 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; @@ -458,7 +462,9 @@ void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { 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/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}}; diff --git a/src/senddata.cpp b/src/senddata.cpp index fd5df963..65d656c5 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 @@ -38,14 +40,14 @@ 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) - 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 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