Skip to content

Commit

Permalink
fix mqtt loop too much
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorYbema committed Dec 17, 2020
1 parent 711f8b7 commit 97d2282
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
26 changes: 16 additions & 10 deletions HeishaMon/HeishaMon.ino
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ void mqtt_reconnect()
{
mqttReconnects++;
sprintf(topic, "%s/%s/#", heishamonSettings.mqtt_topic_base, mqtt_topic_commands);
mqtt_client.loop(); mqtt_client.subscribe(topic); mqtt_client.loop();
mqtt_client.subscribe(topic);
sprintf(topic, "%s/%s", heishamonSettings.mqtt_topic_base, mqtt_send_raw_value_topic);
mqtt_client.loop(); mqtt_client.subscribe(topic); mqtt_client.loop();
mqtt_client.subscribe(topic);
sprintf(topic, "%s/%s", heishamonSettings.mqtt_topic_base, mqtt_willtopic);
mqtt_client.loop(); mqtt_client.publish(topic, "Online"); mqtt_client.loop();
mqtt_client.publish(topic, "Online");
sprintf(topic, "%s/%s", heishamonSettings.mqtt_topic_base, mqtt_iptopic);
mqtt_client.loop(); mqtt_client.publish(topic, WiFi.localIP().toString().c_str(), true); mqtt_client.loop();
mqtt_client.publish(topic, WiFi.localIP().toString().c_str(), true);
}
}
}
Expand All @@ -108,11 +108,17 @@ void log_message(char* string)
Serial1.print(": ");
Serial1.println(string);
}
if (heishamonSettings.logMqtt)
if (heishamonSettings.logMqtt && mqtt_client.connected())
{
char log_topic[256];
sprintf(log_topic, "%s/%s", heishamonSettings.mqtt_topic_base, mqtt_logtopic);
mqtt_client.loop(); mqtt_client.publish(log_topic, string); mqtt_client.loop();

if (!mqtt_client.publish(log_topic, string)) {
Serial1.print(millis());
Serial1.print(": ");
Serial1.println("MQTT publish log message failed!");
mqtt_client.disconnect();
}
}
}

