How to build an ESP8266 solar powered weather station with Bosch BME280 Sensor
In this tutorial we will see how to build an home made weather station based on the cheapest Arduino compatible board with WiFi connectivity: the ESP8266. We’ll create a Solar Powered Weather Station for monitor outside temperature, humidity, pressure and also Dew Point throught the Bosch BME280 sensor.
Starting from this implementation, you could add many others sensors and features. In my case infact, this Weather Station is just one of the five nodes of my IoT Network, but we’ll talk about it later.
So, let’s see what we need to create a fully autonomus ESP8266 Solar Powered Weather station with Bosch BME280 Sensor!
How to build an ESP8266 solar powered weather station with Bosch BME280 Sensor
We need many components plus the ESP8266 Board. In my case, I decided to use the NodeMCU v3 board based on the ESP8266 12E microcontroller. You can easily find it on Aliexpress for a pair of bucks. As sensor, I used the Bosch BME280, a tiny and very reliable sensor. It cost around 2.5 dollars on Aliexpress. Then we need many cables for the wiring. I suggest you tu buy at least twenty standard cable for Arduino applications, those with standard PINs connection. They are very cheap but also not so reliable, so be sure to buy some spare parts. I used this kit. The last three Electric equipment we need are the charger/discharge board, the battery and, of course, the Solar Panel.
As battery you can use/buy any 3.7 Volt battery you want. Be sure to use at least a 1000 mAh battery in order to have enough charge for cloudy days. I used my “ex-drone” battery, a huge 5200 mAh Lithium battery based on two connected 18650 cells. Then you need a Solar Panel. You can use many solutions but, since the charger board accept as input voltage a value between 5 a 7 Volt, I suggest you to use a 6 Volt Solar Panel. I used this one, a tiny Panel that you can buy from China for one Dollar. At the end, we need a Diode and an automatic charge/discharge board with battery protection circuit. I used the cheapest one: the TP4056 Charger Module.
We also need some raw material and an IP67 box for the boards and battery housing. Something like this. In order to make the temperature measurement work properly, we also have to create a Solar Shield. Infact, since the temperature accurancy is influenced by the sun and soil radiation, we need a proper Solar Shield. You could buy some ready to use solution but they cost too much.
I decided to create an homemade Solar Shield using many stacked planpots. You’ll need 10 planpots with a diameter of 18 or 20 centimeter, and one planpot of 20 or 22 centimeter. Then, you need many studs and washers, with the right size threaded rod (at least 60 cm, in order to obtain three pieces of 20 cm). At the end you have to paint the structure with a lucid white color, in order to improve the reflectivity of the device.
Finally, you may need some waterproofing membranes and a back panel for fix all this stuff on a pole at 170 centimeter of height from the soil.
Summarising, you need:
- one NodeMCU ESP8266 Board;
- one Bosch BME8266 Sensor;
- many cables with standard PIN dimension;
- a DIODE;
- one TP4056 Charger Board;
- one or more 6 Volt Solar Panels;
- an IP67 box;
- ten 18-20cm diameter planpots;
- one 20-22cm planpot;
- lucid white color;
- one threaded rod with many studs and washers;
- a 2 meter pole;
- many waterproofing membranes.
Total cost should be around 30-35$.
Hardware setup: connecting ESP8266 and Bosch BME280
First of all we need to connect properly the ESP8266 board with the Bosch BME280 sensor. The Bosch BME280 needs four connections to work. Starting from the left, we have the VCC (3.3-5 Volt) PIN, followed by the GND PIN. Then, we have signal clock line (SCL) and the data connection wire (SDA). In this project we’ll use the BME280 sensor in I2C mode.
Using four cables with PINs, connect the Sensor to the NodeMCU ESP8266 Board in this way: VCC to the 3.3 Volt port, GND to the Ground port of the board, while SCL to the port D4 and SDA to D3.
Hardware setup: connecting ESP8266, battery and Solar Panel
At this point, we have to connect the Solar Panel and the battery to our charge/dischage board. The setup is very simple. The battery must be connected to the bottom of the board. you have to but the wires on the the right positive and negative poles. Usually on the TP4056 board, this connections are marked up with “BAT+” and “BAT-” stamps.
You also have to do the same things for the Solar Panel on the upper part of the board. Remember also to put the DIODE on the positive wire. At the end, you can connect the ESP8266. Your scheme should be like the one below:
The hardware setup it’s done. Now we have to write the right code and put it one the ESP8266 board.
Software Setup: reading Sensor Data and send them to a server through POST method
At this point, we have to work on the software part. I use the Arduino IDE to program my ESP8266 board. However, you can use any other IDE you want. If you decide to use Arduino IDE, be sure to import the right library and COM ports driver. You can check this tutorial.
As reported above, I used the REST approach with the classic GET and POST methods over HTTP protocol. There aren’t sessions or cookies. In my project, I create an own web server, but you can use free tools like ThingSpeak to display your data on the web. This free solutions have many limits in terms of request/day, so if you want to collect data from many sensors in real-time (so each second), you have to pay at least a Student License (if you are a students, of course), which is 80 USD Year. take a look to the difference between FREE and Students solutions:
I decided to create a “rought” web server with a very minimal PHP web page and a Mysql database where the data are stored, all on my hosting server. But first, let’s see how collect the data from the sensor.
Reading Sensor Data
In order to make your Bosch BME280 reachable by the ESp8266, you need the right library. You can find it here: BME Library. You simply have to import it in your Arduino IDE Library folder. Then, just import it in your main, as you can see below:
At this point, we have to define all the variables regarding temperature, humidity, atmospheric pressure and Dew Point. We also have to define many parameters. Check below:
Here we have the parameters and variables explained:
- ALTITUDE: is the altitude in meters over the sea of your locations. It’s used to compute the pressure;
- I2C_SDA 4: is the PIN where you collect the wire regarding the SDA port of the BME280 Sensor;
- I2C_SCL 5: is the PIN where you collect the wire regarding the SCL port of the BME280 Sensor;
- BME280_ADDRESS 0x76: is the memory address of you sensor. Usually it’s 0x76 but sometimes it is 0x77;
- cTemp: support variable for the temperature;
- humidity: support variable for the humidity;
- DP: support variable for the Dew point;
- pressure: support variable for the pressure.
We also defined the BME sensor in a simplier way, as reported below:
Now, we can define the sensor initialization fuction:
This function will be called in the Setup() method in order to check if the sensor is available or not.
At this point, we need a function which uses the temperature and humidity to compute the Dew Point. I found it on the web, but there are many implementations. Check it:
And then, we have the Loop class, where we read the temperature from the sensor. I added an IF to check if the values are fake or real, then, if the values are real, we read humididty and pressure, then we compute the Dew Point. All the results are then printed in the output console.
At the end of the sketch I use the method sleep to send the ESP8266 board in Deep stand-by, in order to save energy, since the device will be Solar Powered. I set a sleep time of 300 seconds, but you can chose any interval you prefer.
When the board is in deep sleep, it must be woken up using the Reset PIN. To do this, you simply have to connect through a wire connected between the RST PIN and the wake PIN. In this way, the system will wake up automatically.
Connect EPS8266 to WiFi
In order to send our data to the web server, we have to connect our ESP8266 Weather station to our wireless network. To do this, we simply have to import the right library (ESP8266WiFi.h) as reported above, then we have to define the wireless network SSID, the password and use the right method to switch on WiFi and establish a connection.
In order to enable the WiFi Connection, I use the following function, which I re-called in the setup() method to initialize the connection. You can check the code here:
When the connection is established. the program will print in the output console “WiFi connected”. At this point we can send our data to our main server!
Send data to the server through a POST method
Now we have the most interesting part: send the data to the server. As reported above, I chose the REST approach and I created my own web server, database and web page. But you can easily use ThingSpeak. The implementation is quite the same.
The REST approach uses the method POST and GET to send and retrive data over HTTP protocol. So, in order to send our data to the server, we have to import the HTTP functionalities. On the ESP8266 board, they are already implemented inside the ESP8266WiFi.h library. We have three things to do . First, we have to create a string with our data retrived from the sensor. Second, we have to create a TCP connection using WiFiClient class and then POST the string to the server in the right path.
As you can see in the screen, I created the string, which will become our URL, appending the variables definition and the measured values. Then I setup the WiFiClient in order to create the TCP connection. This method try to establish a connection on the 80 port with the chosen server, defined in the server variable at the beginning of the sketch. If the connection fails, an error message will be printed. At the end, I complete the URL adding the path where I want to post my data. In my case this is the PHP file where I perform the GET method over the URL. It depends on the web server or service that you are using, so it could vary a lot.
You should obtain something like this:
At this point, we can perform the true POST method. There are many ways to do this, I chose this one:
Starting from the string seen before, this method will POST this URL to the server. It’s like copying and pasting your URL in the browser address bar and then press enter. You can find all the code in my Gitlab repository: HERE
NB: in the sketch that I have uploaded on Gitlab you will find also the code parts related to the CoAP implementation, which I’ll discuss in another tutorial in the next weeks.
At this point, the sketch is ready and can be written on your ESP8266 board.
Writing on the ESP8266 the sketch and testing it
In order to write the code on your board, you have to properly setup the Arduino IDE as explained in the first paragraph. Go to tools and select the right board. In my case, I had to chose the Nodemcu v1.0 in the IDE menu. You also have to chose the right COM port.
At this point, you can compile and write the sketch on your ESP8266 board. To do this, you simply have to press the arrow button under the Edit menù. Arduino IDE will compile the code and check for errors. If all it’s right, the sketch will be uploaded to the ESP8266.
Before uploading, remember to remove the wire connection between the RST and WAKE PIN, otherwise the upload will fail.
At this point, you can check if the device works using the serial monitor. You can find it under the tools menù in the Arduino IDE. Remember to chose a baud rate as setted on the ESP8266 board. If all is ok, in the output console you will see the readed values of temperature, humidity etc and the URL string.
Web server setup
Now we have the final step. Here I’ll briefly show you the setup for my selfmade server. However, I must tell you that this is a very spartan solution… you can do easily better! Let’s start!
I created three files: one in order to create the web page with the collected data and the graphs, one to catch the data through the GET method of the HTTP protocol and to insert them into the database; another one with the parameter to connect to the MySQL database.
The structure is the following: at the begin I collected the parameters to connect to the database, located in the connect.php, then I prepared the MySQL statement,
where I declared the query used to insert data inside the database. Also, inside the query, I used the GET method to collect data incoming from the ESP8266 Weather Station. The PHP file is add_data.php and you can easily find it on my Gitlab repository.
You can take a look to the final result HERE.
At this point, we can finalize he project. I decided to put all the hardware equipment inside a tiny IP67 box. I soldered all the wires and I covered them with waterproofing membranes, in order to obtain a waterproof solution. In order to make the temperature measurement work properly, I created a solar shield using THIS tutorial. It works really well! Otherwise, you could buy some ready to use solution but they cost too much.
At the end, I painted the structure with a lucid white color, in order to improve the reflectivity of the device. Finally, I put the solar panel over the solar shield and I fix the whole ESP8266 Weather Station on a pole at 170 centimeter of height from the soil. Here you can find some shots of the final installation.
After more than four months of activity with many weather conditions (from heavy rain to snowfall and ice, with also 10°C under zero), the system is still working fine.
The weather station is quite accurate. I have an analog weather station near the ESp8266 solution and I often compare the results. I can say that, after four months of activity, the pressure is really accurate, while the temperature during the day light is overstimated by 1-1.5 °C. That’s due to the quality of the Sunlight Shelter, since during the night the temperature values between the ESp8266 station and the analog Station are the same.
I noticed only an issue: during heavy rain or fog, the humidity is overstimated to 100%. When the real humidty is above 95-96%, the ESP8266 reads 100%. Unfortunatly I can’t fix it.
For this tutorial it’s all. I hope you enjoy it and I remeber you that you can find all my codes HERE.