From df8083dba67838959d9f02368e10d235b56fc93b Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 29 Dec 2020 14:25:59 +0100 Subject: [PATCH 1/7] switch off DIO IRQs (MCCI LMIC PR #556) --- src/lmic_config.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lmic_config.h b/src/lmic_config.h index 992e4a18f..d5dcfdbe4 100644 --- a/src/lmic_config.h +++ b/src/lmic_config.h @@ -18,9 +18,10 @@ // use interrupts only if LORA_IRQ and LORA_DIO are connected to interrupt // capable and separate GPIO pins on your board, if not don't enable -#if (LORA_IRQ) != (LORA_IO1) -#define LMIC_USE_INTERRUPTS 1 -#endif +// note: this feature can't be used on ESP32 unless PR #556 of MCCI LMIC was merged +//#if (LORA_IRQ) != (LORA_IO1) +//#define LMIC_USE_INTERRUPTS 1 +//#endif // avoid lmic warning if we don't configure radio in case we haven't one #if !(defined(CFG_sx1272_radio) || defined(CFG_sx1276_radio)) From 0209915877ae495f4245df1a9632ddce42f81dd2 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 29 Dec 2020 20:48:02 +0100 Subject: [PATCH 2/7] macsniff.cpp code sanitization --- src/macsniff.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/macsniff.cpp b/src/macsniff.cpp index d47df342a..e50440399 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -102,6 +102,10 @@ void IRAM_ATTR mac_add(uint8_t *paddr, int8_t rssi, snifftype_t sniff_type) { 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)", @@ -124,8 +128,6 @@ uint16_t mac_analyze(MacBuffer_t MacBuffer) { } }; - uint32_t *mac; // pointer to shortened 4 byte MAC - // 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. @@ -136,12 +138,12 @@ uint16_t mac_analyze(MacBuffer_t MacBuffer) { // https://en.wikipedia.org/wiki/MAC_Address_Anonymization // reversed 4 byte MAC added to current salt - const uint32_t saltedmac = *mac + 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 - const uint16_t hashedmac = hash((const char *)&saltedmac, 4); + hashedmac = hash((const char *)&saltedmac, 4); auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique bool added = From e882ad625e021f18ffa94cd861a03a22e0c3a27a Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 29 Dec 2020 20:48:32 +0100 Subject: [PATCH 3/7] mqttclient.cpp: added base64 encoding --- src/mqttclient.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 6821a6292..4e43244bc 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -1,6 +1,7 @@ #ifdef HAS_MQTT #include "mqttclient.h" +#include static const char TAG[] = __FILE__; @@ -95,14 +96,15 @@ void mqtt_client_task(void *param) { MQTT_KEEPALIVE * 1000 / portTICK_PERIOD_MS) != pdTRUE) continue; - // prepare data to send - char buffer[PAYLOAD_BUFFER_SIZE + 3]; - snprintf(buffer, msg.MessageSize + 3, "%u/%s", msg.MessagePort, - msg.Message); + // prepare mqtt topic + char topic[16]; + snprintf(topic, 16, "%s/%u", MQTT_OUTTOPIC, msg.MessagePort); - // send data to mqtt server and delete sent item from queue - if (mqttClient.publish(MQTT_OUTTOPIC, buffer)) { - ESP_LOGI(TAG, "%d byte(s) sent to MQTT server", msg.MessageSize + 2); + // send base64 encoded message to mqtt server and delete it from queue + if (mqttClient.publish(topic, + base64::encode(msg.Message, msg.MessageSize))) { + ESP_LOGD(TAG, "%s/%s sent to MQTT server", topic, + base64::encode(msg.Message, msg.MessageSize)); xQueueReceive(MQTTSendQueue, &msg, (TickType_t)0); } else ESP_LOGD(TAG, "Couldn't sent message to MQTT server"); From bf5a376926ecc0091abb6f963b546c4496c89272 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 30 Dec 2020 22:07:50 +0100 Subject: [PATCH 4/7] fix issue #697 --- include/reset.h | 3 ++- src/reset.cpp | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/reset.h b/include/reset.h index af9d6c50b..ddf3dfcb6 100644 --- a/include/reset.h +++ b/include/reset.h @@ -11,7 +11,8 @@ void do_reset(bool warmstart); void do_after_reset(void); -void enter_deepsleep(const uint64_t wakeup_sec, const gpio_num_t wakeup_gpio); +void enter_deepsleep(const uint64_t wakeup_sec = 60, + const gpio_num_t wakeup_gpio = GPIO_NUM_MAX); unsigned long long uptime(void); #endif // _RESET_H \ No newline at end of file diff --git a/src/reset.cpp b/src/reset.cpp index 8332589d0..3bd2a619d 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -81,8 +81,7 @@ void do_after_reset(void) { runmode[RTC_runmode]); } -void enter_deepsleep(const uint64_t wakeup_sec = 60, - gpio_num_t wakeup_gpio = GPIO_NUM_MAX) { +void enter_deepsleep(const uint64_t wakeup_sec, gpio_num_t wakeup_gpio) { ESP_LOGI(TAG, "Preparing to sleep..."); From 5d8cec547cd2543cc13bb26949e19ee29423a205 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 30 Dec 2020 22:09:06 +0100 Subject: [PATCH 5/7] lopy4.h: remove LORA_RST, now in pins_arduino.h --- src/hal/lopy4.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hal/lopy4.h b/src/hal/lopy4.h index 8072dfd06..fd99a7d74 100644 --- a/src/hal/lopy4.h +++ b/src/hal/lopy4.h @@ -10,7 +10,6 @@ // Hardware related definitions for Pycom LoPy4 Board #define HAS_LORA 1 // comment out if device shall not send data via LoRa -#define LORA_RST LMIC_UNUSED_PIN // reset pin of lora chip is not wired von LoPy4 //#defin HAS_SPI 1 // comment out if device shall not send data via SPI // pin definitions for local wired SPI slave interface From 98f83d96794147b886a231bbc506d4b3eb4a9fe8 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 30 Dec 2020 22:25:37 +0100 Subject: [PATCH 6/7] MQTT client rcommand completion --- README.md | 4 +++- include/mqttclient.h | 4 ++-- src/mqttclient.cpp | 43 ++++++++++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 79634f3a5..9dc8660de 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ LoLin32lite + [LoraNode32-Lite shield](https://github.com/hallard/LoLin32-Lite-L - Generic ESP32 Depending on board hardware following features are supported: +- LoRaWAN communication, supporting various payload formats (see enclosed .js converters) +- MQTT communication via TCP/IP and Ethernet interface (note: payload transmitted over MQTT will be base64 encoded) +- SPI serial communication to a local host - LED (shows power & status) - OLED Display (shows detailed status) - RGB LED (shows colorized status) @@ -62,7 +65,6 @@ Depending on board hardware following features are supported: - Switch external power / battery - LED Matrix display (similar to [this 64x16 model](https://www.instructables.com/id/64x16-RED-LED-Marquee/), can be ordered on [Aliexpress](https://www.aliexpress.com/item/P3-75-dot-matrix-led-module-3-75mm-high-clear-top1-for-text-display-304-60mm/32616683948.html)) - SD-card (see section SD-card here) for logging pax data -- Ethernet interface for MQTT communication via TCP/IP Target platform must be selected in `platformio.ini`.
Hardware dependent settings (pinout etc.) are stored in board files in /hal directory. If you want to use a ESP32 board which is not yet supported, use hal file generic.h and tailor pin mappings to your needs. Pull requests for new boards welcome.
diff --git a/include/mqttclient.h b/include/mqttclient.h index a4fe4ac18..1c797a01c 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -5,6 +5,7 @@ #include "rcommand.h" #include #include +#include #ifndef MQTT_CLIENTNAME #define MQTT_CLIENTNAME clientId @@ -17,8 +18,7 @@ uint32_t mqtt_queuewaiting(void); void mqtt_queuereset(void); void mqtt_client_task(void *param); int mqtt_connect(const char *my_host, const uint16_t my_port); -void mqtt_callback(MQTTClient *client, char topic[], char payload[], - int length); +void mqtt_callback(MQTTClient *client, char *topic, char *payload, int length); void NetworkEvent(WiFiEvent_t event); esp_err_t mqtt_init(void); void mqtt_deinit(void); diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 4e43244bc..b890c920a 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -1,7 +1,6 @@ #ifdef HAS_MQTT #include "mqttclient.h" -#include static const char TAG[] = __FILE__; @@ -99,12 +98,20 @@ void mqtt_client_task(void *param) { // prepare mqtt topic char topic[16]; snprintf(topic, 16, "%s/%u", MQTT_OUTTOPIC, msg.MessagePort); + size_t out_len = 0; - // send base64 encoded message to mqtt server and delete it from queue - if (mqttClient.publish(topic, - base64::encode(msg.Message, msg.MessageSize))) { - ESP_LOGD(TAG, "%s/%s sent to MQTT server", topic, - base64::encode(msg.Message, msg.MessageSize)); + // get length of base64 encoded message + mbedtls_base64_encode(NULL, 0, &out_len, (unsigned char *)msg.Message, + msg.MessageSize); + + // base64 encode the message + unsigned char encoded[out_len]; + mbedtls_base64_encode(encoded, out_len, &out_len, + (unsigned char *)msg.Message, msg.MessageSize); + + // send encoded message to mqtt server and delete it from queue + if (mqttClient.publish(topic, (const char *)encoded, out_len)) { + ESP_LOGD(TAG, "%u bytes sent to MQTT server", out_len); xQueueReceive(MQTTSendQueue, &msg, (TickType_t)0); } else ESP_LOGD(TAG, "Couldn't sent message to MQTT server"); @@ -117,19 +124,29 @@ void mqtt_client_task(void *param) { } // while (1) } +// process incoming MQTT messages +void mqtt_callback(MQTTClient *client, char *topic, char *payload, int length) { + if (strcmp(topic, MQTT_INTOPIC) == 0) { + + // get length of base64 encoded message + size_t out_len = 0; + mbedtls_base64_decode(NULL, 0, &out_len, (unsigned char *)payload, length); + + // decode the base64 message + unsigned char decoded[out_len]; + mbedtls_base64_decode(decoded, out_len, &out_len, (unsigned char *)payload, + length); + + rcommand(decoded, out_len); + } +} + // enqueue outgoing messages in MQTT send queue void mqtt_enqueuedata(MessageBuffer_t *message) { if (xQueueSendToBack(MQTTSendQueue, (void *)message, (TickType_t)0) != pdTRUE) ESP_LOGW(TAG, "MQTT sendqueue is full"); } -// process incoming MQTT messages -void mqtt_callback(MQTTClient *client, char topic[], char payload[], - int length) { - if (strcmp(topic, MQTT_INTOPIC) == 0) - rcommand((const uint8_t *)payload, (const uint8_t)length); -} - void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); } uint32_t mqtt_queuewaiting(void) { From 4093a39e09dab214713bfbf38fcfd960a6e3d7cc Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 30 Dec 2020 22:28:35 +0100 Subject: [PATCH 7/7] bump to v2.1.1 --- platformio_orig.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio_orig.ini b/platformio_orig.ini index 27e0e6889..a6cf1ac77 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -47,7 +47,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.1.0 +release_version = 2.1.1 ; 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