From 629309bde239d845a763a0e917a8ca434b9b84e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 23 Jul 2017 18:04:24 +0200 Subject: [PATCH] Define MQTT mode with a soft setting --- code/espurna/espurna.ino | 26 ++++++------ code/espurna/mqtt.ino | 86 +++++++++++++++++----------------------- code/espurna/relay.ino | 4 +- 3 files changed, 50 insertions(+), 66 deletions(-) diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 75f81fa4..d287869f 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -53,34 +53,34 @@ void heartbeat() { #if (MQTT_REPORT_INTERVAL) - mqttAppend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000); + mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000); #endif #if (MQTT_REPORT_APP) - mqttAppend(MQTT_TOPIC_APP, APP_NAME); + mqttSend(MQTT_TOPIC_APP, APP_NAME); #endif #if (MQTT_REPORT_VERSION) - mqttAppend(MQTT_TOPIC_VERSION, APP_VERSION); + mqttSend(MQTT_TOPIC_VERSION, APP_VERSION); #endif #if (MQTT_REPORT_HOSTNAME) - //mqttAppend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str()); + //mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str()); #endif #if (MQTT_REPORT_IP) - mqttAppend(MQTT_TOPIC_IP, getIP().c_str()); + mqttSend(MQTT_TOPIC_IP, getIP().c_str()); #endif #if (MQTT_REPORT_MAC) - mqttAppend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str()); + mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str()); #endif #if (MQTT_REPORT_RSSI) - mqttAppend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str()); + mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str()); #endif #if (MQTT_REPORT_UPTIME) - mqttAppend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); + mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); #if ENABLE_INFLUXDB influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); #endif #endif #if (MQTT_REPORT_FREEHEAP) - mqttAppend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); + mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); #if ENABLE_INFLUXDB influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); #endif @@ -90,20 +90,18 @@ void heartbeat() { #endif #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE #if (MQTT_REPORT_COLOR) - mqttAppend(MQTT_TOPIC_COLOR, lightColor().c_str()); + mqttSend(MQTT_TOPIC_COLOR, lightColor().c_str()); #endif #endif #if (MQTT_REPORT_VCC) #if ENABLE_ADC_VCC - mqttAppend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str()); + mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str()); #endif #endif #if (MQTT_REPORT_STATUS) - mqttAppend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE); + mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); #endif - mqttSend(); - } void customReset(unsigned char status) { diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 8d105001..d23198fe 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -9,6 +9,7 @@ Copyright (C) 2016-2017 by Xose PĂ©rez #include #include #include +#include const char *mqtt_user = 0; const char *mqtt_pass = 0; @@ -34,10 +35,10 @@ std::vector _mqtt_callbacks; typedef struct { char * topic; - unsigned char index; char * message; } mqtt_message_t; std::vector _mqtt_queue; +Ticker mqttFlushTicker; // ----------------------------------------------------------------------------- // Public API @@ -88,40 +89,23 @@ void mqttSendRaw(const char * topic, const char * message) { } } -void mqttSend() { +void _mqttFlush() { - #if MQTT_USE_JSON + if (_mqtt_queue.size() == 0) return; - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.createObject(); - for (unsigned char i=0; i<_mqtt_queue.size(); i++) { - mqtt_message_t element = _mqtt_queue[i]; - if (element.index < 255) { - String topic = String(element.topic) + String("/") + String(element.index); - root[topic] = element.message; - } else { - root[element.topic] = element.message; - } - } - - root["time"] = NTP.getTimeDateString(); - root["hostname"] = getSetting("hostname", HOSTNAME); + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + for (unsigned char i=0; i<_mqtt_queue.size(); i++) { + mqtt_message_t element = _mqtt_queue[i]; + root[element.topic] = element.message; + } + root["time"] = NTP.getTimeDateString(); + root["hostname"] = getSetting("hostname", HOSTNAME); - String output; - root.printTo(output); - String path = mqttTopic + String(MQTT_TOPIC_JSON); - mqttSendRaw(path.c_str(), output.c_str()); - - #else - String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); - for (unsigned char i=0; i<_mqtt_queue.size(); i++) { - mqtt_message_t element = _mqtt_queue[i]; - String path = mqttTopic + String(element.topic); - if (element.index < 255) path += String ("/") + String(element.index); - path += mqttGetter; - mqttSendRaw(path.c_str(), element.message); - } - #endif + String output; + root.printTo(output); + String path = mqttTopic + String(MQTT_TOPIC_JSON); + mqttSendRaw(path.c_str(), output.c_str()); for (unsigned char i = 0; i < _mqtt_queue.size(); i++) { mqtt_message_t element = _mqtt_queue[i]; @@ -132,29 +116,33 @@ void mqttSend() { } -void mqttSend(const char * topic, const char * message) { - String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); - String path = mqttTopic + String(topic) + mqttGetter; - mqttSendRaw(path.c_str(), message); +void mqttSend(const char * topic, const char * message, bool force) { + bool useJson = force ? false : getSetting("mqttUseJson", MQTT_USE_JSON).toInt() == 1; + if (useJson) { + mqtt_message_t element; + element.topic = strdup(topic); + element.message = strdup(message); + _mqtt_queue.push_back(element); + mqttFlushTicker.once_ms(100, _mqttFlush); + } else { + String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); + String path = mqttTopic + String(topic) + mqttGetter; + mqttSendRaw(path.c_str(), message); + } } -void mqttSend(const char * topic, unsigned int index, const char * message) { - String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); - String path = mqttTopic + String(topic) + String ("/") + String(index) + mqttGetter;; - mqttSendRaw(path.c_str(), message); +void mqttSend(const char * topic, const char * message) { + mqttSend(topic, message, false); } -unsigned int mqttAppend(const char * topic, unsigned int index, const char * message) { - mqtt_message_t element; - element.topic = strdup(topic); - element.index = index; - element.message = strdup(message); - _mqtt_queue.push_back(element); - return _mqtt_queue.size(); +void mqttSend(const char * topic, unsigned int index, const char * message, bool force) { + char buffer[strlen(topic)+5]; + sprintf(buffer, "%s/%d", topic, index); + mqttSend(buffer, message, force); } -unsigned int mqttAppend(const char * topic, const char * message) { - return mqttAppend(topic, 255, message); +void mqttSend(const char * topic, unsigned int index, const char * message) { + mqttSend(topic, index, message, false); } void mqttSubscribeRaw(const char * topic) { diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index f1b24228..b80d54ad 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -381,7 +381,7 @@ void relayDomoticzSetup() { void relayMQTT(unsigned char id) { if (id >= _relays.size()) return; - mqttAppend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0"); + mqttSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0"); } #if ENABLE_INFLUXDB @@ -405,7 +405,6 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo #if not MQTT_REPORT_RELAY relayMQTT(); - mqttSend(); #endif char buffer[strlen(MQTT_TOPIC_RELAY) + 3]; @@ -526,7 +525,6 @@ void relayLoop(void) { if (_relays[id].scheduledReport) { relayMQTT(id); - mqttSend(); } if (!recursive) { relayPulse(id);