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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (found_bme1) {
  43. mqtt.publish(SENSOR_LOCATION "/temperature", String(bme1_temp()).c_str(), true);
  44. mqtt.publish(SENSOR_LOCATION "/humidity", String(bme1_humid()).c_str(), true);
  45. mqtt.publish(SENSOR_LOCATION "/pressure", String(bme1_pressure()).c_str(), true);
  46. } else if (found_bme2) {
  47. mqtt.publish(SENSOR_LOCATION "/temperature", String(bme2_temp()).c_str(), true);
  48. mqtt.publish(SENSOR_LOCATION "/humidity", String(bme2_humid()).c_str(), true);
  49. mqtt.publish(SENSOR_LOCATION "/pressure", String(bme2_pressure()).c_str(), true);
  50. } else if (found_sht) {
  51. mqtt.publish(SENSOR_LOCATION "/temperature", String(sht_temp()).c_str(), true);
  52. mqtt.publish(SENSOR_LOCATION "/humidity", String(sht_humid()).c_str(), true);
  53. }
  54. #ifdef ENABLE_CCS811
  55. if (found_ccs1) {
  56. mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs1_eco2()).c_str(), true);
  57. mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs1_tvoc()).c_str(), true);
  58. } else if (found_ccs2) {
  59. mqtt.publish(SENSOR_LOCATION "/eco2", String(ccs2_eco2()).c_str(), true);
  60. mqtt.publish(SENSOR_LOCATION "/tvoc", String(ccs2_tvoc()).c_str(), true);
  61. }
  62. #endif // ENABLE_CCS811
  63. }
  64. static void mqttCallback(char* topic, byte* payload, unsigned int length) {
  65. #ifdef FEATURE_RELAIS
  66. int state = 0;
  67. int id = -1;
  68. String ts(topic), ps((char *)payload);
  69. String our_topic(SENSOR_LOCATION);
  70. our_topic += "/";
  71. if (!ts.startsWith(our_topic)) {
  72. debug.print(F("Unknown MQTT room "));
  73. debug.println(ts);
  74. return;
  75. }
  76. String ids = ts.substring(our_topic.length());
  77. for (int i = 0; i < relais_count(); i++) {
  78. if (ids == relais_name(i)) {
  79. id = i;
  80. break;
  81. }
  82. }
  83. if (id < 0) {
  84. debug.print(F("Unknown MQTT topic "));
  85. debug.println(ts);
  86. return;
  87. }
  88. if (ps.indexOf("on") != -1) {
  89. state = 1;
  90. } else if (ps.indexOf("off") != -1) {
  91. state = 0;
  92. } else {
  93. return;
  94. }
  95. if ((id >= 0) && (id < relais_count())) {
  96. debug.print(F("Turning "));
  97. debug.print(state ? "on" : "off");
  98. debug.print(F(" relais "));
  99. debug.println(id);
  100. relais_set(id, state);
  101. writeDatabase();
  102. }
  103. #endif // FEATURE_RELAIS
  104. }
  105. static void mqttReconnect() {
  106. // Create a random client ID
  107. String clientId = F("ESP-" SENSOR_ID "-");
  108. clientId += String(random(0xffff), HEX);
  109. // Attempt to connect
  110. #if defined(MQTT_USER) && defined(MQTT_PASS)
  111. if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) {
  112. #else
  113. if (mqtt.connect(clientId.c_str())) {
  114. #endif
  115. #ifdef FEATURE_RELAIS
  116. for (int i = 0; i < relais_count(); i++) {
  117. String topic(SENSOR_LOCATION);
  118. topic += String("/") + relais_name(i);
  119. mqtt.subscribe(topic.c_str());
  120. }
  121. #endif // FEATURE_RELAIS
  122. }
  123. }
  124. void initMQTT() {
  125. debug.println(F("MQTT"));
  126. mqtt.setServer(MQTT_HOST, MQTT_PORT);
  127. mqtt.setCallback(mqttCallback);
  128. }
  129. void runMQTT() {
  130. unsigned long time = millis();
  131. if ((time - last_mqtt_write_time) >= MQTT_WRITE_INTERVAL) {
  132. last_mqtt_write_time = time;
  133. writeMQTT();
  134. }
  135. if (!mqtt.connected() && ((millis() - last_mqtt_reconnect_time) >= MQTT_RECONNECT_INTERVAL)) {
  136. last_mqtt_reconnect_time = millis();
  137. mqttReconnect();
  138. }
  139. mqtt.loop();
  140. }
  141. #else
  142. void initMQTT() { }
  143. void runMQTT() { }
  144. #endif // ENABLE_MQTT