This commit is contained in:
Klaus K Wilting 2020-09-02 11:23:44 +02:00
commit a87ef165c3
18 changed files with 171 additions and 145 deletions

View File

@ -86,6 +86,8 @@ with open(otakeyfile) as myfile:
key, value = line.partition("=")[::2]
mykeys[key.strip()] = str(value).strip()
# usage of bintray: see https://github.com/r0oland/bintray-secure-ota
# get bintray user credentials from ota key file
user = mykeys["BINTRAY_USER"]
repository = mykeys["BINTRAY_REPO"]

View File

@ -124,7 +124,7 @@ extern configData_t cfg; // current device configuration
extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer
extern uint8_t volatile channel; // wifi channel rotation counter
extern uint8_t batt_level; // display value
extern uint16_t volatile macs_total, macs_wifi, macs_ble; // display values
extern uint16_t volatile macs_wifi, macs_ble; // display values
extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC
extern timesource_t timeSource;
extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ;

View File

@ -11,6 +11,7 @@
#define BME_IRQ 0x080
#define MATRIX_DISPLAY_IRQ 0x100
#define PMU_IRQ 0x200
#define MQTT_IRQ 0x400
#include "globals.h"
#include "button.h"
@ -20,10 +21,12 @@
#include "bmesensor.h"
#include "power.h"
#include "ledmatrixdisplay.h"
#include "mqttclient.h"
void irqHandler(void *pvParameters);
void mask_user_IRQ();
void unmask_user_IRQ();
void doIRQ(int irq);
#ifdef HAS_DISPLAY
void IRAM_ATTR DisplayIRQ();
@ -41,5 +44,4 @@ void IRAM_ATTR ButtonIRQ();
void IRAM_ATTR PMUIRQ();
#endif
#endif

View File

@ -3,23 +3,35 @@
#include "globals.h"
#include "rcommand.h"
#include <MQTT.h>
#include <ETH.h>
#include <PubSubClient.h>
#define MQTT_INTOPIC "paxcounter_in/"
#define MQTT_OUTTOPIC "paxcounter_out/"
#define MQTT_ETHERNET 0 // set to 0 to run on Wifi
#define MQTT_INTOPIC "paxin"
#define MQTT_OUTTOPIC "paxout"
#define MQTT_PORT 1883
#define MQTT_SERVER "broker.hivemq.com"
#define MQTT_RETRYSEC 10 // retry reconnect every 10 seconds
#define MQTT_SERVER "broker.shiftr.io"
//#define MQTT_CLIENTNAME "arduino"
#define MQTT_USER "try"
#define MQTT_PASSWD "try"
#define MQTT_RETRYSEC 20 // retry reconnect every 20 seconds
#define MQTT_KEEPALIVE 10 // keep alive interval in seconds
#define MQTT_TIMEOUT 1000 // timeout for all mqtt commands in milliseconds
#ifndef MQTT_CLIENTNAME
#define MQTT_CLIENTNAME clientId.c_str()
#endif
extern TaskHandle_t mqttTask;
extern PubSubClient mqttClient;
void mqtt_enqueuedata(MessageBuffer_t *message);
void mqtt_queuereset(void);
void mqtt_irq(void);
void mqtt_loop(void);
void mqtt_client_task(void *param);
int mqtt_connect(const char *my_host, const uint16_t my_port);
void mqtt_callback(char *topic, byte *payload, unsigned int length);
void mqtt_callback(MQTTClient *client, char topic[], char payload[],
int length);
void NetworkEvent(WiFiEvent_t event);
esp_err_t mqtt_init(void);

View File

@ -37,11 +37,11 @@ halfile = generic.h
[platformio]
; upload firmware to board with usb cable
default_envs = usb
;default_envs = usb
; upload firmware to a jfrog bintray repository
;default_envs = ota
; use latest versions of libraries
;default_envs = dev
default_envs = dev
description = Paxcounter is a device for metering passenger flows in realtime. It counts how many mobile devices are around.
[common]
@ -49,20 +49,19 @@ description = Paxcounter is a device for metering passenger flows in realtime. I
release_version = 1.9.996
; 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
debug_level = 4
extra_scripts = pre:build.py
otakeyfile = ota.conf
lorakeyfile = loraconf.h
lmicconfigfile = lmic_config.h
platform_espressif32 = espressif32@1.12.2
platform_espressif32 = espressif32@1.12.4
monitor_speed = 115200
upload_speed = 115200 ; set by build.py and taken from hal file
display_library = ; set by build.py and taken from hal file
lib_deps_lora =
MCCI LoRaWAN LMIC library@3.2.0 ; MCCI LMIC by Terrill Moore
lib_deps_display =
;OneBitDisplay@>1.4.0
https://github.com/bitbank2/OneBitDisplay.git
OneBitDisplay@1.5.0
QRCode@0.0.1
BitBang_I2C@2.1.1
TFT_eSPI@>=2.2.8
@ -73,7 +72,7 @@ lib_deps_rgbled =
lib_deps_gps =
1655@>=1.0.2 ; #1655 TinyGPSPlus by Mikal Hart
lib_deps_sensors =
Adafruit Unified Sensor@>=1.1.3
Adafruit Unified Sensor@>=1.1.4
Adafruit BME280 Library@>=2.0.2
Adafruit BMP085 Library@>=1.1.0
BSEC Software Library@1.5.1474
@ -81,11 +80,11 @@ lib_deps_sensors =
lib_deps_basic =
ArduinoJson@<6
76@>=1.2.4 ; #76 Timezone by Jack Christensen
274@>=2.3.4 ; #274 RTC by Michael Miller
274@>=2.3.5 ; #274 RTC by Michael Miller
SimpleButton
AXP202X_Library@>=1.1.1 ; AXP202 PMU lib by Lewis He
AXP202X_Library@>=1.1.2 ; AXP202 PMU lib by Lewis He
esp32-micro-sdcard
PubSubClient@>=2.8.0
MQTT@>=2.4.7 ; MQTT client maintained by Joel Gaehwiler
lib_deps_all =
${common.lib_deps_basic}
${common.lib_deps_lora}

View File

