Hi guys, Nick here, welcome once again to educ8s.tv, Today we build a Weather Station Which gets updated every hour via Openweathermap using the WiFi Capabilities of the new, impressive ESP32 chip for the first time on this channel along with a Nextion display.

Intro to the ESP32 Weather Station Tutorial

Yet another weather station?, yes, I know but emphasis today is on how to use the ESP32 board with its wifi capabilities with a nextion display. We will also be using the new BME280 temperature and humidity sensor which also comes with the ability to measure barometric pressure and like I do with all other tutorials I will be sharing in an easy to follow manner, so you can replicate this on your own or be able to use this two main components (and all the other components) used in this tutorial in any other project you decide to do.

This is my first ever project with the new ESP32 chip and also the first project in which I will be using the Nextion display. If you are not familiar with the ESP 32 Board, it is the successor of the popular ESP8266 chip we have used many times in the past. The ESP32 is a plainly a beast! It offers two 32 processing cores which operate at 160MHz, a massive amount of memory, WiFi, Bluetooth and many other features with a cost of around 7$! you can watch the detailed review I prepared about the chip by clicking on this link(https://www.youtube.com/watch?v=Mq1YhgS5VkY). It will help you understand why this chip will change the way we make things forever!

ESP32

Some of the features of this chip include:

  1. High performance-price ratio
  2. Small volume, easily embedded to other products
  3. Strong function with support LWIP protocol, Freertos
  4. Supporting three modes: AP, STA, and AP+STA
  5. Supporting Lua program, easy to develop codes/firmware

As mentioned earlier, this is also the first time I will be using the nextion display in my tutorials. The nextion displays are powerful displays which produce quality graphics. They have their own ARM processor at the back which is responsible for driving the display and creating the graphical user interface and they can be used with any microcontroller with spectacular outputs. Similarly, I have prepared a detailed review of this Nextion display which explains in depth how they work, how to use them and their drawbacks and you can watch the video by following this link(https://www.youtube.com/watch?v=mVuy33FK9L0).

3.2″ Nextion Display

For an experienced DIY electronics hobbyist, this project should take no more than 5minutes to build while for a beginner it may take longer time. I assigned certain videos which will help beginners build this project easily to be displayed on the video version of this tutorial.

Our goal for this project is to build it in such a way that when the device is powered, it connects to the WiFi network, and retrieves the weather forecast for my location from the openweathermap website. Then display the forecast on the 3.2” Nextion Touch Display along with the readings from the BME280 sensor. The readings from the sensor are updated every two seconds while the weather forecast from openweathermap is updated every hour!

Require Parts/Components and Where to Buy

The following components/parts are needed to build this project and each component can be bought from the link in front of it.

  1. ESP32: https://educ8s.tv/part/ESP32
  2. Nextion Display: https://educ8s.tv/part/Nextion32
  3. BME280: https://educ8s.tv/part/BME280
  4. Small Breadboard: https://educ8s.tv/part/SmallBreadboard
  5. Jumper Wires: https://educ8s.tv/part/JumperWire
  6. 3 in 1 wires: https://educ8s.tv/part/Wires
  7. Power Bank: https://educ8s.tv/part/Powerbank

Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!

[adsense]

Schematics

The connection of the parts is straightforward. Connect the components as shown in the schematics below. The schematics is also included in the file attached to the download section towards the end of this post.

Schematic Diagram of the ESP32 Weather Station Project

ESP32 Weather Station

ESP32 Weather Station Schematics

Since the BME280 sensor uses the I2C interface, we thus need to connect it to the I2C pins of the ESP32. In theory, every digital pin of the ESP32 board can be used with I2C peripherals. In practice though, I found out that some pins did not work because they are reserved for other uses. but Pins 26 and 27 worked quite well.

Pin connections between the BME280 and the ESP are described below.

BME280  ESP32

VCC  5v

GND GND

SDA  pin 26

SCL  pin 27

To send data to the Nextion display, we only need to connect one wire to the TX0 pin of the ESP32. The pin connection of the nextion display to the ESP 32 is also described below.

Vcc  5v(Vin)

GND  GND

TXO  TXD1

 

Preparing the Nextion Display

After connecting the parts, before uploading code to the ESP 32, we need to load the GUI on the Nextion display. The process of creating the graphics is really easy but for this tutorial, I will be sharing the GUI design with you. Named weatherstation.tft, it is part of the files you get when you download the file under the download code section. Copy the file into an empty SD card and insert the card into the slot at the back of the display. Power the Display on and the display, the GUI should now be loaded on the display. Remove the SD card and repower the Display, The GUI will still be stored on the display’s memory. The Nextion GUI consists of a background, some textboxes and a picture that changes depending on the weather forecast.

Nextion Display GUI Canvas

Please watch Nextion display tutorial at this link (https://www.youtube.com/watch?v=mVuy33FK9L0) to learn more about this display. You can quickly design your own GUI if you wish and display more things on it.

With this done, we are then ready to write the code for this project.

Code

To write the code to achieve the goals of these project, we will be using two very important libraries; The BME280 library for ESP32 and the Arduinojson library which will be used to parse the weather data from openweathermap. The Download links for the two libraries are available below.

Libraries

? ESP32 BME280: https://github.com/Takatsuki0204/BME280-I2C-ESP32

? Arduino JSON: https://github.com/bblanchon/ArduinoJson

 

To explain the code as usual, The first thing we do is include the libraries that will be used.

#include "Adafruit_BME280.h" //https://github.com/Takatsuki0204/BME280-I2C-ESP32
#include <ArduinoJson.h>    //https://github.com/bblanchon/ArduinoJson
#include <WiFi.h>

Next, we add the SSID and password of the WiFi Access point to which our ESP will be connected to Access the openweathermap’s website.

const char* ssid     = "yourSSID";
const char* password = "yourPassword";

Next, we set the parameters needed for connection to the openweathermap’s server which is our City ID and the APIKEY after which we define the altitude which will be used by the BME280 sensor.

String CityID = "253394"; //Sparta, Greece
String APIKEY = "yourAPIkey";
#define ALTITUDE 216.0 // Altitude in Sparta, Greece

With the above done, we then specify the pins of the Arduino to which the BME280 is connected and also set the I2C address the sensor should use.

define I2C_SDA 27
#define I2C_SCL 26
#define LED_PIN 2
#define BME280_ADDRESS 0x76  //If the sensor does not work, try the 0x77 address as well

Next, we define and initialize some of the variables that will be used by this project, after which we create an instance of the BME280 library.

float temperature = 0;
float humidity = 0;
float pressure = 0;
int weatherID = 0;

Adafruit_BME280 bme(I2C_SDA, I2C_SCL);

Next, we create an instance of the wifi client and specify the server name for openweathermap.

WiFiClient client;
char* servername ="api.openweathermap.org";  // remote server we will connect to

Next, we move to the void setup function.
The setup function is a simple one as all we have to do is start serial communication so we can monitor the behavior of the system, use the initsensor function to initialize the BME280 and connect the weather station to the wifi network using the connectToWifi() function.

void setup() 
{
  pinMode(LED_PIN, OUTPUT);
  
  Serial.begin(9600);

  initSensor();

  connectToWifi();
}

Next, We write the void loop() function.
The void loop() function gets the weather information from open weather once every hour but prints the temperature, humidity and pressure to the nextion display using the sendtonextion functions.

void loop() {
 
 delay(2000);

 if(iterations == 1800)//We check for updated weather forecast once every hour
 {
   getWeatherData();
   printWeatherIcon(weatherID);
   iterations = 0;   
 }

 getTemperature();
 sendTemperatureToNextion();
 
 getHumidity();
 sendHumidityToNextion();
 
 getPressure();
 sendPressureToNextion();

 iterations++;
 
 blinkLED();
}

To update the display, we simply send some commands to the serial port as shown in the video tutorial. If you get a compilation error while compiling, you have to add this line, “-fno-threadsafe-statics” at the platform.txt file with the path to it shown in the image below.

Press save, and then the project will compile fine. The software for the ESP32 is not mature, yet so some things do not work at once yet.

The complete code for this project including the schematic and the code for the Nextion display can be downloaded from the link below.

 

——————–
CODE & SCHEMATICS
——————–

 

 

 

Copy the complete code and upload to the ESP, you should see the display come up as shown in the image below.

As you can see, an experienced maker today can build an exciting project in just a few hours with a few lines of code and only three parts, like this ESP32 Weather Station! A project like this would have been more difficult to make two years ago! Of course, this is just the beginning of the project. I would like to add many features to it, like graphs, touch functionality that is now missing, maybe a bigger display and of course a beautiful looking 3D printed enclosure. I will also design a better looking GUI and icons. I have some very fresh ideas to implement!

I would love to hear your opinion about today’s project. What kind of features do you want me to add to the project? Do you like how it looks? How do you want to see it evolve? Please post your ideas in the comments section below; I love reading your thoughts!

——————–

SUBSCRIBE ON YOUTUBE

——————–

Never miss a video: Subscribe to educ8s.tv