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.

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