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.

77 lines
1.7 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
8 years ago
  1. /*
  2. ESPurna
  3. DHT MODULE
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #if ENABLE_DHT
  7. #include <DHT.h>
  8. #include <Adafruit_Sensor.h>
  9. DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING);
  10. char dhtTemperature[6];
  11. char dhtHumidity[6];
  12. // -----------------------------------------------------------------------------
  13. // DHT
  14. // -----------------------------------------------------------------------------
  15. char * getDHTTemperature() {
  16. return dhtTemperature;
  17. }
  18. char * getDHTHumidity() {
  19. return dhtHumidity;
  20. }
  21. void dhtSetup() {
  22. dht.begin();
  23. }
  24. void dhtLoop() {
  25. if (!mqttConnected()) return;
  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. // Update websocket clients
  45. char buffer[100];
  46. sprintf_P(buffer, PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s}"), dhtTemperature, dhtHumidity);
  47. wsSend(buffer);
  48. }
  49. }
  50. }
  51. #endif