diff --git a/README.md b/README.md index 79634f3a..9dc8660d 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 a4fe4ac1..1c797a01 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/include/reset.h b/include/reset.h index af9d6c50..ddf3dfcb 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/platformio_orig.ini b/platformio_orig.ini index 27e0e688..a6cf1ac7 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 diff --git a/src/hal/lopy4.h b/src/hal/lopy4.h index 8072dfd0..fd99a7d7 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 diff --git a/src/lmic_config.h b/src/lmic_config.h index 992e4a18..d5dcfdbe 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)) diff --git a/src/macsniff.cpp b/src/macsniff.cpp index d47df342..e5044039 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 = diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 6821a629..b890c920 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -95,14 +95,23 @@ 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); + size_t out_len = 0; - // 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); + // 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"); @@ -115,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) { diff --git a/src/reset.cpp b/src/reset.cpp index 8332589d..3bd2a619 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...");