|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "wled.h" |
| 4 | + |
| 5 | +#include <dht_nonblocking.h> |
| 6 | + |
| 7 | +// USERMOD_DHT_DHTTYPE: |
| 8 | +// 11 // DHT 11 |
| 9 | +// 21 // DHT 21 |
| 10 | +// 22 // DHT 22 (AM2302), AM2321 *** default |
| 11 | +#ifndef USERMOD_DHT_DHTTYPE |
| 12 | +#define USERMOD_DHT_DHTTYPE 22 |
| 13 | +#endif |
| 14 | + |
| 15 | +#if USERMOD_DHT_DHTTYPE == 11 |
| 16 | +#define DHTTYPE DHT_TYPE_11 |
| 17 | +#elif USERMOD_DHT_DHTTYPE == 21 |
| 18 | +#define DHTTYPE DHT_TYPE_21 |
| 19 | +#elif USERMOD_DHT_DHTTYPE == 22 |
| 20 | +#define DHTTYPE DHT_TYPE_22 |
| 21 | +#endif |
| 22 | + |
| 23 | +// Connect pin 1 (on the left) of the sensor to +5V |
| 24 | +// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 |
| 25 | +// to 3.3V instead of 5V! |
| 26 | +// Connect pin 2 of the sensor to whatever your DHTPIN is |
| 27 | +// NOTE: Pin defaults below are for QuinLed Dig-Uno's Q2 on the board |
| 28 | +// Connect pin 4 (on the right) of the sensor to GROUND |
| 29 | +// NOTE: If using a bare sensor (AM*), Connect a 10K resistor from pin 2 |
| 30 | +// (data) to pin 1 (power) of the sensor. DHT* boards have the pullup already |
| 31 | + |
| 32 | +#ifdef USERMOD_DHT_PIN |
| 33 | +#define DHTPIN USERMOD_DHT_PIN |
| 34 | +#else |
| 35 | +#ifdef ARDUINO_ARCH_ESP32 |
| 36 | +#define DHTPIN 21 |
| 37 | +#else //ESP8266 boards |
| 38 | +#define DHTPIN 4 |
| 39 | +#endif |
| 40 | +#endif |
| 41 | + |
| 42 | +// the frequency to check sensor, 1 minute |
| 43 | +#ifndef USERMOD_DHT_MEASUREMENT_INTERVAL |
| 44 | +#define USERMOD_DHT_MEASUREMENT_INTERVAL 60000 |
| 45 | +#endif |
| 46 | + |
| 47 | +// how many seconds after boot to take first measurement, 90 seconds |
| 48 | +// 90 gives enough time to OTA update firmware if this crashses |
| 49 | +#ifndef USERMOD_DHT_FIRST_MEASUREMENT_AT |
| 50 | +#define USERMOD_DHT_FIRST_MEASUREMENT_AT 90000 |
| 51 | +#endif |
| 52 | + |
| 53 | +// from COOLDOWN_TIME in dht_nonblocking.cpp |
| 54 | +#define DHT_TIMEOUT_TIME 10000 |
| 55 | + |
| 56 | +DHT_nonblocking dht_sensor(DHTPIN, DHTTYPE); |
| 57 | + |
| 58 | +class UsermodDHT : public Usermod { |
| 59 | + private: |
| 60 | + unsigned long nextReadTime = 0; |
| 61 | + unsigned long lastReadTime = 0; |
| 62 | + float humidity, temperature = 0; |
| 63 | + bool initializing = true; |
| 64 | + bool disabled = false; |
| 65 | + #ifdef USERMOD_DHT_STATS |
| 66 | + unsigned long nextResetStatsTime = 0; |
| 67 | + uint16_t updates = 0; |
| 68 | + uint16_t clean_updates = 0; |
| 69 | + uint16_t errors = 0; |
| 70 | + unsigned long maxDelay = 0; |
| 71 | + unsigned long currentIteration = 0; |
| 72 | + unsigned long maxIteration = 0; |
| 73 | + #endif |
| 74 | + |
| 75 | + public: |
| 76 | + void setup() { |
| 77 | + nextReadTime = millis() + USERMOD_DHT_FIRST_MEASUREMENT_AT; |
| 78 | + lastReadTime = millis(); |
| 79 | + #ifdef USERMOD_DHT_STATS |
| 80 | + nextResetStatsTime = millis() + 60*60*1000; |
| 81 | + #endif |
| 82 | + } |
| 83 | + |
| 84 | + void loop() { |
| 85 | + if (disabled) { |
| 86 | + return; |
| 87 | + } |
| 88 | + if (millis() < nextReadTime) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + #ifdef USERMOD_DHT_STATS |
| 93 | + if (millis() >= nextResetStatsTime) { |
| 94 | + nextResetStatsTime += 60*60*1000; |
| 95 | + errors = 0; |
| 96 | + updates = 0; |
| 97 | + clean_updates = 0; |
| 98 | + } |
| 99 | + unsigned long dcalc = millis(); |
| 100 | + if (currentIteration == 0) { |
| 101 | + currentIteration = millis(); |
| 102 | + } |
| 103 | + #endif |
| 104 | + |
| 105 | + float tempC; |
| 106 | + if (dht_sensor.measure(&tempC, &humidity)) { |
| 107 | + #ifdef USERMOD_DHT_CELSIUS |
| 108 | + temperature = tempC; |
| 109 | + #else |
| 110 | + temperature = tempC * 9 / 5 + 32; |
| 111 | + #endif |
| 112 | + |
| 113 | + nextReadTime = millis() + USERMOD_DHT_MEASUREMENT_INTERVAL; |
| 114 | + lastReadTime = millis(); |
| 115 | + initializing = false; |
| 116 | + |
| 117 | + #ifdef USERMOD_DHT_STATS |
| 118 | + unsigned long icalc = millis() - currentIteration; |
| 119 | + if (icalc > maxIteration) { |
| 120 | + maxIteration = icalc; |
| 121 | + } |
| 122 | + if (icalc > DHT_TIMEOUT_TIME) { |
| 123 | + errors += icalc/DHT_TIMEOUT_TIME; |
| 124 | + } else { |
| 125 | + clean_updates += 1; |
| 126 | + } |
| 127 | + updates += 1; |
| 128 | + currentIteration = 0; |
| 129 | + |
| 130 | + #endif |
| 131 | + } |
| 132 | + |
| 133 | + #ifdef USERMOD_DHT_STATS |
| 134 | + dcalc = millis() - dcalc; |
| 135 | + if (dcalc > maxDelay) { |
| 136 | + maxDelay = dcalc; |
| 137 | + } |
| 138 | + #endif |
| 139 | + |
| 140 | + if (((millis() - lastReadTime) > 10*USERMOD_DHT_MEASUREMENT_INTERVAL)) { |
| 141 | + disabled = true; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + void addToJsonInfo(JsonObject& root) { |
| 146 | + if (disabled) { |
| 147 | + return; |
| 148 | + } |
| 149 | + JsonObject user = root["u"]; |
| 150 | + if (user.isNull()) user = root.createNestedObject("u"); |
| 151 | + |
| 152 | + JsonArray temp = user.createNestedArray("Temperature"); |
| 153 | + JsonArray hum = user.createNestedArray("Humidity"); |
| 154 | + |
| 155 | + #ifdef USERMOD_DHT_STATS |
| 156 | + JsonArray next = user.createNestedArray("next"); |
| 157 | + if (nextReadTime >= millis()) { |
| 158 | + next.add((nextReadTime - millis()) / 1000); |
| 159 | + next.add(" sec until read"); |
| 160 | + } else { |
| 161 | + next.add((millis() - nextReadTime) / 1000); |
| 162 | + next.add(" sec active reading"); |
| 163 | + } |
| 164 | + |
| 165 | + JsonArray last = user.createNestedArray("last"); |
| 166 | + last.add((millis() - lastReadTime) / 60000); |
| 167 | + last.add(" min since read"); |
| 168 | + |
| 169 | + JsonArray err = user.createNestedArray("errors"); |
| 170 | + err.add(errors); |
| 171 | + err.add(" Errors"); |
| 172 | + |
| 173 | + JsonArray upd = user.createNestedArray("updates"); |
| 174 | + upd.add(updates); |
| 175 | + upd.add(" Updates"); |
| 176 | + |
| 177 | + JsonArray cupd = user.createNestedArray("cleanUpdates"); |
| 178 | + cupd.add(clean_updates); |
| 179 | + cupd.add(" Updates"); |
| 180 | + |
| 181 | + JsonArray iter = user.createNestedArray("maxIter"); |
| 182 | + iter.add(maxIteration); |
| 183 | + iter.add(" ms"); |
| 184 | + |
| 185 | + JsonArray delay = user.createNestedArray("maxDelay"); |
| 186 | + delay.add(maxDelay); |
| 187 | + delay.add(" ms"); |
| 188 | + #endif |
| 189 | + |
| 190 | + if (initializing) { |
| 191 | + // if we haven't read the sensor yet, let the user know |
| 192 | + // that we are still waiting for the first measurement |
| 193 | + temp.add((nextReadTime - millis()) / 1000); |
| 194 | + temp.add(" sec until read"); |
| 195 | + hum.add((nextReadTime - millis()) / 1000); |
| 196 | + hum.add(" sec until read"); |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + hum.add(humidity); |
| 201 | + hum.add("%"); |
| 202 | + |
| 203 | + temp.add(temperature); |
| 204 | + #ifdef USERMOD_DHT_CELSIUS |
| 205 | + temp.add("°C"); |
| 206 | + #else |
| 207 | + temp.add("°F"); |
| 208 | + #endif |
| 209 | + } |
| 210 | + |
| 211 | + uint16_t getId() |
| 212 | + { |
| 213 | + return USERMOD_ID_DHT; |
| 214 | + } |
| 215 | + |
| 216 | +}; |
0 commit comments