@ -55,7 +55,6 @@ void doHousekeeping() {
#ifdef HAS_MQTT
ESP_LOGD(TAG, "MQTTloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(mqttTask), eTaskGetState(mqttTask));
mqttClient.loop();
#endif
#if (defined HAS_DCF77 || defined HAS_IF482)
@ -152,7 +151,6 @@ uint32_t getFreeRAM() {
void reset_counters() {
#if ((WIFICOUNTER) || (BLECOUNTER))
macs.clear(); // clear all macs container
macs_total = 0; // reset all counters
macs_wifi = 0;
macs_ble = 0;
#ifdef HAS_DISPLAY

View File

@ -2,6 +2,9 @@
// upload_speed 921600
// board m5stack-fire
// note use of GPIO16/17
// https://www.bjoerns-techblog.de/2019/03/m5stack-fire-eine-uebersicht/
#ifndef _M5FIRE_H
#define _M5FIRE_H

View File

@ -8,7 +8,7 @@
#include <stdint.h>
// enable only if you want to store a local paxcount table on the device
//#define HAS_SDCARD 2 // this board has a SDMMC card-reader/writer
#define HAS_SDCARD 2 // this board has a SDMMC card-reader/writer
// enable only if you want to send paxcount via ethernet port to mqtt server
#define HAS_MQTT 1 // use MQTT on ethernet interface

View File

@ -9,7 +9,7 @@
/*
Hardware related definitions for TTGO T-Beam board
for T-Beam versions T22_V10 + T22_V11
for T-Beam version T22_V10 + T22_V11
pinouts taken from https://github.com/lewisxhe/TTGO-T-Beam
/// Button functions: ///
@ -61,7 +61,7 @@ Reset -> reset device
/*
// T-Beam V10 has on board power management by AXP192 PMU chip:
// T-Beam V10/V11 has on board power management by AXP192 PMU chip:
//
// DCDC1 0.7-3.5V @ 1200mA -> OLED
// DCDC3 0.7-3.5V @ 700mA -> ESP32 (keep this on!)

View File

@ -69,6 +69,14 @@ void irqHandler(void *pvParameters) {
}
#endif
// MQTT loop due?
#if (HAS_MQTT)
if (InterruptStatus & MQTT_IRQ) {
mqtt_loop();
InterruptStatus &= ~MQTT_IRQ;
}
#endif
// are cyclic tasks due?
if (InterruptStatus & CYCLIC_IRQ) {
doHousekeeping();
@ -91,53 +99,30 @@ void irqHandler(void *pvParameters) {
} // for
} // irqHandler()
// esp32 hardware timer triggered interrupt service routines
// timer triggered interrupt service routines
// they notify the irq handler task
#ifdef HAS_DISPLAY
void IRAM_ATTR DisplayIRQ() {
void IRAM_ATTR doIRQ(int irq) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(irqHandlerTask, DISPLAY_IRQ, eSetBits,
&xHigherPriorityTaskWoken);
xTaskNotifyFromISR(irqHandlerTask, irq, eSetBits, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
#ifdef HAS_DISPLAY
void IRAM_ATTR DisplayIRQ() { doIRQ(DISPLAY_IRQ); }
#endif
#ifdef HAS_MATRIX_DISPLAY
void IRAM_ATTR MatrixDisplayIRQ() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(irqHandlerTask, MATRIX_DISPLAY_IRQ, eSetBits,
&xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
void IRAM_ATTR MatrixDisplayIRQ() { doIRQ(MATRIX_DISPLAY_IRQ); }
#endif
#ifdef HAS_BUTTON
void IRAM_ATTR ButtonIRQ() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(irqHandlerTask, BUTTON_IRQ, eSetBits,
&xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
void IRAM_ATTR ButtonIRQ() { doIRQ(BUTTON_IRQ); }
#endif
#ifdef HAS_PMU
void IRAM_ATTR PMUIRQ() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(irqHandlerTask, PMU_IRQ, eSetBits,
&xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
void IRAM_ATTR PMUIRQ() { doIRQ(PMU_IRQ); }
#endif
void mask_user_IRQ() { xTaskNotify(irqHandlerTask, MASK_IRQ, eSetBits); }

View File

@ -80,7 +80,7 @@ static const u4_t DEVADDR =
0x00000000; // <-- Change this address for every node!
// set additional ABP parameters in loraconf_abp.cpp
void setABPParamaters();
void setABPParameters();
#endif

View File

@ -13,7 +13,7 @@
*
*********************************************************************/
void setABPParamaters() {
void setABPParameters() {
/** **************************************************************
* ************************************************************* */

View File

@ -1,4 +1,5 @@
// Basic Config
#if (HAS_LORA)
#include "lorawan.h"
@ -319,7 +320,7 @@ esp_err_t lora_stack_init(bool do_join) {
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession(NETID, DEVADDR, nwkskey, appskey);
// These parameters are defined as macro in loraconf.h
setABPParamaters();
setABPParameters();
#else
// Start join procedure if not already joined,
// lora_setupForNetwork(true) is called by eventhandler when joined
@ -581,6 +582,7 @@ const char *getCrName(rps_t rps) {
#if (VERBOSE)
// decode LORAWAN MAC message
// see https://github.com/mcci-catena/arduino-lmic/blob/master/doc/LoRaWAN-at-a-glance.pdf
void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, bool is_down) {
if (!cmdlen)

View File

@ -81,8 +81,7 @@ configData_t cfg; // struct holds current device configuration
char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message
uint8_t volatile channel = 0; // channel rotation counter
uint8_t batt_level = 0; // display value
uint16_t volatile macs_total = 0, macs_wifi = 0,
macs_ble = 0; // globals for display
uint16_t volatile macs_wifi = 0, macs_ble = 0; // globals for display
hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL;

View File

@ -7,43 +7,47 @@ static const char TAG[] = __FILE__;
QueueHandle_t MQTTSendQueue;
TaskHandle_t mqttTask;
WiFiClient EthClient;
PubSubClient mqttClient(EthClient);
Ticker mqttTimer;
WiFiClient netClient;
MQTTClient mqttClient;
void NetworkEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_ETH_START:
ESP_LOGI(TAG, "Ethernet link layer started");
ETH.setHostname(ETH.macAddress().c_str());
break;
case SYSTEM_EVENT_ETH_CONNECTED:
ESP_LOGI(TAG, "Network link connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
ESP_LOGI(TAG, "ETH MAC: %s", ETH.macAddress().c_str());
ESP_LOGI(TAG, "IPv4: %s", ETH.localIP().toString().c_str());
ESP_LOGI(TAG, "Link Speed: %d Mbps %s", ETH.linkSpeed(),
ETH.fullDuplex() ? "full duplex" : "half duplex");
mqtt_connect(MQTT_SERVER, MQTT_PORT);
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
ESP_LOGI(TAG, "Network link disconnected");
break;
case SYSTEM_EVENT_ETH_STOP:
ESP_LOGI(TAG, "Ethernet link layer stopped");
break;
default:
break;
esp_err_t mqtt_init(void) {
// setup network connection
WiFi.onEvent(NetworkEvent);
ETH.begin();
//WiFi.mode(WIFI_STA);
//WiFi.begin("SSID", "PASSWORD");
// setup mqtt client
mqttClient.begin(MQTT_SERVER, MQTT_PORT, netClient);
mqttClient.onMessageAdvanced(mqtt_callback);
assert(SEND_QUEUE_SIZE);
MQTTSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t));
if (MQTTSendQueue == 0) {
ESP_LOGE(TAG, "Could not create MQTT send queue. Aborting.");
return ESP_FAIL;
}
ESP_LOGI(TAG, "MQTT send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
ESP_LOGI(TAG, "Starting MQTTloop...");
mqttTimer.attach(MQTT_KEEPALIVE, mqtt_irq);
xTaskCreate(mqtt_client_task, "mqttloop", 4096, (void *)NULL, 1, &mqttTask);
return ESP_OK;
}
int mqtt_connect(const char *my_host, const uint16_t my_port) {
IPAddress mqtt_server_ip;
static String clientId = "paxcounter-" + ETH.macAddress();
ESP_LOGI(TAG, "MQTT name is %s", clientId.c_str());
// static String clientId = "paxcounter-" + ETH.macAddress();
static String clientId = "paxcounter-" + String(random(0xffff), HEX);
// resolve server
ESP_LOGI(TAG, "MQTT name is %s", MQTT_CLIENTNAME);
// resolve server host name
if (WiFi.hostByName(my_host, mqtt_server_ip)) {
ESP_LOGI(TAG, "Attempting to connect to %s [%s]", my_host,
mqtt_server_ip.toString().c_str());
@ -52,24 +56,53 @@ int mqtt_connect(const char *my_host, const uint16_t my_port) {
return -1;
}
// attempt to connect to MQTT server
if (EthClient.connect(mqtt_server_ip, my_port, HOMECYCLE * 2 * 1000)) {
mqttClient.setServer(mqtt_server_ip, my_port);
mqttClient.setKeepAlive(HOMECYCLE * 2);
mqttClient.setCallback(mqtt_callback);
if (mqttClient.connect(clientId.c_str())) {
if (mqttClient.connect(MQTT_CLIENTNAME, MQTT_USER, MQTT_PASSWD)) {
ESP_LOGI(TAG, "MQTT server connected, subscribing...");
mqttClient.publish(MQTT_OUTTOPIC, clientId.c_str());
mqttClient.publish(MQTT_OUTTOPIC, MQTT_CLIENTNAME);
// Clear retained messages that may have been published earlier on topic
mqttClient.publish(MQTT_INTOPIC, "", true, 1);
mqttClient.subscribe(MQTT_INTOPIC);
ESP_LOGI(TAG, "MQTT topic subscribed");
} else {
ESP_LOGD(TAG, "MQTT last_error = %d / rc = %d", mqttClient.lastError(),
mqttClient.returnCode());
ESP_LOGW(TAG, "MQTT server not responding, retrying later");
return -1;
}
} else {
ESP_LOGW(TAG, "MQTT server not connected, retrying later");
return -1;
}
void NetworkEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_ETH_START:
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "Network link layer started");
// ETH.setHostname(ETH.macAddress().c_str());
break;
case SYSTEM_EVENT_ETH_STOP:
case SYSTEM_EVENT_STA_STOP:
ESP_LOGI(TAG, "Network link layer stopped");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "Network link connected");
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Network link disconnected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
ESP_LOGI(TAG, "IP: %s", ETH.localIP().toString().c_str());
ESP_LOGI(TAG, "Link Speed: %d Mbps %s", ETH.linkSpeed(),
ETH.fullDuplex() ? "full duplex" : "half duplex");
mqtt_connect(MQTT_SERVER, MQTT_PORT);
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "IP: %s", WiFi.localIP().toString().c_str());
mqtt_connect(MQTT_SERVER, MQTT_PORT);
break;
default:
break;
}
}
@ -87,46 +120,33 @@ void mqtt_client_task(void *param) {
// send data to mqtt server, if we are connected
if (mqttClient.connected()) {
mqttClient.beginPublish(MQTT_OUTTOPIC, msg.MessageSize + 2, false);
mqttClient.write(msg.MessagePort);
mqttClient.write('/');
mqttClient.write(msg.Message, msg.MessageSize);
if (mqttClient.endPublish()) {
ESP_LOGI(TAG, "%d byte(s) sent to MQTT", msg.MessageSize + 2);
continue; // while(1)
} else
goto reconnect;
char buffer[PAYLOAD_BUFFER_SIZE + 3];
snprintf(buffer, msg.MessageSize + 3, "%s/%s", msg.MessagePort,
msg.Message);
if (mqttClient.publish(MQTT_OUTTOPIC, buffer)) {
ESP_LOGI(TAG, "%d byte(s) sent to MQTT server", msg.MessageSize + 2);
continue;
} else {
mqtt_enqueuedata(&msg); // postpone the undelivered message
ESP_LOGD(TAG,
"Couldn't sent message to MQTT server, message postponed");
}
} else {
// attempt to reconnect to MQTT server
reconnect:
ESP_LOGD(TAG, "MQTT client reconnecting...");
ESP_LOGD(TAG, "MQTT last_error = %d / rc = %d", mqttClient.lastError(),
mqttClient.returnCode());
mqtt_enqueuedata(&msg); // postpone the undelivered message
delay(MQTT_RETRYSEC * 1000);
mqtt_connect(MQTT_SERVER, MQTT_PORT);
}
} // while(1)
}
esp_err_t mqtt_init(void) {
assert(SEND_QUEUE_SIZE);
MQTTSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t));
if (MQTTSendQueue == 0) {
ESP_LOGE(TAG, "Could not create MQTT send queue. Aborting.");
return ESP_FAIL;
}
ESP_LOGI(TAG, "MQTT send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
ESP_LOGI(TAG, "Starting MQTTloop...");
xTaskCreate(mqtt_client_task, "mqttloop", 4096, (void *)NULL, 2, &mqttTask);
WiFi.onEvent(NetworkEvent);
ETH.begin();
return ESP_OK;
}
void mqtt_enqueuedata(MessageBuffer_t *message) {
// enqueue message in MQTT send queue
BaseType_t ret;
@ -150,14 +170,19 @@ void mqtt_enqueuedata(MessageBuffer_t *message) {
ESP_LOGW(TAG, "MQTT sendqueue is full");
}
void mqtt_callback(char *topic, byte *payload, unsigned int length) {
String s = "";
for (int i = 0; i < length; i++)
s += (char)payload[i];
ESP_LOGD(TAG, "MQTT: Received %u byte(s) of payload [%s]", length, s);
// rcommand(payload, length);
void mqtt_callback(MQTTClient *client, char topic[], char payload[],
int length) {
if (topic == MQTT_INTOPIC)
rcommand((const uint8_t *)payload, (const uint8_t)length);
}
void mqtt_loop(void) {
if (!mqttClient.loop())
ESP_LOGD(TAG, "MQTT last_error = %d / rc = %d", mqttClient.lastError(),
mqttClient.returnCode());
}
void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); }
void mqtt_irq(void) { xTaskNotify(irqHandlerTask, MQTT_IRQ, eSetBits); }
#endif // HAS_MQTT

View File

@ -22,6 +22,7 @@
using namespace std;
const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE);
// usage of bintray: see https://github.com/r0oland/bintray-secure-ota
// Connection port (HTTPS)
const int port = 443;

View File

@ -13,7 +13,7 @@ uint8_t rtc_init(void) {
if (I2C_MUTEX_LOCK()) { // block i2c bus access
Wire.begin(HAS_RTC);
Rtc.Begin();
Rtc.Begin(MY_DISPLAY_SDA, MY_DISPLAY_SCL);
// configure RTC chip
Rtc.Enable32kHzPin(false);

View File

@ -1,6 +1,5 @@
#include "timekeeper.h"
/*
#if !(HAS_LORA)
#if (TIME_SYNC_LORASERVER)
#error TIME_SYNC_LORASERVER defined, but device has no LORA configured
@ -8,7 +7,6 @@
#error TIME_SYNC_LORAWAN defined, but device has no LORA configured
#endif
#endif
*/
// Local logging tag
static const char TAG[] = __FILE__;