ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mqtt.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * mqtt.cpp
  3. *
  4. * ESP8266 / ESP32 Environmental Sensor
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  9. * you can do whatever you want with this stuff. If we meet some day, and you
  10. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  11. * ----------------------------------------------------------------------------
  12. */
  13. #include <Arduino.h>
  14. #include "config.h"
  15. #include "DebugLog.h"
  16. #include "sensors.h"
  17. #include "relais.h"
  18. #include "influx.h"
  19. #include "mqtt.h"
  20. #ifdef ENABLE_MQTT
  21. #if defined(ARDUINO_ARCH_ESP8266)
  22. #include <ESP8266WiFi.h>
  23. #include <ESP8266WebServer.h>
  24. #include <ESP8266mDNS.h>
  25. #elif defined(ARDUINO_ARCH_ESP32)
  26. #include <WiFi.h>
  27. #include <WebServer.h>
  28. #include <ESPmDNS.h>
  29. #elif defined(ARDUINO_ARCH_AVR)
  30. #include <UnoWiFiDevEdSerial1.h>
  31. #include <WiFiLink.h>
  32. #endif
  33. #include <PubSubClient.h>
  34. WiFiClient mqttClient;
  35. PubSubClient mqtt(mqttClient);
  36. static unsigned long last_mqtt_reconnect_time = 0;
  37. static unsigned long last_mqtt_write_time = 0;
  38. static void writeMQTT() {
  39. if (!mqtt.connected()) {
  40. return;
  41. }
  42. bool wrote = false;
  43. if (found_sht) {
  44. mqtt.publish(SENSOR_LOCATION "/temperature", String(sht_temp()).c_str(), true);
  45. mqtt.publish(SENSOR_LOCATION "/humidity", String(sht_humid()).c_str(), true);
  46. wrote = true;
  47. #ifdef ENABLE_BME280
  48. } else if (found_bme1) {
  49. mqtt.publish(SENSOR_LOCATION "/temperature", String(bme1_temp()).c_str(), true);
  50. mqtt.publish(SENSOR_LOCATION "/humidity", String(bme1_humid()).c_str(), true);
  51. mqtt.publish(SENSOR_LOCATION "/pressure", String(bme1_pressure()).c_str(), true);
  52. wrote = true;
  53. } else if (found_bme2) {
  54. mqtt.publish(SENSOR_LOCATION "/temperature", String(bme2_temp()).c_str(), true);
  55. mqtt.publish(SENSOR_LOCATION "/humidity", String(bme2_humid()).c_str(), true);
  56. mqtt.publish(SENSOR_LOCATION "/pressure", String(bme2_pressure()).c_str(), true);
  57. wrote = true;
  58. #endif // ENABLE_BME280
  59. }
  60. #ifdef ENABLE_CCS811
  61. if (found_ccs1) {
  62. mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs1_eco2()).c_str(), true);
  63. mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs1_tvoc()).c_str(), true);
  64. wrote = true;
  65. } else if (found_ccs2) {
  66. mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs2_eco2()).c_str(), true);
  67. mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs2_tvoc()).c_str(), true);
  68. wrote = true;
  69. }
  70. #endif // ENABLE_CCS811
  71. if (wrote) {
  72. debug.println(F("Updated MQTT sensor values"));
  73. }
  74. }
  75. static void mqttCallback(char* topic, byte* payload, unsigned int length) {
  76. String ts(topic), ps;
  77. for (unsigned int i = 0; i < length; i++) {
  78. char c = payload[i];
  79. ps += c;
  80. }
  81. debug.print(F("MQTT Rx @ \""));
  82. debug.print(ts);
  83. debug.print(F("\" = \""));
  84. debug.print(ps);
  85. debug.println(F("\""));
  86. #ifdef FEATURE_RELAIS
  87. int state = 0;
  88. int id = -1;
  89. String our_topic(SENSOR_LOCATION);
  90. our_topic += "/";
  91. if (!ts.startsWith(our_topic)) {
  92. debug.print(F("Unknown MQTT room "));
  93. debug.println(ts);
  94. return;
  95. }
  96. String ids = ts.substring(our_topic.length());
  97. for (int i = 0; i < relais_count(); i++) {
  98. if (ids == relais_name(i)) {
  99. id = i;
  100. break;
  101. }
  102. }
  103. if (id < 0) {
  104. debug.print(F("Unknown MQTT topic "));
  105. debug.println(ts);
  106. return;
  107. }
  108. if (ps.indexOf("on") != -1) {
  109. state = 1;
  110. } else if (ps.indexOf("off") != -1) {
  111. state = 0;
  112. } else {
  113. return;
  114. }
  115. if ((id >= 0) && (id < relais_count())) {
  116. debug.print(F("Turning "));
  117. debug.print(state ? "on" : "off");
  118. debug.print(F(" relais "));
  119. debug.println(id);
  120. relais_set(id, state);
  121. writeDatabase();
  122. }
  123. #endif // FEATURE_RELAIS
  124. }
  125. static void mqttReconnect() {
  126. // Create a random client ID
  127. String clientId = F("ESP-" SENSOR_ID "-");
  128. clientId += String(random(0xffff), HEX);
  129. // Attempt to connect
  130. #if defined(MQTT_USER) && defined(MQTT_PASS)
  131. if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) {
  132. #else
  133. if (mqtt.connect(clientId.c_str())) {
  134. #endif
  135. #ifdef FEATURE_RELAIS
  136. for (int i = 0; i < relais_count(); i++) {
  137. String topic(SENSOR_LOCATION);
  138. topic += String("/") + relais_name(i);
  139. mqtt.subscribe(topic.c_str());
  140. }
  141. #endif // FEATURE_RELAIS
  142. #ifdef FEATURE_UI
  143. mqtt.subscribe("livingroom/light_kitchen");
  144. #endif // FEATURE_UI
  145. }
  146. }
  147. void initMQTT() {
  148. mqtt.setServer(MQTT_HOST, MQTT_PORT);
  149. mqtt.setCallback(mqttCallback);
  150. }
  151. void runMQTT() {
  152. unsigned long time = millis();
  153. if ((time - last_mqtt_write_time) >= MQTT_WRITE_INTERVAL) {
  154. last_mqtt_write_time = time;
  155. writeMQTT();
  156. }
  157. if (!mqtt.connected() && ((millis() - last_mqtt_reconnect_time) >= MQTT_RECONNECT_INTERVAL)) {
  158. last_mqtt_reconnect_time = millis();
  159. mqttReconnect();
  160. }
  161. mqtt.loop();
  162. }
  163. #ifdef FEATURE_UI
  164. void writeMQTTtopic(const char *topic, const char *data, bool retain) {
  165. mqtt.publish(topic, data, retain);
  166. }
  167. #endif
  168. #else
  169. void initMQTT() { }
  170. void runMQTT() { }
  171. #endif // ENABLE_MQTT