From 6b1803d3895afeb9559bb36beff3f498e8dc0f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 12 Mar 2017 10:10:21 +0100 Subject: [PATCH] Refactor MQTT topics and reports setting names --- code/espurna/button.ino | 6 +-- code/espurna/config/general.h | 53 ++++++++++++-------- code/espurna/espurna.ino | 93 ++++++++++++++++++++++------------- code/espurna/led.ino | 8 +-- code/espurna/light.ino | 6 +-- code/espurna/mqtt.ino | 20 ++++---- code/espurna/relay.ino | 18 ++++--- 7 files changed, 120 insertions(+), 84 deletions(-) diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 4479ef51..24f78af7 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -21,12 +21,12 @@ typedef struct { std::vector _buttons; -#ifdef MQTT_BUTTON_TOPIC +#ifdef MQTT_TOPIC_BUTTON void buttonMQTT(unsigned char id, uint8_t event) { if (id >= _buttons.size()) return; char payload[2]; sprintf(payload, "%d", event); - mqttSend(MQTT_BUTTON_TOPIC, id, payload); + mqttSend(MQTT_TOPIC_BUTTON, id, payload); } #endif @@ -69,7 +69,7 @@ void buttonEvent(unsigned int id, unsigned char event) { DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", id, event); if (event == 0) return; - #ifdef MQTT_BUTTON_TOPIC + #ifdef MQTT_TOPIC_BUTTON buttonMQTT(id, event); #endif diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index f34ef24d..96bfddee 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -145,17 +145,38 @@ #define MQTT_MAX_TRIES 12 #define MQTT_SKIP_RETAINED 1 #define MQTT_SKIP_TIME 1000 -#define MQTT_ACTION_TOPIC "/action" -#define MQTT_RELAY_TOPIC "/relay" -#define MQTT_LED_TOPIC "/led" -#define MQTT_COLOR_TOPIC "/color" -#define MQTT_BUTTON_TOPIC "/button" -#define MQTT_IP_TOPIC "/ip" -#define MQTT_VERSION_TOPIC "/version" -#define MQTT_UPTIME_TOPIC "/uptime" -#define MQTT_FREEHEAP_TOPIC "/freeheap" -#define MQTT_VCC_TOPIC "/vcc" -#define MQTT_STATUS_TOPIC "/status" + +#define MQTT_TOPIC_ACTION "/action" +#define MQTT_TOPIC_RELAY "/relay" +#define MQTT_TOPIC_LED "/led" +#define MQTT_TOPIC_COLOR "/color" +#define MQTT_TOPIC_BUTTON "/button" +#define MQTT_TOPIC_IP "/ip" +#define MQTT_TOPIC_VERSION "/version" +#define MQTT_TOPIC_UPTIME "/uptime" +#define MQTT_TOPIC_FREEHEAP "/freeheap" +#define MQTT_TOPIC_VCC "/vcc" +#define MQTT_TOPIC_STATUS "/status" +#define MQTT_TOPIC_MAC "/mac" +#define MQTT_TOPIC_APP "/app" +#define MQTT_TOPIC_INTERVAL "/interval" +#define MQTT_TOPIC_HOSTNAME "/hostname" + +// Periodic reports +#define MQTT_REPORT_STATUS 1 +#define MQTT_REPORT_IP 1 +#define MQTT_REPORT_MAC 1 +#define MQTT_REPORT_UPTIME 1 +#define MQTT_REPORT_FREEHEAP 1 +#define MQTT_REPORT_VCC 1 +#define MQTT_REPORT_RELAY 1 +#define MQTT_REPORT_HOSTNAME 1 +#define MQTT_REPORT_APP 1 +#define MQTT_REPORT_VERSION 1 +#define MQTT_REPORT_INTERVAL 0 + +#define MQTT_STATUS_ONLINE "1" +#define MQTT_STATUS_OFFLINE "0" #define MQTT_ACTION_RESET "reset" @@ -168,16 +189,6 @@ #define MQTT_USE_GETTER "" #define MQTT_USE_SETTER "" -// Periodic reports -#define MQTT_STATUS_REPORT 1 -#define MQTT_IP_REPORT 2 -#define MQTT_UPTIME_REPORT 4 -#define MQTT_FREEHEAP_REPORT 8 -#define MQTT_VCC_REPORT 16 -#define MQTT_RELAY_REPORT 32 - -#define MQTT_REPORTS (MQTT_STATUS_REPORT | MQTT_UPTIME_REPORT | MQTT_FREEHEAP_REPORT | MQTT_RELAY_REPORT) - // ----------------------------------------------------------------------------- // I2C // ----------------------------------------------------------------------------- diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index bf5621c8..a2db36b9 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -32,6 +32,62 @@ String getIdentifier() { return String(identifier); } +void heartbeat() { + + static unsigned long last_uptime = 0; + static unsigned char uptime_overflows = 0; + + if (millis() < last_uptime) ++uptime_overflows; + last_uptime = millis(); + unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000); + + DEBUG_MSG("[MAIN] Time: %s\n", (char *) NTP.getTimeDateString().c_str()); + if (!mqttConnected()) { + DEBUG_MSG("[MAIN] Uptime: %ld seconds\n", uptime_seconds); + DEBUG_MSG("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap()); + #if ENABLE_ADC_VCC + DEBUG_MSG("[MAIN] Power: %d mV\n", ESP.getVcc()); + #endif + } + + #if (MQTT_REPORT_INTERVAL) + mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000); + #endif + #if (MQTT_REPORT_APP) + mqttSend(MQTT_TOPIC_APP, APP_NAME); + #endif + #if (MQTT_REPORT_VERSION) + mqttSend(MQTT_TOPIC_VERSION, APP_VERSION); + #endif + #if (MQTT_REPORT_HOSTNAME) + mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str()); + #endif + #if (MQTT_REPORT_IP) + mqttSend(MQTT_TOPIC_IP, getIP().c_str()); + #endif + #if (MQTT_REPORT_MAC) + mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str()); + #endif + #if (MQTT_REPORT_UPTIME) + mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); + #endif + #if (MQTT_REPORT_FREEHEAP) + mqttSend(MQTT_TOPIC_FREEHEAP, String(ESP.getFreeHeap()).c_str()); + #endif + #if (MQTT_REPORT_RELAY) + relayMQTT(); + #endif + #if (MQTT_REPORT_VCC) + #if ENABLE_ADC_VCC + mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str()); + #endif + #endif + #if (MQTT_REPORT_STATUS) + mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE); + #endif + +} + void hardwareSetup() { EEPROM.begin(4096); Serial.begin(SERIAL_BAUDRATE); @@ -42,44 +98,11 @@ void hardwareSetup() { void hardwareLoop() { - static unsigned long last_uptime = 0; - static unsigned char uptime_overflows = 0; - // Heartbeat + static unsigned long last_uptime = 0; if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) { - - if (millis() < last_uptime) ++uptime_overflows; last_uptime = millis(); - unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000); - - DEBUG_MSG("[MAIN] Time: %s\n", (char *) NTP.getTimeDateString().c_str()); - DEBUG_MSG("[MAIN] Uptime: %ld seconds\n", uptime_seconds); - DEBUG_MSG("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap()); - #if ENABLE_ADC_VCC - DEBUG_MSG("[MAIN] Power: %d mV\n", ESP.getVcc()); - #endif - - #if (MQTT_REPORTS & MQTT_STATUS_REPORT) - mqttSend(MQTT_STATUS_TOPIC, "1"); - #endif - #if (MQTT_REPORTS & MQTT_IP_REPORT) - mqttSend(MQTT_IP_TOPIC, getIP().c_str()); - #endif - #if (MQTT_REPORTS & MQTT_UPTIME_REPORT) - mqttSend(MQTT_UPTIME_TOPIC, String(uptime_seconds).c_str()); - #endif - #if (MQTT_REPORTS & MQTT_FREEHEAP_REPORT) - mqttSend(MQTT_FREEHEAP_TOPIC, String(ESP.getFreeHeap()).c_str()); - #endif - #if (MQTT_REPORTS & MQTT_RELAY_REPORT) - relayMQTT(); - #endif - #if (MQTT_REPORTS & MQTT_VCC_REPORT) - #if ENABLE_ADC_VCC - mqttSend(MQTT_VCC_TOPIC, String(ESP.getVcc()).c_str()); - #endif - #endif - + heartbeat(); } } diff --git a/code/espurna/led.ino b/code/espurna/led.ino index 210f3169..a81334c3 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -63,8 +63,8 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload static bool isFirstMessage = true; if (type == MQTT_CONNECT_EVENT) { - char buffer[strlen(MQTT_LED_TOPIC) + 3]; - sprintf(buffer, "%s/+", MQTT_LED_TOPIC); + char buffer[strlen(MQTT_TOPIC_LED) + 3]; + sprintf(buffer, "%s/+", MQTT_TOPIC_LED); mqttSubscribe(buffer); } @@ -72,10 +72,10 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload // Match topic String t = mqttSubtopic((char *) topic); - if (!t.startsWith(MQTT_LED_TOPIC)) return; + if (!t.startsWith(MQTT_TOPIC_LED)) return; // Get led ID - unsigned int ledID = t.substring(strlen(MQTT_LED_TOPIC)+1).toInt(); + unsigned int ledID = t.substring(strlen(MQTT_TOPIC_LED)+1).toInt(); if (ledID >= ledCount()) { DEBUG_MSG("[LED] Wrong ledID (%d)\n", ledID); return; diff --git a/code/espurna/light.ino b/code/espurna/light.ino index ab8750f2..110713d6 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -94,7 +94,7 @@ void lightColor(const char * rgb, bool save, bool forward) { if (save) colorTicker.once(LIGHT_SAVE_DELAY, lightColorSave); // Report color to MQTT broker - if (forward) mqttSend(MQTT_COLOR_TOPIC, rgb); + if (forward) mqttSend(MQTT_TOPIC_COLOR, rgb); // Report color to WS clients char message[20]; @@ -143,14 +143,14 @@ void lightMQTTCallback(unsigned int type, const char * topic, const char * paylo if (type == MQTT_CONNECT_EVENT) { - mqttSubscribe(MQTT_COLOR_TOPIC); + mqttSubscribe(MQTT_TOPIC_COLOR); } if (type == MQTT_MESSAGE_EVENT) { // Match topic String t = mqttSubtopic((char *) topic); - if (!t.equals(MQTT_COLOR_TOPIC)) return; + if (!t.equals(MQTT_TOPIC_COLOR)) return; lightColor(payload, true, mqttForward()); diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index dfc0ff6c..7e8efb2e 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -21,6 +21,8 @@ bool _mqttConnected = false; String mqttTopic; bool _mqttForward; +char * _mqtt_pass = 0; +char * _mqtt_user = 0; std::vector _mqtt_callbacks; #if MQTT_SKIP_RETAINED unsigned long mqttConnectedAt = 0; @@ -118,13 +120,11 @@ void _mqttOnConnect() { // Build MQTT topics buildTopics(); - // Say hello and report our IP and VERSION - mqttSend(MQTT_IP_TOPIC, getIP().c_str()); - mqttSend(MQTT_VERSION_TOPIC, APP_VERSION); - mqttSend(MQTT_STATUS_TOPIC, "1"); + // Send first Heartbeat + heartbeat(); // Subscribe to system topics - mqttSubscribe(MQTT_ACTION_TOPIC); + mqttSubscribe(MQTT_TOPIC_ACTION); // Send connect event to subscribers for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) { @@ -160,7 +160,7 @@ void _mqttOnMessage(char* topic, char* payload, unsigned int len) { // Check system topics String t = mqttSubtopic((char *) topic); - if (t.equals(MQTT_ACTION_TOPIC)) { + if (t.equals(MQTT_TOPIC_ACTION)) { if (strcmp(message, MQTT_ACTION_RESET) == 0) { ESP.restart(); } @@ -203,13 +203,13 @@ void mqttConnect() { char * user = strdup(getSetting("mqttUser").c_str()); char * pass = strdup(getSetting("mqttPassword").c_str()); - DEBUG_MSG("[MQTT] Connecting to broker at %s", host); + DEBUG_MSG("[MQTT] Connecting to broker at %s:%d", host, port); mqtt.setServer(host, port); #if MQTT_USE_ASYNC mqtt.setKeepAlive(MQTT_KEEPALIVE).setCleanSession(false); - mqtt.setWill((mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); + mqtt.setWill((mqttTopic + MQTT_TOPIC_STATUS).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); if ((strlen(user) > 0) && (strlen(pass) > 0)) { DEBUG_MSG(" as user '%s'.", user); mqtt.setCredentials(user, pass); @@ -223,10 +223,10 @@ void mqttConnect() { if ((strlen(user) > 0) && (strlen(pass) > 0)) { DEBUG_MSG(" as user '%s'\n", user); - response = mqtt.connect(getIdentifier().c_str(), user, pass, (mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); + response = mqtt.connect(getIdentifier().c_str(), user, pass, (mqttTopic + MQTT_TOPIC_STATUS).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); } else { DEBUG_MSG("\n"); - response = mqtt.connect(getIdentifier().c_str(), (mqttTopic + MQTT_STATUS_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); + response = mqtt.connect(getIdentifier().c_str(), (mqttTopic + MQTT_TOPIC_STATUS).c_str(), MQTT_QOS, MQTT_RETAIN, "0"); } if (response) { diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 714c529c..e012e446 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -121,8 +121,8 @@ void relayPulseMode(unsigned int value, bool report) { /* if (report) { - char topic[strlen(MQTT_RELAY_TOPIC) + 10]; - sprintf(topic, "%s/pulse", MQTT_RELAY_TOPIC); + char topic[strlen(MQTT_TOPIC_RELAY) + 10]; + sprintf(topic, "%s/pulse", MQTT_TOPIC_RELAY); char value[2]; sprintf(value, "%d", value); mqttSend(topic, value); @@ -373,7 +373,7 @@ void relayDomoticzSetup() { void relayMQTT(unsigned char id) { if (id >= _relays.size()) return; - mqttSend(MQTT_RELAY_TOPIC, id, relayStatus(id) ? "1" : "0"); + mqttSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0"); } void relayMQTT() { @@ -386,10 +386,12 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo if (type == MQTT_CONNECT_EVENT) { - relayMQTT(); + #if not MQTT_REPORT_RELAY + relayMQTT(); + #endif - char buffer[strlen(MQTT_RELAY_TOPIC) + 3]; - sprintf(buffer, "%s/+", MQTT_RELAY_TOPIC); + char buffer[strlen(MQTT_TOPIC_RELAY) + 3]; + sprintf(buffer, "%s/+", MQTT_TOPIC_RELAY); mqttSubscribe(buffer); } @@ -398,7 +400,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo // Match topic String t = mqttSubtopic((char *) topic); - if (!t.startsWith(MQTT_RELAY_TOPIC)) return; + if (!t.startsWith(MQTT_TOPIC_RELAY)) return; // Get value unsigned int value = (char)payload[0] - '0'; @@ -410,7 +412,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo } // Get relay ID - unsigned int relayID = t.substring(strlen(MQTT_RELAY_TOPIC)+1).toInt(); + unsigned int relayID = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt(); if (relayID >= relayCount()) { DEBUG_MSG("[RELAY] Wrong relayID (%d)\n", relayID); return;