From 6d6878d59ff090904093141d60a8bfbf5ec70f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 30 Dec 2016 19:01:36 +0100 Subject: [PATCH] Changes in MQTT calls. Using default client_id from AsyncMqttClient --- code/espurna/dht.ino | 4 ++-- code/espurna/ds18b20.ino | 2 +- code/espurna/emon.ino | 2 +- code/espurna/espurna.ino | 2 +- code/espurna/led.ino | 4 ++-- code/espurna/mqtt.ino | 38 ++++++++++++++++++++++++++++---------- code/espurna/pow.ino | 2 +- code/espurna/relay.ino | 13 ++----------- 8 files changed, 38 insertions(+), 29 deletions(-) diff --git a/code/espurna/dht.ino b/code/espurna/dht.ino index 5dd07492..4da26661 100644 --- a/code/espurna/dht.ino +++ b/code/espurna/dht.ino @@ -60,8 +60,8 @@ void dhtLoop() { DEBUG_MSG("[DHT] Humidity: %s\n", dhtHumidity); // Send MQTT messages - mqttSend((char *) getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), dhtTemperature); - mqttSend((char *) getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), dhtHumidity); + mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), dhtTemperature); + mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), dhtHumidity); // Update websocket clients char buffer[100]; diff --git a/code/espurna/ds18b20.ino b/code/espurna/ds18b20.ino index 02ecdd0f..7ea03cf1 100644 --- a/code/espurna/ds18b20.ino +++ b/code/espurna/ds18b20.ino @@ -54,7 +54,7 @@ void dsLoop() { DEBUG_MSG("[DS18B20] Temperature: %s\n", dsTemperature); // Send MQTT messages - mqttSend((char *) getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), dsTemperature); + mqttSend(getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), dsTemperature); // Update websocket clients char buffer[100]; diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index 7a85ba0a..7bb21e20 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -104,7 +104,7 @@ void powerMonitorLoop() { if (measurements == EMON_MEASUREMENTS) { double p = (sum - max - min) * mainsVoltage / (measurements - 2); sprintf(power, "%d", int(p)); - mqttSend((char *) getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power); + mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power); sum = 0; measurements = 0; } diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 8019104d..52e02a50 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -67,7 +67,7 @@ void hardwareLoop() { if (mqttConnected()) { if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) { last_heartbeat = millis(); - mqttSend((char *) MQTT_HEARTBEAT_TOPIC, (char *) "1"); + mqttSend(MQTT_HEARTBEAT_TOPIC, "1"); DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap()); DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str()); } diff --git a/code/espurna/led.ino b/code/espurna/led.ino index 962ffc1e..0038d699 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -74,7 +74,7 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload if (type == MQTT_MESSAGE_EVENT) { // Match topic - String t = String(topic); + String t = String(topic + mqttTopicRootLength()); if (!t.startsWith(MQTT_LED_TOPIC)) return; if (!t.endsWith(mqttSetter)) return; @@ -134,7 +134,7 @@ void ledSetup() { mqttRegister(ledMQTTCallback); DEBUG_MSG("[LED] Number of leds: %d\n", _leds.size()); - DEBUG_MSG("[LED] Led auto indicator is %s.\n", ledAuto ? "ON" : "OFF" ); + DEBUG_MSG("[LED] Led auto indicator is %s\n", ledAuto ? "ON" : "OFF" ); } diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 38274737..bedbf90d 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -37,17 +37,32 @@ void buildTopics() { mqttTopic.replace("{identifier}", getSetting("hostname")); } +unsigned int mqttTopicRootLength() { + return mqttTopic.length(); +} + +void mqttSendRaw(const char * topic, const char * message) { + if (mqtt.connected()) { + DEBUG_MSG("[MQTT] Sending %s %s\n", topic, message); + mqtt.publish(topic, MQTT_QOS, MQTT_RETAIN, message); + } +} + void mqttSend(const char * topic, const char * message) { - if (!mqtt.connected()) return; String path = mqttTopic + String(topic); - DEBUG_MSG("[MQTT] Sending %s %s\n", (char *) path.c_str(), message); - mqtt.publish(path.c_str(), MQTT_QOS, MQTT_RETAIN, message); + mqttSendRaw(path.c_str(), message); +} + +void mqttSubscribeRaw(const char * topic) { + if (mqtt.connected()) { + DEBUG_MSG("[MQTT] Subscribing to %s\n", topic); + mqtt.subscribe(topic, MQTT_QOS); + } } void mqttSubscribe(const char * topic) { String path = mqttTopic + String(topic); - DEBUG_MSG("[MQTT] Subscribing to %s\n", (char *) path.c_str()); - mqtt.subscribe(path.c_str(), MQTT_QOS); + mqttSubscribeRaw(path.c_str()); } void mqttRegister(void (*callback)(unsigned int, const char *, const char *)) { @@ -67,11 +82,11 @@ void _mqttOnConnect(bool sessionPresent) { mqtt.setWill((mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, (char *) "0"); // Say hello and report our IP and VERSION - mqttSend((char *) MQTT_IP_TOPIC, (char *) getIP().c_str()); - mqttSend((char *) MQTT_VERSION_TOPIC, (char *) APP_VERSION); + mqttSend(MQTT_IP_TOPIC, getIP().c_str()); + mqttSend(MQTT_VERSION_TOPIC, APP_VERSION); char buffer[50]; getFSVersion(buffer); - mqttSend((char *) MQTT_FSVERSION_TOPIC, buffer); + mqttSend(MQTT_FSVERSION_TOPIC, buffer); // Send connect event to subscribers for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) { @@ -93,7 +108,10 @@ void _mqttOnDisconnect(AsyncMqttClientDisconnectReason reason) { void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - DEBUG_MSG("[MQTT] Received %s %c", topic, payload[0]); + char message[len+1]; + strlcpy(message, payload, len+1); + + DEBUG_MSG("[MQTT] Received %s => %s", topic, message); #if MQTT_SKIP_RETAINED if (millis() - mqttConnectedAt < MQTT_SKIP_TIME) { @@ -107,7 +125,7 @@ void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties // Send message event to subscribers // Topic is set to the specific part each one might be checking for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) { - (*_mqtt_callbacks[i])(MQTT_MESSAGE_EVENT, topic + mqttTopic.length(), payload); + (*_mqtt_callbacks[i])(MQTT_MESSAGE_EVENT, topic, message); } } diff --git a/code/espurna/pow.ino b/code/espurna/pow.ino index 428838e5..bf820296 100644 --- a/code/espurna/pow.ino +++ b/code/espurna/pow.ino @@ -151,7 +151,7 @@ void powLoop() { wsSend(buffer); if (--report_count == 0) { - mqttSend((char *) getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), (char *) String(power).c_str()); + mqttSend(getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), String(power).c_str()); report_count = POW_REPORT_EVERY; } diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 8b0b1dbf..64ed9e36 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -26,7 +26,7 @@ void relayMQTT(unsigned char id) { String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3]; sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str()); - mqttSend(buffer, (char *) (relayStatus(id) ? "1" : "0")); + mqttSend(buffer, relayStatus(id) ? "1" : "0"); } void relayMQTT() { @@ -175,8 +175,6 @@ unsigned char relayCount() { void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) { - static bool isFirstMessage = true; - String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER); String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0; @@ -191,17 +189,10 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo if (type == MQTT_MESSAGE_EVENT) { // Match topic - String t = String(topic); + String t = String(topic + mqttTopicRootLength()); if (!t.startsWith(MQTT_RELAY_TOPIC)) return; if (!t.endsWith(mqttSetter)) return; - // If relayMode is not SAME avoid responding to a retained message - if (sameSetGet && isFirstMessage) { - isFirstMessage = false; - byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); - if (relayMode != RELAY_MODE_SAME) return; - } - // Get relay ID unsigned int relayID = topic[strlen(MQTT_RELAY_TOPIC)+1] - '0'; if (relayID >= relayCount()) relayID = 0;