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!
Some of the features of this chip include:
- High performance-price ratio
- Small volume, easily embedded to other products
- Strong function with support LWIP protocol, Freertos
- Supporting three modes: AP, STA, and AP+STA
- 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).
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.
- ESP32: https://educ8s.tv/part/ESP32
- Nextion Display: https://educ8s.tv/part/Nextion32
- BME280: https://educ8s.tv/part/BME280
- Small Breadboard: https://educ8s.tv/part/SmallBreadboard
- Jumper Wires: https://educ8s.tv/part/JumperWire
- 3 in 1 wires: https://educ8s.tv/part/Wires
- 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
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.
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
I have built this – great fun – thanks! But I am not there yet .. My ESP32 is at COM46 .. all the Serial.print output us coming to COM46 – debug stuff, *and* the serial data for the display – nothing apparently down the serial port (TX0) to the display itself, so I see this in the COM46 serial monitor … (the 6,3 stuff is some sxtra debug stuff I put in for the WiFi)
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:956
load:0x40078000,len:0
load:0x40078000,len:11904
entry 0x40078a3c
Init BME280
6
.6
.6
.6
.6
.6
.3
.
WeatherID: 802⸮⸮⸮weatherIcon.pic=5⸮⸮⸮temperature.txt=”26.5″⸮⸮⸮humidity.txt=”45.4″⸮⸮⸮pressure.txt=”1013.6″⸮⸮⸮temperature.txt=”26.5″⸮⸮⸮humidity.txt=”45.4″⸮⸮⸮pressure.txt=”1013.6″⸮⸮⸮temperature.txt=”26.5″⸮⸮⸮humidity.txt=”45.3″⸮⸮⸮pressure.txt=”1013.6″⸮⸮⸮
That’s strange. The results look fine
Yes, the output does look right – should the output going to *both* the COM46 serial port as well as TXD by default?
Ok, I found the issue — I had the Serial.begin at 115200 rather than 9600. The initial debug data is at 115200, so that appears as garbage, but the Nextion wants to be at 9600. The answer to my question of whether COM46 serial monitor (in my case) and TX0 are the same – is yes. COM46 reflects what is being output down TX0
Thanks for sharing!
Hi Will this work with the esp8266 on a nodemcu? Thanks.
Tenho o Esp 32 lora da ttgo e é possível usá-lo?
is it possible to add dht22 and 1-wire DS18xx ?
Great little project that works well. What is the code to change to F*
Any help to convert the temperature values to Fahrenheit or barometric pressure to inches of mercury?
hello when verifying the code i get a lot of errors
ESP32_Weather_Station:30: error: no matching function for call to ‘Adafruit_BME280::Adafruit_BME280(int, int)’
Adafruit_BME280 bme(I2C_SDA, I2C_SCL);
and then its a lot wifi errors
wrong librarys? or . any ideas?
thanks Lasse
Anyone find a solution here? I also had this problem.
Looks like I found a solution. This uses the https://github.com/Takatsuki0204/BME280-I2C-ESP32 library. Delete the AdafruitBME280 library in your Arduino libraries folder. Restart the IDE program and enjoy!
Not at all clear what to do with BME library.
Great tutorial, Nick – just what I’ve been searching for! Would be cool to see this project extend into thermostat controls… maybe even wifi-controllable like Nest.
Hey there,
nice tutorial.. now I just started to do it and I’m stumbling with compilation problems.
SP32_Weather_Station:159:1: error: ‘StaticJsonBuffer’ was not declared in this scope:
StaticJsonBuffer json_buf;
SP32_Weather_Station:159:24: error: ‘json_buf’ was not declared in this scope
StaticJsonBuffer json_buf;
^
ESP32_Weather_Station:161:11: error: ‘class ArduinoJson::JsonObject’ has no member named ‘success’
if (!root.success())
^
I downloaded both needed Repositories as zip and then added them with the library manager on the Arduino IDE (v.1.8.6) Maybe when you built this example there was another active version for ArduinoJson? do you still have it? can you check which version did you use? Thanks in advance!
Hi Nick,
I encounter the same problem as Duckie has described…..
I am anxious to hear how to solve the error
rgds Rinus
Nice Project,
Does anybody know how I can skip the BME? I want just my local weather to be displayed. Any help is welcome!
Thanks Paul
Hi
Great projecrt.
It seems that you forget some mistake in this this web page
You wrote
BME280 ESP32
VCC ▶ 5v
GND ▶ GND
SDA ▶ pin 26
SCL ▶ pin 27
And later. you wrote in the code
define I2C_SDA 27 . (Missing the #)
#define I2C_SCL 26
and the SDA and the SCL are inverted.
Best regards.
JB
Bump….Nobody that can help me with skipping the BME?
Thank you so much!
I have repeated you project as is! It works fine!
I’ve translated words on display into Russian language. Excellent!
Ciao Nick nella compilazione mi da’ errore : Static JsonBuffer was not declared not in this scope.Cosa fare ? Grazie
ESP32_Weather_Station:10:81: error: Adafruit_BME280.h: No such file or directory
compilation terminated.
exit status 1
Adafruit_BME280.h: No such file or directory
What can i do ?…
sendTemperatureToNextion – what is the magic method?
How can I add the current date and time to this project?
Very rewarding and wonderful
Thank you, all worked well and pretty straight forward.
Hi. I have this project working great, but want to add the location name, and outdoor temperature and outdoor humidity from the weather data that it pulls from openweathermap.org. I am needing to know what code to add to make this data available to show on my nextion display. Please help. Thanks.
I don’t use WiFi yet. What changes should be made to the sketch so that only the BME280 sensor data is displayed?
Cool project but it doesn’t compile anymore. The library is at JSON 6 when the code was written for 5. A lot of the calls have changed. It is a learning experience going through all of them but it mostly documented here https://arduinojson.org/v6/doc/upgrade/
Hello,
no such path on my computer…
C:/Users/UserName/Documents/Arduino/hardware/espressif/esp32/platform.txt
Where I put the file to help me with the compilation?
Another problem is: invalid conversion from ‘int’ to ‘SPIClass*’ [-fpermissive]
how to fix this problem?
An also try to put the file in the NEXTION Display with the SD card, but message says: File version to low… how fix this?
Cheers Jay