ESP8266 weather station using Arduino IDE

Today we are making really cool weather station using ESP8266 (NodeMCU) and DHT11, rain sensor. In this project, we’ll learn how to make a DIY fully featured weather station using an ESP8266 and sensors. There are two types of weather station, one which is having own sensors and second type of weather station is where we pull data from the weather station servers. In this tutorial we are looking at both types of weather stations.

NodeMCU Weather Station

Components Required

  1. ESP8266 (NodeMCU)
  2. DHT11
  3. Connecting Wires
  4. 0.96″ OLED Display (Required for second type weather station)

Introduction

What is Weather Station ?

A weather station is a device that collects data related to the weather and environment using many different sensors. Weather stations are also called weather centers, personal weather stations, professional weather stations, home weather station, weather forecaster and forecasters.

Weather stations sensors may include a thermometer (LM35 or DHT11) to take temperature readings, a barometer (BMP180) to measure the pressure in the atmosphere, as well as other sensors to measure rain, wind, humidity and more. Weather stations range from simple analog technology to digital technology. Some even connect to your computer or the internet, so the data collected can be analyzed using weather station software.

Types of Sensors used for Weather Station

The following is a list of measurement devices that are used in weather stations:

Thermometer (LM35) – A thermometer measures temperature. You can measure both the temperature indoors and outdoors, record highs and lows, show trend arrows to indicate temperature rising or falling, and even predict short-term future temperature ranges.

You can read this example code for LM35 using ESP8266

LM35 Temperature Sensor

Hygrometer (DHT11)- A hygrometer measures relative humidity. Relative humidity is the quantity or percentage of water vapor (water in gas form) in the air. Humidity influences environmental factors and calculations like precipitation, fog, dew point and heat index. In addition, maintaining proper humidity indoors has implications for your health and home.

You can read ESP8266 Reading data from DHT-11

DHT11 Pinout for ESP8266

Barometer (BMP180) – A barometer measures atmospheric pressure. A barometer can help to forecast upcoming weather based on the changes it measures in the atmospheric pressure. You can create history chart or a pressure trend arrow so you can easily track changes, like a pressure drop. ESP8266 DHT11 Charts example is given here.

BMP180 Barometric Pressure/Temperature/Altitude Sensor
BMP180 Barometric Pressure/Temperature/Altitude Sensor

Anemometer – An anemometer measures how fast the wind is blowing, or wind speed. ESP8266 weather stations can display wind speed in MPH, KPH or knots, and record current, peak and average wind speed readings.

NodeMCU Anemometer Wind Speed Sensor
Anemometer Wind Speed Sensor

Wind Vane – A wind vane, or weather vane, is an instrument that determines which direction the wind is blowing. To display wind direction you can use rotary encoder or magnetic compass sensor with ESP8266.

Wind Vane Arduino
Wind Vane

Rain Gauge – A rain gauge measures rainfall or liquid precipitation. This weather stations include rainfall alerts to notify you when a rain event has begun, or to alert you of potential flood conditions.

Rain Sensor Module
Rain Sensor Module

ESP8266 Based Weather Station

In first part we make weather station using DHT-11 and Rain Sensor. So this data is sent to client (web browser). In this we make web server on ESP8266. If you are looking for much advanced weather station using BMP180, DHT11 and Rain sensor read here.

Circuit Diagram of Weather Station

Connect rain sensor to A0 Pin of NodeMCU through a voltage divider, ESP8266 is having 1V input on ADC. Connect DHT11 to D5 of NodeMCU.

ESP8266 Weather Station Circuit Diagram

NodeMCU Code for Weather Station

Program is divided in two parts. first part contains ESP8266 WiFi and Hardware related functions i.e. our main.ino file. Second part is HTML and user interface GUI. it is index.h file.

before uploading program to ESP. Update your SSID and Password in code.

main.ino

index.h

Results and Testing

After uploading open serial monitor and get the ip address.

Open IP address in Web Browser.  After opening it in web browser, you will see that on board blue LED will Blink. this led toggles when it receives http request from web browser.

In program we are sending ajax request to update the page without refreshing it. You can read more on it here.

Weather Station Server

How Weather Station program works ?

Now lets see the complete working of weather station program and circuit.

Complete project is consists of Hardware and Software part. First we discuss hardware part.

Weather Station Hardware

Hardware part is simple we are using only two sensors DHT11 and Rain Sensor. You can add more sensors like BMP180 barometric pressure sensor.

Rain sensor analog output is connected to Analog input of ESP8266 through a voltage divider. which drops the output of rain sensor to suit ESP8266 analog input maximum range.

DHT11 gives us humidity and temperature readings. It is digital sensor gives output in serial stream of data. For this sensor interfacing we are using DHTesp library which takes care of all communication with DHT11 sensor. DHT11 is connected to NodeMCU pin D5 which is GPIO14.

Weather Station Software Working

Software consists of following parts.

  1. Connecting to WiFi
  2. Server Creation
  3. Getting Data from Sensors
  4. Sending GUI to user
  5. Updating Sensors values on web page

Connecting to WiFi

Connection to you wifi is made by ESP8266 using below code. First we define wifi ssid and password.

Then we try to connect with wifi using WiFi.begin(ssid, password) and wait until connection successful using while (WiFi.status() != WL_CONNECTED).

Once we connect to wifi successfully we print IP address assigned by you WiFi Router on Serial monitor using WiFi.localIP() function.

Next part is to create web server on ESP8266. for this we are including web server library  #include <ESP8266WebServer.h>

Then we create server instance using below function. Web uses default port 80, so we defined it as port 80.

ESP8266WebServer server(80); //Server on port 80

After this in setup() we create client request handling functions, one for displaying GUI and another which sends the sensor data to user using AJAX and JSON. AJAX is part of web page. You can read more on AJAX here and JSON parsing here.

When user enters ESP8266 IP it requests root page. That request is served by handleRoot() function. It sends web page (HTML) to user (web browser).Once the web page is loaded it contains javascript with AJAX code which sends data request to ESP8266 webserver at /readADC. If you enter IP with readADC request as shown below. ESP sends sensor data in JSON format.

This readADC request is handled by handleADC() function. as shown below. Handled ADC sends data first to web client and after that it reads DHT11 sensor to prevent data lags. In this you can clearly see data variable holds the JSON of sensor values. Many escape characters are uses as we are using double quotes in JSON. You can read more on how to use double quotes inside the string without compilation errors.

Now let’s move to web page i.e. HTML and Java script code part. Which is present in index.h file. In java script we access HTML elements using id’s.

Ajax java script part of the web page. We call drawClock() function at every two seconds. using setInterval(drawClock, 2000); function.

In drawClock() Function we have AJAX part which sends the GET request to the ESP at  readADC. when web browser (java program) receives it response text i.e. JSON. it calls onreadystatechange function. in that we confirm that the request is OK (200). if it is OK then we get the responseText i.e. JSON and then using javascript json parsing JSON.parse(txt); we get all our sensor data in json objects. This json object data is then updated in web page dynamically (without page refresh) using document.getElementById(“id of html element“).innerHTML = sensor data.

This is how all works. GUI is created using CSS and HTML. which is not covered here as its more web development related topic.

ESP8266 Based Weather Station using  server data

In this we are not using any sensor but we get weather data from local weather station server. To display it we need a display. This type of application is like you have small device on table which request weather data to weather stations and continuously updates on small display. Advantage of this type of weather station is you get weather forecast.

Before you start read more on OLED interfacing with ESP8266.

Weather Station Connections

In this we have only OLED 0.96″ Display and NodeMCU.

NodeMCU OLED Weather Station

Arduino IDE Code for Weather Station using NodeMCU

Before we start coding we need few libraries to installed.

  1. OLED See Example
  2. JSON See Example

Read this for more on how to send HTTPS request ?

Before uploading code get your location lat lon. you can search in google and replace it

String Link = “/api/current?lat=18.53&lon=73.86“;

Then enter your wifi SSID and Password then upload the code.

Results and Testing

You can get lot of information about weather. I have taken it all information in variable but only location, temperature, humidity and pressure is displayed on display. I accidentally dropped OLED and it is showing corrupted text. So graphics and other things are pending.

But finally I can say that this is the simplest example on internet. Here I am using fcc weather APIs. All other examples on internet use openWeather and some other paid services require registrations. You don’t have to register to any website. simply put lat long and it works.

Read More

 

Leave a Reply