Skip to content

Commit

Permalink
mqtt: use static json docs, fixes stack overflow in timer
Browse files Browse the repository at this point in the history
  • Loading branch information
vooon committed Nov 3, 2020
1 parent 9b90d9b commit db4d445
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ lib_deps =
https://github.com/bblanchon/ArduinoJson.git#6.x
AsyncTCP
;https://github.com/me-no-dev/AsyncTCP.git#idf-update
AsyncMqttClient
ESP Async WebServer
AsyncMqttClient-esphome
ESPAsyncWebServer-esphome
;ESP32 BLE Arduino ;-> In framework BLE
;BLE ;-> older
;U8g2 ; choose one gfx lib
Expand Down
30 changes: 17 additions & 13 deletions src/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

unsigned int ble::raw_advertise_counter = 0;

static BLEScan *pScan;


static inline std::string to_hex(const std::string &str)
{
Expand Down Expand Up @@ -71,7 +69,7 @@ class ElehantMeterAdvertismentB0 {
class MyAdvertisedDeviceCallbacls:
public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice dev)
void onResult(BLEAdvertisedDevice dev) override
{
log_d("Got advertisment");

Expand All @@ -93,7 +91,8 @@ class MyAdvertisedDeviceCallbacls:

void send_raw(uint32_t now, unsigned long ts, BLEAdvertisedDevice &dev)
{
DynamicJsonDocument jdoc(1024);
//DynamicJsonDocument jdoc(1024);
jdoc.clear();
auto root = jdoc.to<JsonObject>();

auto addr_type = dev.getAddressType();
Expand Down Expand Up @@ -156,7 +155,8 @@ class MyAdvertisedDeviceCallbacls:

void send_elehant_counter(uint32_t now, unsigned long ts, BLEAdvertisedDevice &dev, ElehantMeterAdvertismentB0 &edata)
{
DynamicJsonDocument jdoc(512);
//DynamicJsonDocument jdoc(512);
jdoc.clear();
auto root = jdoc.to<JsonObject>();

mqtt::json_stamp(root, now, ts);
Expand All @@ -176,10 +176,21 @@ class MyAdvertisedDeviceCallbacls:
mqtt::ble_report_counter(edata.device_num, jdoc);
display::update_counter(now, edata.device_num, edata.counter, dev.getRSSI());
}

private:
StaticJsonDocument<2048> jdoc;
};

static void ble_thd(void *arg)
{
BLEDevice::init("svd-15-mqtt");
auto pScan = BLEDevice::getScan();
pScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacls(), true);
pScan->setActiveScan(false);
pScan->setInterval(100);
pScan->setWindow(99);


log_i("BLE thread started.");

for (;;) {
Expand All @@ -196,12 +207,5 @@ static void ble_thd(void *arg)

void ble::init()
{
BLEDevice::init("svd-15-mqtt");
pScan = BLEDevice::getScan();
pScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacls(), true);
pScan->setActiveScan(false);
pScan->setInterval(100);
pScan->setWindow(99);

xTaskCreatePinnedToCore(ble_thd, "ble", 4096, NULL, 0, NULL, USE_CORE);
xTaskCreatePinnedToCore(ble_thd, "ble", 8192, NULL, 0, NULL, USE_CORE);
}
4 changes: 2 additions & 2 deletions src/influx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static inline void send(InfluxDBBuffer &buf)
log_i("INFLUX: %s", str.c_str());
}

void influx::send_status(DynamicJsonDocument jdoc)
void influx::send_status(const JsonDocument &jdoc)
{
InfluxDBBuffer buf;
auto root = jdoc.as<JsonObject>();
Expand All @@ -41,7 +41,7 @@ void influx::send_status(DynamicJsonDocument jdoc)
send(buf);
}

void influx::send_counter(DynamicJsonDocument jdoc)
void influx::send_counter(const JsonDocument &jdoc)
{
InfluxDBBuffer buf;
auto root = jdoc.as<JsonObject>();
Expand Down
4 changes: 2 additions & 2 deletions src/influx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace influx {

void init();
void send_status(DynamicJsonDocument jdoc);
void send_counter(DynamicJsonDocument jdoc);
void send_status(const JsonDocument &jdoc);
void send_counter(const JsonDocument &jdoc);

}; // namespace influx
19 changes: 12 additions & 7 deletions src/mqtt_commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static AsyncMqttClient m_mqtt_client;
static TimerHandle_t m_mqtt_reconnect_tmr;
static TimerHandle_t m_report_stats_tmr;

static StaticJsonDocument<512> m_jdoc;

using TT = cfg::topic::Type;

namespace MQTT {
Expand All @@ -46,7 +48,7 @@ static void pub_topic(TT type, String topic, const String &value, MQTT::Qos qos=
m_mqtt_client.publish(full_topic.c_str(), qos, retain, value.c_str(), value.length());
}

static void pub_topic(TT type, String topic, const DynamicJsonDocument jdoc, MQTT::Qos qos=MQTT::QOS0, bool retain=false)
static void pub_topic(TT type, String topic, const JsonDocument &jdoc, MQTT::Qos qos=MQTT::QOS0, bool retain=false)
{
String value;

Expand All @@ -73,7 +75,8 @@ void mqtt::json_stamp(JsonObject &root, uint32_t now, uint64_t ts)

static void pub_device_info()
{
DynamicJsonDocument jdoc(512);
//DynamicJsonDocument jdoc(512);
auto &jdoc = m_jdoc; jdoc.clear();
JsonObject root = jdoc.to<JsonObject>();

mqtt::json_stamp(root);
Expand Down Expand Up @@ -106,7 +109,8 @@ static void pub_device_info()

static void pub_stats()
{
DynamicJsonDocument jdoc(256);
//DynamicJsonDocument jdoc(256);
auto &jdoc = m_jdoc; jdoc.clear();
JsonObject root = jdoc.to<JsonObject>();

auto t_f = temprature_sens_read();
Expand Down Expand Up @@ -140,7 +144,8 @@ static void on_mqtt_message(char *c_topic, char *c_payload, AsyncMqttClientMessa
{
std::string topic(c_topic);
std::string payload(c_payload, len);
DynamicJsonDocument jdoc(128);
//StaticJsonDocument<128> jdoc;
auto &jdoc = m_jdoc; jdoc.clear();
JsonObject root = jdoc.to<JsonObject>();

log_i("SUB: %s (qos: %d, dup: %d, retain: %d, sz: %d:%d:%d) => %s",
Expand Down Expand Up @@ -253,7 +258,7 @@ void mqtt::on_wifi_state_change(bool connected_)
void mqtt::ota_report(ota::Result result)
{
bool reboot = false;
DynamicJsonDocument jdoc(128);
StaticJsonDocument<128> jdoc;
JsonObject root = jdoc.to<JsonObject>();

mqtt::json_stamp(root);
Expand Down Expand Up @@ -282,12 +287,12 @@ void mqtt::ota_report(ota::Result result)
}
}

void mqtt::ble_report_raw_adv(DynamicJsonDocument jdoc)
void mqtt::ble_report_raw_adv(const JsonDocument &jdoc)
{
pub_topic(TT::tele, "BLE_ADV_RAW", jdoc, MQTT::QOS0);
}

void mqtt::ble_report_counter(uint32_t device_num, DynamicJsonDocument jdoc)
void mqtt::ble_report_counter(uint32_t device_num, const JsonDocument &jdoc)
{
pub_topic(TT::tele, "SNS/" + String(device_num, 10), jdoc, MQTT::QOS1, true);
influx::send_counter(jdoc);
Expand Down
4 changes: 2 additions & 2 deletions src/mqtt_commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void shedule_report_feed(bool success);
void on_wifi_state_change(bool connected);
void json_stamp(JsonObject &root, uint32_t now=0, uint64_t ts=0);
void ota_report(ota::Result result);
void ble_report_raw_adv(DynamicJsonDocument jdoc);
void ble_report_counter(uint32_t device_num, DynamicJsonDocument jdoc);
void ble_report_raw_adv(const JsonDocument &jdoc);
void ble_report_counter(uint32_t device_num, const JsonDocument &jdoc);
bool is_connected();

};

0 comments on commit db4d445

Please sign in to comment.