-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensor_dht.cpp
125 lines (112 loc) · 4.05 KB
/
sensor_dht.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Temperature and Humidity sensor,
*
* Mitra Ardron: Nov 2024
*
* Testing using the Arduino KY-015 board with its DHT11 - connected to Lolin D1 Mini
*
*
* Configuration options example - these are all in _configuration.h
* Required:
* Optional:
* SENSOR_DHT_MS // How often to poll each sensor, for now we presume we poll them all this often
* SENSOR_DHT_PIN // Which pins sensors connected to - default to 4
* SENSOR_DHT_DEBUG // Debugging output
* SENSOR_DHT_COUNT // How many devices - default to 1
*
* See examples at https://www.thegeekpub.com/wiki/sensor-wiki-ky-015-dht11-combination-temperature-and-humidity-sensor
* Bit bashing at https://www.phippselectronics.com/using-the-temperature-and-humidity-sensor-ky-015-with-arduino/ looks simple as well
* similar at https://arduinomodules.info/ky-015-temperature-humidity-sensor-module/
* or non-blocking DHT at https://github.com/toannv17/DHT-Sensors-Non-Blocking/blob/main/DHT_Async.cpp (this is a library)
*
* This version uses Rob Tillart's library (who also did the SHT library we use)
*/
#include "_settings.h" // Settings for what to include etc
#ifdef SENSOR_DHT_WANT
#include <Arduino.h>
#include <dhtnew.h> // https://github.com/RobTillaart/DHTNew
#include "sensor_dht.h"
#include "system_mqtt.h" // Library for sending messages
Sensor_DHT::Sensor_DHT(const uint8_t pin_init, const char* topic_init, const char* topic2_init, const unsigned long ms_init)
: Sensor_HT(topic_init, topic2_init, ms_init), pin(pin_init) {
dht = new DHTNEW(pin_init); //TODO-64 is the library working for other DHTs - check other examples at https://github.com/RobTillaart/DHTNew/tree/master/examples
// dht->setType(11); // Override bug in DHTnew till fixed see https://github.com/RobTillaart/DHTNew/issues/104
}
#ifdef SENSOR_DHT_DEBUG
void printErrorCode(int chk) {
switch (chk)
{
case DHTLIB_OK:
Serial.print(F("OK,\t"));
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print(F("Checksum error,\t"));
break;
case DHTLIB_ERROR_TIMEOUT_A:
Serial.print(F("Time out A error,\t"));
break;
case DHTLIB_ERROR_TIMEOUT_B:
Serial.print(F("Time out B error,\t"));
break;
case DHTLIB_ERROR_TIMEOUT_C:
Serial.print(F("Time out C error,\t"));
break;
case DHTLIB_ERROR_TIMEOUT_D:
Serial.print(F("Time out D error,\t"));
break;
case DHTLIB_ERROR_SENSOR_NOT_READY:
Serial.print(F("Sensor not ready,\t"));
break;
case DHTLIB_ERROR_BIT_SHIFT:
Serial.print(F("Bit shift error,\t"));
break;
case DHTLIB_WAITING_FOR_READ:
Serial.print(F("Waiting for read,\t"));
break;
default:
Serial.print(F("Unknown: "));
Serial.print(chk);
Serial.print(F(",\t"));
break;
}
}
#endif // SHT_DHT_DEBUG
void Sensor_DHT::readAndSet() {
#ifdef SENSOR_DHT_DEBUG
Serial.print("DHT");
Serial.print(pin);
Serial.print(F(" "));
#endif
int chk = dht->read();
#ifdef SENSOR_DHT_DEBUG
printErrorCode(chk);
//Serial.println(dht->getType());
#endif
if (!chk) { // Dont read if error
float temp = dht->getTemperature();
float humy = dht->getHumidity();
// Note, not smoothing the data as it seems fairly stable and is float rather than bits anyway
#ifdef SENSOR_DHT_DEBUG
Serial.print(temp, 1);
Serial.print(F("°C\t"));
Serial.print(humy, 1);
Serial.println(F("%"));
#endif
// Store new results and optionally if changed send on MQTT
if (temp != temperature) {
temperature = temp;
if (topic) {
Mqtt->messageSend(topic, temperature, 1, false, 0); // topic, value, width, retain, qos
}
}
if (humy != humidity) { // TODO may want to add some bounds (e.g a percentage)
humidity = humy;
if (topic2) {
Mqtt->messageSend(topic2, humidity, 1, false, 0);
}
}
}
}
//Sensor_DHT sensor_dht(SENSOR_DHT_PIN, "temperature", "humidity", SENSOR_DHT_MS);
#endif // SENSOR_DHT_WANT
// -- END OF FILE --