ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mqtt.cpp 4.9KB

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