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.

112 lines
3.2 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 ENABLE_DHT
  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. apiRegister(DHT_TEMPERATURE_TOPIC, DHT_TEMPERATURE_TOPIC, [](char * buffer, size_t len) {
  23. dtostrf(_dhtTemperature, len-1, 1, buffer);
  24. });
  25. apiRegister(DHT_HUMIDITY_TOPIC, DHT_HUMIDITY_TOPIC, [](char * buffer, size_t len) {
  26. snprintf(buffer, len, "%d", _dhtHumidity);
  27. });
  28. }
  29. void dhtLoop() {
  30. // Check if we should read new data
  31. static unsigned long last_update = 0;
  32. if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
  33. last_update = millis();
  34. unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();
  35. // Read sensor data
  36. double h = dht.readHumidity();
  37. double t = dht.readTemperature(tmpUnits == TMP_FAHRENHEIT);
  38. // Check if readings are valid
  39. if (isnan(h) || isnan(t)) {
  40. DEBUG_MSG_P(PSTR("[DHT] Error reading sensor\n"));
  41. } else {
  42. _dhtTemperature = t;
  43. _dhtHumidity = h;
  44. char temperature[6];
  45. char humidity[6];
  46. dtostrf(t, 4, 1, temperature);
  47. itoa((unsigned int) h, humidity, 10);
  48. DEBUG_MSG_P(PSTR("[DHT] Temperature: %s%s\n"), temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF");
  49. DEBUG_MSG_P(PSTR("[DHT] Humidity: %s\n"), humidity);
  50. // Send MQTT messages
  51. mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  52. mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  53. // Send to Domoticz
  54. #if ENABLE_DOMOTICZ
  55. {
  56. domoticzSend("dczTmpIdx", 0, temperature);
  57. int status;
  58. if (h > 70) {
  59. status = HUMIDITY_WET;
  60. } else if (h > 45) {
  61. status = HUMIDITY_COMFORTABLE;
  62. } else if (h > 30) {
  63. status = HUMIDITY_NORMAL;
  64. } else {
  65. status = HUMIDITY_DRY;
  66. }
  67. char buffer[2];
  68. sprintf(buffer, "%d", status);
  69. domoticzSend("dczHumIdx", humidity, buffer);
  70. }
  71. #endif
  72. #if ENABLE_INFLUXDB
  73. influxDBSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
  74. influxDBSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
  75. #endif
  76. // Update websocket clients
  77. char buffer[100];
  78. sprintf_P(buffer, PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits);
  79. wsSend(buffer);
  80. }
  81. }
  82. }
  83. #endif