mqtt_out.ino 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. float currTemp_lastPublished;
  2. int currHum_lastPublished;
  3. void publishCurrentSensorValues(bool force = false)
  4. {
  5. char _tmp_topic_out[50];
  6. char _temp_chararr[6];
  7. char _hum_chararr[4];
  8. #ifdef ENABLE_SENSOR_DHT22
  9. if (confSens.DHT_enable && sensor_DHT_lastUpdate != 0 && (millis() - sensor_DHT_lastUpdate) < 120000)
  10. { // ensure values are not too old
  11. dtostrf(sensor_DHT_currTemp, 1, 1, _temp_chararr);
  12. sprintf(_hum_chararr, "%2i", sensor_DHT_currHum);
  13. // if (serialdebug)
  14. // {
  15. // Serial.print(F("sensors: {"));
  16. // Serial.print("'temp':");
  17. // Serial.print(_temp_chararr);
  18. // }
  19. if (force || sensor_DHT_currTemp != currTemp_lastPublished)
  20. {
  21. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp");
  22. mqttclient.publish(_tmp_topic_out, _temp_chararr, confMqtt.mqtt_outRetain_sensors);
  23. currTemp_lastPublished = sensor_DHT_currTemp;
  24. yield();
  25. }
  26. // if (serialdebug)
  27. // {
  28. // Serial.print(F(",'hum':"));
  29. // Serial.print(sensor_DHT_currHum);
  30. // }
  31. if (force || sensor_DHT_currHum != currHum_lastPublished)
  32. {
  33. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum");
  34. mqttclient.publish(_tmp_topic_out, _hum_chararr, confMqtt.mqtt_outRetain_sensors);
  35. yield();
  36. }
  37. dtostrf(sensor_DHT_currTemp_raw, 1, 1, _temp_chararr);
  38. sprintf(_hum_chararr, "%2i", sensor_DHT_currHum_raw);
  39. // if (serialdebug)
  40. // {
  41. // Serial.print(F(",'temp_raw':"));
  42. // Serial.print(_temp_chararr);
  43. // }
  44. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_raw");
  45. mqttclient.publish(_tmp_topic_out, _temp_chararr);
  46. yield();
  47. // if (serialdebug)
  48. // {
  49. // Serial.print(F(",'hum_raw':"));
  50. // Serial.print(sensor_DHT_currHum_raw);
  51. // }
  52. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum_raw");
  53. mqttclient.publish(_tmp_topic_out, _hum_chararr);
  54. yield();
  55. }
  56. #endif // ENABLE_SENSOR_DHT22
  57. #ifdef ENABLE_SENSORS_ONEWIRE
  58. if (owTemp_feed > -50.0) {
  59. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_feed");
  60. dtostrf(owTemp_feed, 1, 1, _temp_chararr);
  61. mqttclient.publish(_tmp_topic_out, _temp_chararr);
  62. yield();
  63. }
  64. if (owTemp_return > -50.0) {
  65. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_return");
  66. dtostrf(owTemp_return, 1, 1, _temp_chararr);
  67. mqttclient.publish(_tmp_topic_out, _temp_chararr);
  68. yield();
  69. }
  70. if (owTemp_out > -50.0) {
  71. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp_out");
  72. dtostrf(owTemp_out, 1, 1, _temp_chararr);
  73. mqttclient.publish(_tmp_topic_out, _temp_chararr);
  74. yield();
  75. }
  76. #endif // ENABLE_SENSORS_ONEWIRE
  77. }
  78. void publishCurrentPIRValue()
  79. {
  80. char _tmp_topic_out[50];
  81. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "PIR");
  82. // PIR internal topic
  83. if (PIRSensorOn)
  84. {
  85. mqttclient.publish(_tmp_topic_out, confAdd.mqtt_payload_pir_on);
  86. sendLog("SENS: PIR=ON", LOGLEVEL_INFO);
  87. }
  88. else
  89. {
  90. mqttclient.publish(_tmp_topic_out, confAdd.mqtt_payload_pir_off);
  91. sendLog("SENS: PIR=OFF", LOGLEVEL_INFO);
  92. }
  93. // PIR additional topic
  94. if (strlen(confAdd.mqtt_topic_pir) >= 4)
  95. {
  96. if (PIRSensorOn)
  97. {
  98. mqttclient.publish(confAdd.mqtt_topic_pir, confAdd.mqtt_payload_pir_on);
  99. }
  100. else
  101. {
  102. mqttclient.publish(confAdd.mqtt_topic_pir, confAdd.mqtt_payload_pir_off);
  103. }
  104. }
  105. }
  106. void publishDeleteRetainedSavedStates()
  107. {
  108. // after "save states to MQTT retained" is switched off
  109. // old retained messages are deleted by publishing an empty retained message to the topics
  110. // to be called from config when confTherm.saveToMqttRetained was changed to false
  111. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  112. if (!confTherm.saveToMqttRetained)
  113. {
  114. mqttclient.publish(mqtt_topic_in_setTemp, "", true);
  115. mqttclient.publish(mqtt_topic_in_setMode, "", true);
  116. mqttclient.publish(mqtt_topic_in_setPreset, "", true);
  117. thermostat_publishCurrentValues(true); // force publish current values again
  118. sendLog("MQTT: [Thermostat] deleted retained saved states", LOGLEVEL_INFO);
  119. }
  120. #endif
  121. }
  122. void publishDeleteRetainedOutMessages()
  123. {
  124. // after "MQTT-out retained" is switched off
  125. // old retained messages are deleted by publishing an empty retained message to the topics
  126. // to be called from config when confMqtt.mqtt_outRetain was changed to false
  127. if (!confMqtt.mqtt_outRetain)
  128. {
  129. #ifdef FIRMWARE_VARIANT_THERMOSTAT
  130. char _tmp_topic_out[50];
  131. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "setTemp");
  132. mqttclient.publish(_tmp_topic_out, "", true);
  133. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "currSetTemp");
  134. mqttclient.publish(_tmp_topic_out, "", true);
  135. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "mode");
  136. mqttclient.publish(_tmp_topic_out, "", true);
  137. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "modeName");
  138. mqttclient.publish(_tmp_topic_out, "", true);
  139. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "modeHA");
  140. mqttclient.publish(_tmp_topic_out, "", true);
  141. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "preset");
  142. mqttclient.publish(_tmp_topic_out, "", true);
  143. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "presetName");
  144. mqttclient.publish(_tmp_topic_out, "", true);
  145. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "presetHA");
  146. mqttclient.publish(_tmp_topic_out, "", true);
  147. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heating");
  148. mqttclient.publish(_tmp_topic_out, "", true);
  149. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heatingOnTime");
  150. mqttclient.publish(_tmp_topic_out, "", true);
  151. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "heatingOffTime");
  152. mqttclient.publish(_tmp_topic_out, "", true);
  153. thermostat_publishCurrentValues(true); // force publish current values again
  154. #endif
  155. sendLog("MQTT: deleted retained messages (states)", LOGLEVEL_INFO);
  156. }
  157. }
  158. void publishDeleteRetainedOutMessages_sensors()
  159. {
  160. // after "MQTT-out retained" is switched off
  161. // old retained messages are deleted by publishing an empty retained message to the topics
  162. // to be called from config when confMqtt.mqtt_outRetain was changed to false
  163. if (!confMqtt.mqtt_outRetain)
  164. {
  165. char _tmp_topic_out[50];
  166. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "temp");
  167. mqttclient.publish(_tmp_topic_out, "", true);
  168. snprintf(_tmp_topic_out, 50, "%s/%s", confMqtt.mqtt_topic_out, "hum");
  169. mqttclient.publish(_tmp_topic_out, "", true);
  170. sendLog("MQTT: deleted retained messages (sensors)", LOGLEVEL_INFO);
  171. publishCurrentSensorValues(true); // force publish current values again
  172. }
  173. }