From 6a49596cc762845d31b5c11addbb3c89bcefec42 Mon Sep 17 00:00:00 2001 From: Anna Tikhomirova Date: Wed, 29 Sep 2021 21:21:13 +0300 Subject: [PATCH] Add RSSI reporting --- watermeter/mqtt.ino | 174 +++++++++++++++++++------------------- watermeter/watermeter.ino | 9 +- 2 files changed, 94 insertions(+), 89 deletions(-) diff --git a/watermeter/mqtt.ino b/watermeter/mqtt.ino index 61c6581..dd07ca5 100644 --- a/watermeter/mqtt.ino +++ b/watermeter/mqtt.ino @@ -1,87 +1,87 @@ -void createMqttTopic() { - - String mac, s = wmConfig.mqttTopic; - - mac = makeMacAddress(); - - s += "/" + mac + "/"; - - mqttTopicHotOut = s + END_TOPIC_HOT_OUT; - mqttTopicColdOut = s + END_TOPIC_COLD_OUT; -} - - -void mqttReconnect() { - - if (apModeNow) return; - - createMqttTopic(); - - if (mqttFirstStart) { - Serial.printf("Full name out topic for hot water: %s\n", mqttTopicHotOut.c_str()); - Serial.printf("Full name out topic for cold water: %s\n", mqttTopicColdOut.c_str()); - mqttFirstStart = false; - } - - mqttClientId = MODULE_NAME; - mqttClientId += "-"; - mqttClientId += makeMacAddress(); - - mqttClient.setServer(wmConfig.mqttBroker, MQTT_PORT); - - if (!mqttClient.connected()) { - if (DEBUG) Serial.printf("Connecting to MQTT server: %s\n", wmConfig.mqttBroker); - if (mqttClient.connect(mqttClientId.c_str(), wmConfig.mqttUser, wmConfig.mqttPassword)) { - if (DEBUG) Serial.printf("Connected to MQTT server: %s\n", wmConfig.mqttBroker); - mqttRestart = false; - } else { - mqttRestart = true; - if (DEBUG) { - Serial.println("Could not connect to MQTT server"); - //printMqttState(); - } - } - } -} - -void printMqttState() { - - if (DEBUG) { - - switch (mqttClient.state()) { - case -4: - Serial.println("MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time"); - break; - case -3: - Serial.println("MQTT_CONNECTION_LOST - the network connection was broken"); - break; - case -2: - Serial.println("MQTT_CONNECT_FAILED - the network connection failed"); - break; - case -1: - Serial.println("MQTT_DISCONNECTED - the client is disconnected cleanly"); - break; - case 0: - Serial.println("MQTT_CONNECTED - the client is connected"); - break; - case 1: - Serial.println("MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT"); - break; - case 2: - Serial.println("MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier"); - break; - case 3: - Serial.println("MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection"); - break; - case 4: - Serial.println("MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected"); - break; - case 5: - Serial.println("MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect"); - break; - default: - break; - } - } -} - +void createMqttTopic() { + + String mac, s = wmConfig.mqttTopic; + + mac = makeMacAddress(); + + s += "/" + mac + "/"; + + mqttTopicHotOut = s + END_TOPIC_HOT_OUT; + mqttTopicColdOut = s + END_TOPIC_COLD_OUT; + mqttTopicRSSI = s + END_TOPIC_RSSI; +} + + +void mqttReconnect() { + + if (apModeNow) return; + + createMqttTopic(); + + if (mqttFirstStart) { + Serial.printf("Full name out topic for hot water: %s\n", mqttTopicHotOut.c_str()); + Serial.printf("Full name out topic for cold water: %s\n", mqttTopicColdOut.c_str()); + mqttFirstStart = false; + } + + mqttClientId = MODULE_NAME; + mqttClientId += "-"; + mqttClientId += makeMacAddress(); + + mqttClient.setServer(wmConfig.mqttBroker, MQTT_PORT); + + if (!mqttClient.connected()) { + if (DEBUG) Serial.printf("Connecting to MQTT server: %s\n", wmConfig.mqttBroker); + if (mqttClient.connect(mqttClientId.c_str(), wmConfig.mqttUser, wmConfig.mqttPassword)) { + if (DEBUG) Serial.printf("Connected to MQTT server: %s\n", wmConfig.mqttBroker); + mqttRestart = false; + } else { + mqttRestart = true; + if (DEBUG) { + Serial.println("Could not connect to MQTT server"); + //printMqttState(); + } + } + } +} + +void printMqttState() { + + if (DEBUG) { + + switch (mqttClient.state()) { + case -4: + Serial.println("MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time"); + break; + case -3: + Serial.println("MQTT_CONNECTION_LOST - the network connection was broken"); + break; + case -2: + Serial.println("MQTT_CONNECT_FAILED - the network connection failed"); + break; + case -1: + Serial.println("MQTT_DISCONNECTED - the client is disconnected cleanly"); + break; + case 0: + Serial.println("MQTT_CONNECTED - the client is connected"); + break; + case 1: + Serial.println("MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT"); + break; + case 2: + Serial.println("MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier"); + break; + case 3: + Serial.println("MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection"); + break; + case 4: + Serial.println("MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected"); + break; + case 5: + Serial.println("MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect"); + break; + default: + break; + } + } +} diff --git a/watermeter/watermeter.ino b/watermeter/watermeter.ino index fad7410..f406811 100644 --- a/watermeter/watermeter.ino +++ b/watermeter/watermeter.ino @@ -22,7 +22,7 @@ extern "C" { /* Name and Version */ #define PLATFORM "Wemos D1 mini & Micro SD" -#define MODULE_VERSION "v2.2.1" +#define MODULE_VERSION "v2.2.2" #define MODULE_NAME "WaterMeter " MODULE_VERSION #define WEB_WATERMETER_FIRST_NAME "Water" #define WEB_WATERMETER_LAST_NAME "Meter" @@ -77,12 +77,14 @@ WiFiClient wifiClient; #define MQTT_PORT 1883 #define END_TOPIC_HOT_OUT "HotWater" #define END_TOPIC_COLD_OUT "ColdWater" +#define END_TOPIC_RSSI "RSSI" PubSubClient mqttClient(wifiClient); String mqttClientId; /* "MODULE_NAME-MacAddress" */ -String mqttTopicHotOut, mqttTopicColdOut; +String mqttTopicHotOut, mqttTopicColdOut, mqttTopicRSSI; /* Full name Topic - * * mqttTopicHotOut - "MQTT_TOPIC/MacAddress/HotWater" * * mqttTopicColdOut - "MQTT_TOPIC/MacAddress/ColdWater" * + * mqttTopicRSSI - "MQTT_TOPIC/MacAddress/RSSI" * * see mqtt.ino */ @@ -224,6 +226,9 @@ void loop () { s = ""; s += wmConfig.hotWater; mqttClient.publish(mqttTopicHotOut.c_str(),s.c_str()); + s = ""; + s += WiFi.RSSI(); + mqttClient.publish(mqttTopicRSSI.c_str(),s.c_str()); mqttPublishLastTime = millis(); } }