Replace DHT sensor library
This commit is contained in:
parent
45ae85594b
commit
f90672fda2
|
@ -14,8 +14,9 @@ DHT sensor: [Wemos DHT Shield](https://wiki.wemos.cc/products:retired:dht_shield
|
||||||
- Download and install.
|
- Download and install.
|
||||||
- [esp8266 library for Arduino](https://github.com/esp8266/Arduino#installing-with-boards-manager)
|
- [esp8266 library for Arduino](https://github.com/esp8266/Arduino#installing-with-boards-manager)
|
||||||
- See the instructions on the page.
|
- See the instructions on the page.
|
||||||
- [Adafruit DHT sensor library](https://github.com/adafruit/DHT-sensor-library)
|
- [DHT sensor library for ESPx](https://github.com/beegee-tokyo/DHTesp)
|
||||||
- Install using the library manager, including all dependencies.
|
- Install using the Arduino library manager.
|
||||||
|
- The Adafruit one didn't work for the WEMOS D1.
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
// Debug mode is enabled if not zero
|
// Debug mode is enabled if not zero
|
||||||
#define DEBUG_MODE 0
|
#define DEBUG_MODE 0
|
||||||
// DHT sensor type: DHT11, DHT21 or DHT22
|
// DHT sensor type
|
||||||
#define DHT_TYPE DHT11
|
#define DHT_TYPE DHT11
|
||||||
// DHT pin
|
// DHT pin
|
||||||
#define DHT_PIN D4
|
#define DHT_PIN 2
|
||||||
// HTTP server port
|
// HTTP server port
|
||||||
#define HTTP_SERVER_PORT 80
|
#define HTTP_SERVER_PORT 80
|
||||||
// HTTP metrics endpoint
|
// HTTP metrics endpoint
|
||||||
|
@ -14,8 +14,8 @@
|
||||||
#define TEMPERATURE_CORRECTION_OFFSET 0
|
#define TEMPERATURE_CORRECTION_OFFSET 0
|
||||||
// Humidity offset in percent
|
// Humidity offset in percent
|
||||||
#define HUMIDITY_CORRECTION_OFFSET 0
|
#define HUMIDITY_CORRECTION_OFFSET 0
|
||||||
// How long to cache the sensor results
|
// How long to cache the sensor results, in milliseconds
|
||||||
#define READ_INTERVAL 2000
|
#define READ_INTERVAL 5000
|
||||||
// How many times to try to read the sensor before returning an error
|
// How many times to try to read the sensor before returning an error
|
||||||
#define READ_TRY_COUNT 5
|
#define READ_TRY_COUNT 5
|
||||||
// WiFi SSID (required)
|
// WiFi SSID (required)
|
||||||
|
|
27
src/src.ino
27
src/src.ino
|
@ -1,7 +1,7 @@
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include <DHT.h>
|
#include <DHTesp.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
@ -20,12 +20,12 @@ void read_sensors(boolean force=false);
|
||||||
bool read_sensor(float (*function)(), float *value);
|
bool read_sensor(float (*function)(), float *value);
|
||||||
void log(char const *message, LogLevel level=LogLevel::INFO);
|
void log(char const *message, LogLevel level=LogLevel::INFO);
|
||||||
|
|
||||||
DHT dht_sensor(DHT_PIN, DHT_TYPE);
|
DHTesp dht_sensor;
|
||||||
ESP8266WebServer http_server(HTTP_SERVER_PORT);
|
ESP8266WebServer http_server(HTTP_SERVER_PORT);
|
||||||
|
|
||||||
float humidity, temperature;
|
float humidity, temperature;
|
||||||
char str_humidity[10], str_temperature[10];
|
char str_humidity[10], str_temperature[10];
|
||||||
unsigned long previousReadTime = 0;
|
uint32_t previous_read_time = 0;
|
||||||
|
|
||||||
void setup(void) {
|
void setup(void) {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
@ -36,7 +36,8 @@ void setup(void) {
|
||||||
|
|
||||||
void setup_dht_sensor() {
|
void setup_dht_sensor() {
|
||||||
log("Setting up DHT sensor ...");
|
log("Setting up DHT sensor ...");
|
||||||
dht_sensor.begin();
|
dht_sensor.setup(DHT_PIN, DHTesp::DHT_TYPE);
|
||||||
|
delay(dht_sensor.getMinimumSamplingPeriod());
|
||||||
// Test read
|
// Test read
|
||||||
read_sensors(true);
|
read_sensors(true);
|
||||||
log("DHT sensor ready.");
|
log("DHT sensor ready.");
|
||||||
|
@ -76,34 +77,34 @@ void handle_http_home_client() {
|
||||||
"Project: https://github.com/HON95/prometheus-esp8266-dht-exporter\n"
|
"Project: https://github.com/HON95/prometheus-esp8266-dht-exporter\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Usage: " HTTP_METRICS_ENDPOINT "\n";
|
"Usage: " HTTP_METRICS_ENDPOINT "\n";
|
||||||
http_server.send(200, "text/plain", response);
|
http_server.send(200, "text/plain; charset=utf-8", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_http_metrics_client() {
|
void handle_http_metrics_client() {
|
||||||
read_sensors();
|
read_sensors();
|
||||||
char response[100];
|
char response[100];
|
||||||
snprintf(response, 100, "Temperature: %s\nHumidity: %s", str_temperature, str_humidity);
|
snprintf(response, 100, "Temperature: %s\nHumidity: %s", str_temperature, str_humidity);
|
||||||
http_server.send(200, "text/plain", response);
|
http_server.send(200, "text/plain; charset=utf-8", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_sensors(boolean force) {
|
void read_sensors(boolean force) {
|
||||||
// TODO remove caching, the library already does it
|
uint32_t min_interval = max(dht_sensor.getMinimumSamplingPeriod(), READ_INTERVAL);
|
||||||
unsigned long currentTime = millis();
|
uint32_t current_time = millis();
|
||||||
if (!force && currentTime - previousReadTime < READ_INTERVAL) {
|
if (!force && current_time - previous_read_time < min_interval) {
|
||||||
log("Sensors were recently read, will not read again yet.", LogLevel::DEBUG);
|
log("Sensors were recently read, will not read again yet.", LogLevel::DEBUG);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
previousReadTime = currentTime;
|
previous_read_time = current_time;
|
||||||
|
|
||||||
read_humidity_sensor();
|
read_humidity_sensor();
|
||||||
read_temperature_sensor();
|
read_temperature_sensor();
|
||||||
// TODO float hic = dht.computeHeatIndex(t, h, false);
|
// TODO float hic = dht.computeHeatIndex(temperature, humidity, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_humidity_sensor() {
|
void read_humidity_sensor() {
|
||||||
log("Reading humidity sensor ...", LogLevel::DEBUG);
|
log("Reading humidity sensor ...", LogLevel::DEBUG);
|
||||||
bool result = read_sensor([] {
|
bool result = read_sensor([] {
|
||||||
return dht_sensor.readHumidity();
|
return dht_sensor.getHumidity();
|
||||||
}, &humidity);
|
}, &humidity);
|
||||||
if (result) {
|
if (result) {
|
||||||
humidity += HUMIDITY_CORRECTION_OFFSET;
|
humidity += HUMIDITY_CORRECTION_OFFSET;
|
||||||
|
@ -117,7 +118,7 @@ void read_humidity_sensor() {
|
||||||
void read_temperature_sensor() {
|
void read_temperature_sensor() {
|
||||||
log("Reading temperature sensor ...", LogLevel::DEBUG);
|
log("Reading temperature sensor ...", LogLevel::DEBUG);
|
||||||
bool result = read_sensor([] {
|
bool result = read_sensor([] {
|
||||||
return dht_sensor.readTemperature(false);
|
return dht_sensor.getTemperature();
|
||||||
}, &temperature);
|
}, &temperature);
|
||||||
if (result) {
|
if (result) {
|
||||||
temperature += TEMPERATURE_CORRECTION_OFFSET;
|
temperature += TEMPERATURE_CORRECTION_OFFSET;
|
||||||
|
|
Loading…
Reference in New Issue