123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- float currTemp_lastPublished;
- int currHum_lastPublished;
- void publishCurrentSensorValues(bool force = false)
- {
- char _tmp_topic_out[50];
- char _temp_chararr[6];
- char _hum_chararr[4];
-
- #ifdef ENABLE_SENSOR_DHT22
- if (confSens.DHT_enable && sensor_DHT_lastUpdate != 0 && (millis() - sensor_DHT_lastUpdate) < 120000)
- { // ensure values are not too old
- dtostrf(sensor_DHT_currTemp, 1, 1, _temp_chararr);
- sprintf(_hum_chararr, "%2i", sensor_DHT_currHum);
- // if (serialdebug)
- // {
- // Serial.print(F("sensors: {"));
- // Serial.print("'temp':");
- // Serial.print(_temp_chararr);
- // }
- if (force || sensor_DHT_currTemp != currTemp_lastPublished)
- {
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp");
- mqttclient.publish(_tmp_topic_out, _temp_chararr, confMqtt.mqtt_outRetain_sensors);
- currTemp_lastPublished = sensor_DHT_currTemp;
- yield();
- }
- // if (serialdebug)
- // {
- // Serial.print(F(",'hum':"));
- // Serial.print(sensor_DHT_currHum);
- // }
- if (force || sensor_DHT_currHum != currHum_lastPublished)
- {
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum");
- mqttclient.publish(_tmp_topic_out, _hum_chararr, confMqtt.mqtt_outRetain_sensors);
- yield();
- }
- dtostrf(sensor_DHT_currTemp_raw, 1, 1, _temp_chararr);
- sprintf(_hum_chararr, "%2i", sensor_DHT_currHum_raw);
- // if (serialdebug)
- // {
- // Serial.print(F(",'temp_raw':"));
- // Serial.print(_temp_chararr);
- // }
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_raw");
- mqttclient.publish(_tmp_topic_out, _temp_chararr);
- yield();
- // if (serialdebug)
- // {
- // Serial.print(F(",'hum_raw':"));
- // Serial.print(sensor_DHT_currHum_raw);
- // }
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum_raw");
- mqttclient.publish(_tmp_topic_out, _hum_chararr);
- yield();
- }
- #endif // ENABLE_SENSOR_DHT22
- #ifdef ENABLE_SENSORS_ONEWIRE
- if (owTemp_feed > -50.0) {
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_feed");
- dtostrf(owTemp_feed, 1, 1, _temp_chararr);
- mqttclient.publish(_tmp_topic_out, _temp_chararr);
- yield();
- }
- if (owTemp_return > -50.0) {
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_return");
- dtostrf(owTemp_return, 1, 1, _temp_chararr);
- mqttclient.publish(_tmp_topic_out, _temp_chararr);
- yield();
- }
- if (owTemp_out > -50.0) {
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_out");
- dtostrf(owTemp_out, 1, 1, _temp_chararr);
- mqttclient.publish(_tmp_topic_out, _temp_chararr);
- yield();
- }
- #endif // ENABLE_SENSORS_ONEWIRE
- }
- void publishCurrentPIRValue()
- {
- char _tmp_topic_out[50];
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "PIR");
- // PIR internal topic
- if (PIRSensorOn)
- {
- mqttclient.publish(_tmp_topic_out, confAdd.mqtt_payload_pir_on);
- sendLog("SENS: PIR=ON", LOGLEVEL_INFO);
- }
- else
- {
- mqttclient.publish(_tmp_topic_out, confAdd.mqtt_payload_pir_off);
- sendLog("SENS: PIR=OFF", LOGLEVEL_INFO);
- }
- // PIR additional topic
- if (strlen(confAdd.mqtt_topic_pir) >= 4)
- {
- if (PIRSensorOn)
- {
- mqttclient.publish(confAdd.mqtt_topic_pir, confAdd.mqtt_payload_pir_on);
- }
- else
- {
- mqttclient.publish(confAdd.mqtt_topic_pir, confAdd.mqtt_payload_pir_off);
- }
- }
- }
- void publishDeleteRetainedSavedStates()
- {
- // after "save states to MQTT retained" is switched off
- // old retained messages are deleted by publishing an empty retained message to the topics
- // to be called from config when confTherm.saveToMqttRetained was changed to false
- #ifdef FIRMWARE_VARIANT_THERMOSTAT
- if (!confTherm.saveToMqttRetained)
- {
- mqttclient.publish(mqtt_topic_in_setTemp, "", true);
- mqttclient.publish(mqtt_topic_in_setMode, "", true);
- mqttclient.publish(mqtt_topic_in_setPreset, "", true);
- thermostat_publishCurrentValues(true); // force publish current values again
-
- sendLog("MQTT: [Thermostat] deleted retained saved states", LOGLEVEL_INFO);
- }
- #endif
- }
- void publishDeleteRetainedOutMessages()
- {
- // after "MQTT-out retained" is switched off
- // old retained messages are deleted by publishing an empty retained message to the topics
- // to be called from config when confMqtt.mqtt_outRetain was changed to false
- if (!confMqtt.mqtt_outRetain)
- {
- #ifdef FIRMWARE_VARIANT_THERMOSTAT
- char _tmp_topic_out[50];
-
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "setTemp");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "currSetTemp");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "mode");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "modeName");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "modeHA");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "preset");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "presetName");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "presetHA");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heating");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heatingOnTime");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heatingOffTime");
- mqttclient.publish(_tmp_topic_out, "", true);
-
- thermostat_publishCurrentValues(true); // force publish current values again
- #endif
- sendLog("MQTT: deleted retained messages (states)", LOGLEVEL_INFO);
- }
- }
- void publishDeleteRetainedOutMessages_sensors()
- {
- // after "MQTT-out retained" is switched off
- // old retained messages are deleted by publishing an empty retained message to the topics
- // to be called from config when confMqtt.mqtt_outRetain was changed to false
- if (!confMqtt.mqtt_outRetain)
- {
- char _tmp_topic_out[50];
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp");
- mqttclient.publish(_tmp_topic_out, "", true);
- snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum");
- mqttclient.publish(_tmp_topic_out, "", true);
- sendLog("MQTT: deleted retained messages (sensors)", LOGLEVEL_INFO);
- publishCurrentSensorValues(true); // force publish current values again
- }
- }
|