ESP8266 DHT Sensor Server
This firmware for ESP8266 provides a simple website showing the current temperature and humidity measured by the attached DHT or AM2302 sensor. In addition a MH-19ZB can be added for CO2 measurements or a PMS3003 for dust/particulate matter measurements.
If you are using an influxDB for storing your measurements this firmware also allows to enter values to the influxDB via the http API.
Furthermore the firmware can be updated via Arduino OTA and the configuration can be provided via a rather failsave configuration mode.
Dependencies
The following arduino libraries are required:
General
- ArduinoOTA.h
- ESP8266WiFi.h
- ESP8266mDNS.h
- WiFiClient.h
- ESP8266WebServer.h
- SPI.h
- FS.h
- ArduinoJson.h
When using a DHT22 sensor
- DHT.h (Adafruit, requires "Adafruit Unified Sensors" library)
When using a BMP sensor
- Adafruit BusIO (Wire.h)
- Adafruit BMP280 Library
When using a BME sensor
- Adafruit BusIO (Wire.h)
- Adafruit BME280 Library
When using a SHT sensor
- Adafruit BusIO (Wire.h)
- Adafruit STH31 Library
When using a MH-19ZB CO2 sensor
- SoftwareSerial.h
- MHZ19.h
When using dust sensor PMS3003 or compatible
- SoftwareSerial.h
- PMS.h
When using influxDB
- ESP8266HTTPClient.h
Configuration
The configuration is mainly done by a JSON config file located in the SPIFFS of the ESP. More basic configuration like used sensors and the influx capabilities are possible via preprocessor directives.
Configuring sensor types and influxDB support
Please modify lines 10 to 12 according to your needs and comment or undefine lines if you don't want or need either DHT, CO2 or influxDB support.
The pins used for the sensors as well as the DHT type can be changed in line 44/45 (DHT) and line 64/65 (MH-19ZB).
Upload or update the configuration
There are to options two upload the configuration.
Option 1 is to connect the ESP via serial connection to your computer and upload the config file to the ESP via the Arduino ESP8266 filesystem uploader. When using this method be sure to call your file config.json
and place it directly in the root of your SPIFFS (respectively your data
folder).
Option 2 is to use the config mode. If you have flashed the firmware and no config file was transferred to the SPIFFS, the ESP will start up a wifi access point named Weather ESP
with the password 42esp23!
. Connect to that wifi network and visit http://192.168.4.1. Here you can either format the SPIFFS to remove all files stored in the ESPs SPIFFS or upload a new config file (it is not required to name it config.json
as it will be renamed automatically, this might increase you convinienc in organizing different configurations for multiple sensor ESP on your computer).
After the file has been uploaded the ESP needs to be rebooted, either by resetting it with the restart
button in the config mode interface, resetting it with the hardware reset button or disrupting the power supply.
The configuration mode can also be entered by visiting <your-sensor-name>/config
in case a valid network configuration was already provided. In case of a valid network configuration the config mode interface is available via your normal network as well as via the configuration hot spot.
Available Parameters
All parameters are required (beside the influx
block if you don't use the influxDB capabilities). Most of the them should be self explaining. In Addition to that:
-
wifiTimeout
is the time the ESP tries to connect to the specified network. If the connection can not be established within that time (in milliseconds), the ESP will fall back to the config mode and open the configuration hot spot again. -
mdnsName
is the name you sensor will have in your local network. -
sensorLocation
the sensor will specify the tag keylocation
to the measurement sent to influxDB.sensorLocation
is the value of the tag keylocation
.
Example
{
"otaPass": "<your-ota-password",
"network": {
"wifiSsid": "<your-wifi-ssid>",
"wifiPass": "<your-wifi-password>",
"wifiTimeout": 10000,
"mdnsName": "<your-sensor-name>"
},
"influx": {
"https": false,
"host": "<your-influx-host>",
"measurementsDb": "<your-influx-measurements-db-name>",
"eventsDb": "<your-influx-events-db-name>",
"logInterval": 10000,
"sensorLocation": "<your-sensor-location>"
}
}
REST API
If some sensors are not configured to use, the according interface will not provide any data.
/temp
Delivers the current temperature as plain text, with .
as decimal point.
/humidity
Delivers the current humidity as plaint text. The humidity value has no decimals, if a DHT sensor is used.
/pressure
Delivers the current pressure as plaint text, with .
as decimal point.
/co2
Delivers the current CO2 measurement in ppm as plaint text, as well as - separated by a ,
- the raw sensor value from the MH-Z19B sensor as the second value.
/dust
Delivers the current dust measurements in µg/m³ separated by a ,
in the follwing order PM1.0,PM2.5,PM10
.
/json
Delivers all sensor data as JSON-object. The provided properties are depended on the configured sensors.
/config
Starts the configuration mode.
InfluxDB
If influxDB support is enabled the sensor will write the measurements directly to your influxDB using the influxDB http API.
The measurements are called temp
, hum
, pressure
and co2
and are enriched with a tag key location
. A timestamp is not provided, this should be added by the influxDB server.
Furthermore the ESP writes an event to an influxDB event database when being restarted (and having a working network connection). This can for example be used for annotations in Grafana. An event entry contains the title
"Sensor reset", the text
"Sensor <your-sensor-name> has been reseted" and the tags
"sensor" and "reset". Further events might be implemented in future.
Querying Intervall
Due to the slowness of the DHT sensors the firmware internally queries the sensor at a maximum interval of 2000ms. The web interface actualizes the shown values every 5000ms. The MH-19ZB sensor has a preheating phase of 180s, readings read before might be inacurate. All sensor values are pulled as fast as possible internally. The REST interface provides the last value read from a sensor at the time of the request.
Wiring example with WeMOS D1 mini and DHT22
Required parts
- DHT22/AM2302 sensor
- 4.7kΩ resistor
- WeMOS D1 mini
- some wires
Code changes
To exactly match this example you need to change the DHTPIN definition in line 45 (WeMOS pin D5 is equal to pin 14 in arduino). For further pin mappings see http://escapequotes.net/wemos-d1-mini-pins-and-diagram/.