ESP8266 + DHT11 weather station

Weather Station Device

In my previous article about Weather Station I presented frontend application to display weather station sensor data. Today I would like to present my own weather station project based on ESP8266 and DHT11/22.

Let’s start form the beginning. Some time ago I discovered ESP8266-01 module, which allows to connect to my WiFi network, build some small server and has two I/O pins. My first idea was to build some simple weather stations in each my room which allows me in the future control my heating devices. Hmm… good idea, but project will be to big do that in one step so I decided to split it. First step – weather stations, second step – heating control.

Weather Station

Main assumptions of that project is:

  • use ESP8266-01 as server and micro controller
  • use DHT11/DHT22 as sensor device
  • display current data sensor as HTML page
  • create API to read sensor data
  • save data for synchronization

I add in advance that after few days I discovered that ESP8266-01 module has some disadvantages like:

  • needs additional USB-UART cable
  • needs change cable connection after upload program
  • needs additional power module to work as single device

After quick research I found ESP8266 Nodemcu v3 module which solves all above problems at once and also gives much more opportunities than ESP8266-01 module. Of course this module is more expensive but it is worth that.

I use also DHT11/DHT22 sensors because they are cheap. If someone has experience with other type of sensors there is no problem to change my program and use another library.

All below code you are able to see on github.

Program files

To create more readable program I split one file into fives, to separate functions by they usage. Because of that we have:

  • weather_station.ino – main program file
  • dht.ino – functions correlated with DHT sensors
  • file_operations.ino – functions responsible for read and write sensor data to flash memory file
  • server.ino – all functions that response on user requests to the server
  • utils.ino – other not classified functions

The important part of the file is configuration part.

#define DHTTYPE DHT22                 // Sensor type DHT11 or DHT22
#define DHTPin D4                     // PIN to which is connected 
                                      // sensor

#define TIMES_PER_HOURE 2             // number of times per hour to 
                                      // read data from sensor

#define DATA_FILE "/data.csv"         // place to store data file
#define SSID "YOUR_WIFI_ID"           // put your WIFI SSID  
#define PASSWORD "YOUR_WIFI_PASSWORD" // put your WIFI password

#define PAGE_TITLE "PAGE_TITLE"       // put your page title - device
                                      // name 

#define LOG_ITEMS 150                 // number of log data records

Here we can specify:

  • what kind of sensor device we use (DHTTYPE): DHT11 or DHT22
  • to which pin of our ESP8266 we have connected our sensor data pin (DHTPin)
  • number of times per hour when server automatically check sensor data and store it the data file (TIMES_PER_HOURE) – I recomend 2 as it is set but more than 4 in home or flat environment is not necessary
  • name of the data file (DATA_FILE)
  • name of your WiFi network (SSID)
  • password of your WiFi (YOUR_WIFI_PASSWORD)
  • page title/device name, to recognize it as you have more than one (PAGE_TITLE)
  • number of line of logs stored in data file (LOG_ITEMS)

Next part of file is modules configuration:

// Initialize Http Server
ESP8266WebServer server(80);

// Initialize DHT sensor.

// Initialize Time
NTPClient timeClient(ntpUDP);

float periodBetweenMeasure = 60 * 60 / TIMES_PER_HOURE;

int lastSec = 0; // timestamp in which was made last measure

Then we have setup function.

void setup() {

  pinMode(DHTPin, INPUT);



First of all we initialize Serial communication to able displaying some debug messages, then we set sensor PIN and initialize DHT library.

  Serial.println("Connecting to ");

  //connect to your local wi-fi network
  WiFi.begin(SSID, PASSWORD);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");

Then we start connecting to WiFi. Program try to connect to network many times taking 1 seconds delay. As the connection is established program display message that is connected and also display IP of the device.

After that we configure our server passing different handle request function for different URLs.

// display HTML page with current sensor data and save it do DATA file
server.on("/", handle_OnConnect);                         

// return JSON with current sensor data and save it to DATA file
server.on("/api", handle_OnApiCall);                      

// clear DATA file
server.on("/api/file/clear", handle_OnApiClearDataFile);  

// return JSON with all data stored in DATA file
server.on("/api/file/sync", handle_OnSync);     
// error page

Then we start server

Serial.println("HTTP server started");

And start NTP client passing our +/- GMT in seconds (my is GMT+1, so I pass 3600)


At the end we check if files system can be mounted.

if (!SPIFFS.begin()) {
  Serial.println("Failed to mount file system");

After whole configuration and setup we run our main program function

void loop() {
  while (!timeClient.update()) {



Its responsibility is to:

  • update time if necessary
  • call saveData – read data from sensor and add it to log file
  • listen on server user requests

File contains function related to DHT sensor device.

  • saveData – function is responsible for get current temperature and humidity and save it in data file in flash memory
  • parseDhtData(float Temperature, float Humidity) function return string of timestam|temperature|humidity

This file contains all functions responsible for operation on data file:

  • readFile() – read data file and return its content
  • clearFile() – clear data file content
  • getLastNRowsFromString(String message, String arr[]) – fill arr with last N rows of data file

File contains all functions that are responsible for create HTTP response for server requests:

  • handle_OnConnect() – return HTML page
  • handle_OnSync() – return JSON with data file content
  • handle_OnApiCall() – return JSON response with current sensor values and also save them in data file
  • handle_OnApiClearDataFile() clear data file
  • handle_NotFound() – return error response when someone call URL that does not exist
  • SendHTML(float TempCstat, float Humiditystat) generate HTML page and return it as a String


Generally that is all. I fulfill all my requirements. Now I have 3 different device working with DHT11 in different rooms in my flat. the third one is connected to DHT22. I planned to set up it outside the flat, but I still looking for good place.

All my devices are powered by old smartphones chargers (1A or even sometimes less). This is another advantage ESP8266 Nodemcu v3 over ESP8266-01. The same micro USB is used to upload software and finally power up device.

Of course this is not a finish of my weather station project. As i mention previously you can find on my page article about Angular module which allows to display all that information which is aggregate by devices described in this articles. But you can probably asked yourself how it is possible to communicate between angular and such devices – it is possible, but in my case I use my smart-home-server and I add to it functionality which download data from devices and store it in database, but this is story for another article.

So if you have any questions or any ideas I’m more than happy to hear it, so do not to hesitate to contact with me.

ESP8266 + DHT11 weather station
Przewiń na górę