Skip to content

Commit

Permalink
Merge pull request #698 from cyberman54/development
Browse files Browse the repository at this point in the history
v2.1.1
  • Loading branch information
cyberman54 authored Dec 30, 2020
2 parents 6279589 + 4093a39 commit 0c210d2
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 30 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`.<br>
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.<br>
Expand Down
4 changes: 2 additions & 2 deletions include/mqttclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "rcommand.h"
#include <MQTT.h>
#include <ETH.h>
#include <mbedtls/base64.h>

#ifndef MQTT_CLIENTNAME
#define MQTT_CLIENTNAME clientId
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion include/reset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion platformio_orig.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/hal/lopy4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/lmic_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 6 additions & 4 deletions src/macsniff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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.
Expand All @@ -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 =
Expand Down
49 changes: 34 additions & 15 deletions src/mqttclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// 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);
// prepare mqtt topic
char topic[16];
snprintf(topic, 16, "%s/%u", MQTT_OUTTOPIC, msg.MessagePort);
size_t out_len = 0;

// 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");
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/reset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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...");

Expand Down

0 comments on commit 0c210d2

Please sign in to comment.