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.

82 lines
2.0 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. char dhtTemperature[6];
  10. char dhtHumidity[6];
  11. // -----------------------------------------------------------------------------
  12. // Values
  13. // -----------------------------------------------------------------------------
  14. char * getDHTTemperature() {
  15. return dhtTemperature;
  16. }
  17. char * getDHTHumidity() {
  18. return dhtHumidity;
  19. }
  20. void dhtSetup() {
  21. dht.begin();
  22. apiRegister("/api/temperature", "temperature", getDHTTemperature);
  23. apiRegister("/api/humidity", "humidity", getDHTHumidity);
  24. }
  25. void dhtLoop() {
  26. // Check if we should read new data
  27. static unsigned long last_update = 0;
  28. if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
  29. last_update = millis();
  30. // Read sensor data
  31. double h = dht.readHumidity();
  32. double t = dht.readTemperature();
  33. // Check if readings are valid
  34. if (isnan(h) || isnan(t)) {
  35. DEBUG_MSG("[DHT] Error reading sensor\n");
  36. } else {
  37. dtostrf(t, 4, 1, dhtTemperature);
  38. itoa((int) h, dhtHumidity, 10);
  39. DEBUG_MSG("[DHT] Temperature: %s\n", dhtTemperature);
  40. DEBUG_MSG("[DHT] Humidity: %s\n", dhtHumidity);
  41. // Send MQTT messages
  42. mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), dhtTemperature);
  43. mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), dhtHumidity);
  44. // Send to Domoticz
  45. #if ENABLE_DOMOTICZ
  46. domoticzSend("dczTmpIdx", dhtTemperature);
  47. domoticzSend("dczHumIdx", dhtHumidity);
  48. #endif
  49. // Update websocket clients
  50. char buffer[100];
  51. sprintf_P(buffer, PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s}"), dhtTemperature, dhtHumidity);
  52. wsSend(buffer);
  53. }
  54. }
  55. }
  56. #endif