Fork of the espurna firmware for `mhsw` switches
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.

119 lines
3.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. DHT MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DHT_SUPPORT
  6. #include <DHT.h>
  7. #include <Adafruit_Sensor.h>
  8. DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING);
  9. double _dhtTemperature = 0;
  10. unsigned int _dhtHumidity = 0;
  11. // -----------------------------------------------------------------------------
  12. // Values
  13. // -----------------------------------------------------------------------------
  14. double getDHTTemperature() {
  15. return _dhtTemperature;
  16. }
  17. unsigned int getDHTHumidity() {
  18. return _dhtHumidity;
  19. }
  20. void dhtSetup() {
  21. dht.begin();
  22. #if WEB_SUPPORT
  23. apiRegister(DHT_TEMPERATURE_TOPIC, DHT_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
  24. dtostrf(_dhtTemperature, 1-len, 1, buffer);
  25. });
  26. apiRegister(DHT_HUMIDITY_TOPIC, DHT_HUMIDITY_TOPIC, [](char * buffer, size_t len) {
  27. snprintf_P(buffer, len, PSTR("%d"), _dhtHumidity);
  28. });
  29. #endif
  30. }
  31. void dhtLoop() {
  32. // Check if we should read new data
  33. static unsigned long last_update = 0;
  34. if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
  35. last_update = millis();
  36. unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
  37. // Read sensor data
  38. double h = dht.readHumidity();
  39. double t = dht.readTemperature(tmpUnits == TMP_FAHRENHEIT);
  40. // Check if readings are valid
  41. if (isnan(h) || isnan(t)) {
  42. DEBUG_MSG_P(PSTR("[DHT] Error reading sensor\n"));
  43. } else {
  44. _dhtTemperature = t;
  45. _dhtHumidity = h;
  46. char temperature[6];
  47. char humidity[6];
  48. dtostrf(t, 1-sizeof(temperature), 1, temperature);
  49. itoa((unsigned int) h, humidity, 10);
  50. DEBUG_MSG_P(PSTR("[DHT] Temperature: %s%s\n"), temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF");
  51. DEBUG_MSG_P(PSTR("[DHT] Humidity: %s\n"), humidity);
  52. // Send MQTT messages
  53. mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  54. mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  55. // Send to Domoticz
  56. #if DOMOTICZ_SUPPORT
  57. {
  58. domoticzSend("dczTmpIdx", 0, temperature);
  59. int status;
  60. if (h > 70) {
  61. status = HUMIDITY_WET;
  62. } else if (h > 45) {
  63. status = HUMIDITY_COMFORTABLE;
  64. } else if (h > 30) {
  65. status = HUMIDITY_NORMAL;
  66. } else {
  67. status = HUMIDITY_DRY;
  68. }
  69. char buffer[2];
  70. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), status);
  71. domoticzSend("dczHumIdx", humidity, buffer);
  72. }
  73. #endif
  74. #if INFLUXDB_SUPPORT
  75. influxDBSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  76. influxDBSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  77. #endif
  78. // Update websocket clients
  79. #if WEB_SUPPORT
  80. char buffer[100];
  81. snprintf_P(buffer, sizeof(buffer), PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits);
  82. wsSend(buffer);
  83. #endif
  84. }
  85. }
  86. }
  87. #endif