Skip to content

Commit

Permalink
Add RSSI reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
anyuta1166 committed Sep 29, 2021
1 parent c9e92e1 commit 6a49596
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 89 deletions.
174 changes: 87 additions & 87 deletions watermeter/mqtt.ino
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
9 changes: 7 additions & 2 deletions watermeter/watermeter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */


Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 6a49596

Please sign in to comment.