Expand Down Expand Up @@ -474,14 +480,14 @@ void loop() {
if (heishamonSettings.use_s0) s0Loop(mqtt_client, log_message, heishamonSettings.mqtt_topic_base, heishamonSettings.s0Settings);

if ((!sending) && (!heishamonSettings.listenonly) && (heishamonSettings.optionalPCB)) send_optionalpcb_query();

// run the data query only each WAITTIME
if (millis() > nexttime) {
String message = "Heishamon stats: Uptime: " + getUptime() + " ## Free memory: " + getFreeMemory() + "% " + ESP.getFreeHeap() + " bytes ## Wifi: " + getWifiQuality() + "% ## Mqtt reconnects: " + mqttReconnects;
log_message((char*)message.c_str());
if (!mqtt_client.connected())
{
log_message((char *)"Lost MQTT connection, reconnecting...");
log_message((char *)"Lost MQTT connection!");
if (WiFi.status() != WL_CONNECTED) {
log_message((char *)"Lost WiFi connection, rebooting...");
delay(1000);
Expand All @@ -496,10 +502,10 @@ void loop() {
}
nexttime = millis() + (1000 * heishamonSettings.waitTime);
if (!heishamonSettings.listenonly) send_panasonic_query();

MDNS.announce();
//Make sure the LWT is set to Online, even if the broker have marked it dead.
sprintf(mqtt_topic, "%s/%s", heishamonSettings.mqtt_topic_base, mqtt_willtopic);
mqtt_client.loop(); mqtt_client.publish(mqtt_topic, "Online"); mqtt_client.loop();
mqtt_client.publish(mqtt_topic, "Online");
}
}
2 changes: 1 addition & 1 deletion HeishaMon/dallas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void readNewDallasTemp(PubSubClient &mqtt_client, void (*log_message)(char*), ch
actDallasData[i].temperature = temp;
sprintf(log_msg, "Received 1wire sensor temperature (%s): %.2f", actDallasData[i].address, actDallasData[i].temperature); log_message(log_msg);
sprintf(valueStr, "%.2f", actDallasData[i].temperature);
sprintf(mqtt_topic, "%s/%s/%s", mqtt_topic_base, mqtt_topic_1wire, actDallasData[i].address); mqtt_client.loop(); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES); mqtt_client.loop();
sprintf(mqtt_topic, "%s/%s/%s", mqtt_topic_base, mqtt_topic_1wire, actDallasData[i].address); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions HeishaMon/decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void decode_heatpump_data(char* data, String actData[], PubSubClient &mqtt_clien
if ((updatenow) || ( actData[Topic_Number] != Topic_Value )) {
actData[Topic_Number] = Topic_Value;
sprintf(log_msg, "received TOP%d %s: %s", Topic_Number, topics[Topic_Number], Topic_Value.c_str()); log_message(log_msg);
sprintf(mqtt_topic, "%s/%s/%s", mqtt_topic_base, mqtt_topic_values, topics[Topic_Number]); mqtt_client.loop(); mqtt_client.publish(mqtt_topic, Topic_Value.c_str(), MQTT_RETAIN_VALUES); mqtt_client.loop();
sprintf(mqtt_topic, "%s/%s/%s", mqtt_topic_base, mqtt_topic_values, topics[Topic_Number]); mqtt_client.publish(mqtt_topic, Topic_Value.c_str(), MQTT_RETAIN_VALUES);
}
}
}
Expand Down Expand Up @@ -248,7 +248,7 @@ void decode_optional_heatpump_data(char* data, String actOptData[], PubSubClient
if ((updatenow) || ( actOptData[Topic_Number] != Topic_Value )) {
actOptData[Topic_Number] = Topic_Value;
sprintf(log_msg, "received OPT%d %s: %s", Topic_Number, Topic_Name.c_str(), Topic_Value.c_str()); log_message(log_msg);
sprintf(mqtt_topic, "%s/%s/%s", mqtt_topic_base, mqtt_topic_pcbvalues, Topic_Name.c_str()); mqtt_client.loop(); mqtt_client.publish(mqtt_topic, Topic_Value.c_str(), MQTT_RETAIN_VALUES); mqtt_client.loop();
sprintf(mqtt_topic, "%s/%s/%s", mqtt_topic_base, mqtt_topic_pcbvalues, Topic_Name.c_str()); mqtt_client.publish(mqtt_topic, Topic_Value.c_str(), MQTT_RETAIN_VALUES);
}
}
//response to heatpump should contain the data from heatpump on byte 4 and 5
Expand Down
6 changes: 3 additions & 3 deletions HeishaMon/s0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ void s0Loop(PubSubClient &mqtt_client, void (*log_message)(char*), char* mqtt_to
char valueStr[20];
sprintf(log_msg, "Measured Watthour on S0 port %d: %.2f", (i + 1), Watthour ); log_message(log_msg);
sprintf(valueStr, "%.2f", Watthour);
sprintf(mqtt_topic, "%s/%s/Watthour/%d", mqtt_topic_base, mqtt_topic_s0, (i + 1)); mqtt_client.loop(); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES); mqtt_client.loop();
sprintf(mqtt_topic, "%s/%s/Watthour/%d", mqtt_topic_base, mqtt_topic_s0, (i + 1)); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES);
float WatthourTotal = (actS0Data[i].pulsesTotal * ( 1000.0 / actS0Settings[i].ppkwh));
sprintf(log_msg, "Measured total Watthour on S0 port %d: %.2f", (i + 1), WatthourTotal ); log_message(log_msg);
sprintf(valueStr, "%.2f", WatthourTotal);
sprintf(mqtt_topic, "%s/%s/WatthourTotal/%d", mqtt_topic_base, mqtt_topic_s0, (i + 1)); mqtt_client.loop(); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES); mqtt_client.loop();
sprintf(mqtt_topic, "%s/%s/WatthourTotal/%d", mqtt_topic_base, mqtt_topic_s0, (i + 1)); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES);
sprintf(log_msg, "Calculated Watt on S0 port %d: %u", (i + 1), actS0Data[i].watt); log_message(log_msg);
sprintf(valueStr, "%u", actS0Data[i].watt);
sprintf(mqtt_topic, "%s/%s/Watt/%d", mqtt_topic_base, mqtt_topic_s0, (i + 1)); mqtt_client.loop(); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES); mqtt_client.loop();
sprintf(mqtt_topic, "%s/%s/Watt/%d", mqtt_topic_base, mqtt_topic_s0, (i + 1)); mqtt_client.publish(mqtt_topic, valueStr, MQTT_RETAIN_VALUES);
}
}
}
Expand Down
Binary file not shown.

0 comments on commit 97d2282

Please sign in to comment.