Created
June 30, 2020 21:06
-
-
Save gsuberland/00d9cd763a6bc36aa3df7120e0b5941b to your computer and use it in GitHub Desktop.
ESP8266 DHT22 Environmental Sensors for use with Prometheus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
#include <DHT_U.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
const char* ssid = "ssid"; | |
const char* password = "password"; | |
const char* hostname = "esp_dht22_bedroom"; | |
#define DHTTYPE DHT22 | |
#define DHTPIN D4 | |
DHT_Unified dht(DHTPIN, DHTTYPE); | |
ESP8266WebServer server(9000); | |
void startWiFi() | |
{ | |
Serial.println(""); | |
Serial.println(F("Starting WiFi...")); | |
WiFi.begin(ssid, password); | |
WiFi.hostname(hostname); | |
Serial.println(F("Waiting for connection")); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.println(F(" ")); | |
Serial.println(F("Connected!")); | |
byte mac[6]; | |
WiFi.macAddress(mac); | |
Serial.print("MAC: "); | |
Serial.print(mac[5],HEX); | |
Serial.print(":"); | |
Serial.print(mac[4],HEX); | |
Serial.print(":"); | |
Serial.print(mac[3],HEX); | |
Serial.print(":"); | |
Serial.print(mac[2],HEX); | |
Serial.print(":"); | |
Serial.print(mac[1],HEX); | |
Serial.print(":"); | |
Serial.println(mac[0],HEX); | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP: "); | |
Serial.println(ip); | |
} | |
float convertCtoF(float c) { return c * 1.8 + 32; } | |
float convertFtoC(float f) { return (f - 32) * 0.55555; } | |
float computeHeatIndex(float temperature, float percentHumidity, | |
bool isFahrenheit) { | |
float hi; | |
if (!isFahrenheit) | |
temperature = convertCtoF(temperature); | |
hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + | |
(percentHumidity * 0.094)); | |
if (hi > 79) { | |
hi = -42.379 + 2.04901523 * temperature + 10.14333127 * percentHumidity + | |
-0.22475541 * temperature * percentHumidity + | |
-0.00683783 * pow(temperature, 2) + | |
-0.05481717 * pow(percentHumidity, 2) + | |
0.00122874 * pow(temperature, 2) * percentHumidity + | |
0.00085282 * temperature * pow(percentHumidity, 2) + | |
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2); | |
if ((percentHumidity < 13) && (temperature >= 80.0) && | |
(temperature <= 112.0)) | |
hi -= ((13.0 - percentHumidity) * 0.25) * | |
sqrt((17.0 - abs(temperature - 95.0)) * 0.05882); | |
else if ((percentHumidity > 85.0) && (temperature >= 80.0) && | |
(temperature <= 87.0)) | |
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2); | |
} | |
return isFahrenheit ? hi : convertFtoC(hi); | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
Serial.println(F("ESP8266 DHT22 Prometheus Sensor")); | |
delay(2000); | |
Serial.println(F("Starting DHT sensor...")); | |
dht.begin(); | |
sensor_t sensor; | |
dht.temperature().getSensor(&sensor); | |
Serial.println(F("------------------------------------")); | |
Serial.println(F("Temperature Sensor")); | |
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); | |
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); | |
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); | |
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C")); | |
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C")); | |
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C")); | |
Serial.println(F("------------------------------------")); | |
// Print humidity sensor details. | |
dht.humidity().getSensor(&sensor); | |
Serial.println(F("Humidity Sensor")); | |
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); | |
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); | |
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); | |
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%")); | |
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%")); | |
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%")); | |
Serial.println(F("------------------------------------")); | |
startWiFi(); | |
server.on("/metrics", handle_MetricsRequest); | |
server.on("/reset", handle_ResetRequest); | |
server.begin(); | |
} | |
void loop() | |
{ | |
if (WiFi.status() != WL_CONNECTED) | |
{ | |
startWiFi(); | |
} | |
server.handleClient(); | |
} | |
void handle_MetricsRequest() | |
{ | |
String content = ""; | |
bool got_temperature = false; | |
bool got_humidity = false; | |
float temperature = 0; | |
float humidity = 0; | |
sensors_event_t event; | |
dht.temperature().getEvent(&event); | |
if (isnan(event.temperature)) | |
{ | |
Serial.println(F("Error reading temperature!")); | |
} | |
else | |
{ | |
Serial.print(F("Temperature: ")); | |
Serial.print(event.temperature); | |
Serial.println(F("°C")); | |
content += "esp_dht_temperature_c " + String(event.temperature) + "\n"; | |
got_temperature = true; | |
temperature = event.temperature; | |
} | |
dht.humidity().getEvent(&event); | |
if (isnan(event.relative_humidity)) | |
{ | |
Serial.println(F("Error reading humidity!")); | |
} | |
else | |
{ | |
Serial.print(F("Humidity: ")); | |
Serial.print(event.relative_humidity); | |
Serial.println(F("%")); | |
content += "esp_dht_relative_humidity " + String(event.relative_humidity) + "\n"; | |
got_humidity = true; | |
humidity = event.relative_humidity; | |
} | |
if (got_temperature && got_humidity) | |
{ | |
float heatIndex = computeHeatIndex(temperature, humidity, false); | |
if (isnan(heatIndex)) | |
{ | |
Serial.println(F("Error computing heat index!")); | |
} | |
else | |
{ | |
Serial.print(F("Heat index: ")); | |
Serial.print(heatIndex); | |
Serial.println(F("°C")); | |
content += "esp_dht_heat_index " + String(heatIndex) + "\n"; | |
} | |
} | |
server.send(200, "text/plain", content); | |
} | |
void handle_ResetRequest() | |
{ | |
Serial.println("Resetting..."); | |
Serial.println(); | |
Serial.flush(); | |
delay(100); | |
ESP.restart(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment