Categories
Blog My projects

Simple home weather station based on ESP8266 NodeMCU

Spread the love

Simple home weather station on ESP8266 NodeMCU with weather forecast and Internet access – before we begin I’d like to explain what was implemented in this project. Elementary home weather station, which displays the current temperature and humidity, atmospheric pressure and altitude (very approximate) above sea level, also it is available via local Wi-Fi network and even from the Internet, can be easily implemented using the microcontroller ESP8266 NodeMCU. One of the biggest advantages of this microcontroller is the presence of a built-in Wi-Fi module, which allows you to play Internet of Things without leaving home. Also in this project, we will obtain information about the weather forecast and display it with the sensors data on the web server implemented on ESP8266.

Used equipment:

  1. Microcontroller ESP8266 (model ESP-12E NODEMCU);
  2. Pressure and temperature sensor BMP280;
  3. Digital thermometer DS18B20;
  4. DHT11 temperature and humidity sensor;
  5. Breadboard, conductors.

Setting up and programming the ESP8266 microcontroller will not be considered here, because the Internet is full of all sorts of instructions on how to do it. I programmed my NODEMCU through the Arduino IDE. I will also not dwell on the connection of the necessary libraries. This publication is just a compilation of a few interesting articles, links to which can be found below, with some refinement, and most importantly with the addition of some very useful interesting features such as access to sensors data from the Internet and weather forecasting using API calls to the weather service which provides access to meteorological data from around the world.

Let’s divide the process of building a weather station into several stages:

  1. Connecting sensors to the microcontroller;
  2. Microcontroller programming;
  3. Connecting the microcontroller to a local Wi-Fi network and displaying sensor readings by calling the ESP8266 with IP address;
  4. Opening access to sensors data  from the Internet;
  5. Displaying the weather forecast.

1. Connecting sensors to the microcontroller

In general, BMP280, DS18B20, and DHT11 are suitable as temperature sensors, but the measurement accuracy is different. Thus I used DS18B20 directly to determine the air temperature, DHT11 to measure the humidity, and BMP280 module to determine the atmospheric pressure and altitude (which, in turn, is calculated from the temperature and pressure). The connection diagram becomes clear if you look at the program code.

BMP280 sensor → microcontroller:
#define BMP_SCK D1
#define BMP_MISO D4
#define BMP_MOSI D2
#define BMP_CS D3

Digital thermometer DS18B20 → microcontroller:
#define ONE_WIRE_BUS D5
DHT11 sensor → microcontroller:
uint8_t DHTPin = D6;

2. Programming of the ESP8266 microcontroller

In order to program the weather station I used the following libraries:
#include <Arduino_JSON.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHT.h"

The full program’s source code may be found by the link to GitHub at the end of the publication. The code is ready for use, but don’t forget to change the name and password of the Wi-Fi network to connect the microcontroller, as well as the coordinates of your city, and your API key for weather forecast:

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Replace the next line with your API Key
#define ONECALLKEY "REPLACE_WITH_YOUR_API_KEY"
// Sample Lat and Lon for Lviv, UA
float myLatitude = 49.8383; //<-----------------------------in range to use GPS coordinates
float myLongitude = 24.0232; // Coordinates for Lviv, UA. Change these for your city

3. Connecting ESP8266 microcontroller to a local Wi-Fi network and displaying results by IP address

After connecting all the sensors and downloading the program code to the microcontroller memory, it should automatically connect to the Wi-Fi network specified in the ssid. In most cases, the router will dynamically issue a specific local IP address to the microcontroller using a DHCP server. You can find out in two ways: open the Serial Monitor (if you use the Arduino IDE) or view the client’s list in your router admin console.

Serial Monitor in the Arduino IDE:

Microcontroller IP address in the Wi-Fi router settings:

If everything is done correctly, going to this address from any device connected to our local Wi-Fi network, we will see something like this:

4. Opening access to sensors data from the Internet

We are now making our weather station accessible from the Internet. If you do not have a dedicated static IP address (most likely you do not have it), you will have to use services such as DynDNS or NoIP. In my case, I used NoIP (https://www.noip.com/). These services are necessary in order to fix the router port we opened at the weather station’s IP address with a static domain name. First, you need to open port 80 for the microcontroller IP address in the router settings. In routers from different manufacturers, this process may be different, just as the names of the menu settings may differ, but usually, it will be something like Port forwarding or Virtual servers. You can simply google or look in the datasheet specifically for your model of network equipment. In my TP-Link it looks like this:

Then we have to register in the NoIP service and create a test domain for our weather station. The IP / Target field will display your router network address:

Thanks to the actions performed, we can now apply for a created domain name from the Internet from anywhere in the world and get data from our simple home weather station. It looks something like this:

5. Displaying the weather forecast

You can easily download weather forecasts from the Internet using services such as https://openweathermap.org/. After registration on the website, you can get an API key in order to make HTTP requests and receive information from the server about the current status and weather forecast. You can use the default key or generate a new one:

You can read more about the OpenWeatherMap API and query parameters in their documentation (https://openweathermap.org/api). openweathermap.org allows you to query their API free of charge for a limited number of times a day. Limits can be extended by a paid subscription, but, honestly, the free version is more than enough for home experiments (60 calls per minute). You can also get both the current weather and a fairly detailed forecast. By the way, there are other libraries for working with their API, but the method described here turned out to be the easiest for me personally. if you realized it otherwise, it would be interesting if you share your experience in the comments.

The way I implemented getting the weather forecast and displaying it on the weather station server is quite clear from the program code. I will only emphasize that it is important to convert JSON objects after parsing the server API response into a type suitable for processing and output – here the method JSON.stringify () of the Arduino_JSON library came in handy for me), so here I will give only a screenshot with my work results:

The simple home weather station I built is shown in the photo below. In general, you may connect more sensors, add an outlet-free power supply, and put it in a nice case. In my future publications, I plan to implement similar meteorological stations (and not only them) on the basis of the Arduino microcontroller platform and the Raspberry Pi single-board mini-computer. With the Raspberry Pi, I think it will be possible to significantly expand the project functionality. Thank you for your attention! Feel free to share your thoughts, ideas, and projects in the comments. I will be also grateful for the subscription in any convenient way, so you will not miss my new publications. I’m sure some of them may be quite interesting. Good luck with your projects!

weather station

You can find the full program code on GitHub:
https://github.com/OlehBoreiko/academicfox.com/blob/master/home_weather_station_forecaster_NodeMCU.ino

You may find similar publications with the following links:
https://theiotprojects.com/bme280-based-mini-weather-station-esp8266-esp32/
https://www.circuitschools.com/interfacing-bmp280-with-esp8266-on-i2c-with-errors-and-solutions/
https://randomnerdtutorials.com/esp32-http-get-post-arduino/
https://randomnerdtutorials.com/esp8266-weather-forecaster/
https://randomnerdtutorials.com/esp8266-nodemcu-http-get-post-arduino/#http-post
https://electrosome.com/calling-api-esp8266/

Click to rate this post!
[Total: 2 Average: 5]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.