Introduction: ESP32 Based IoT Weather Station

Hi, in this instructable I will show you how to make a Weather Station using the very famous ESP32 with 3 sensors that can measure and display live values including:

  1. Teperature
  2. Humidity
  3. Barometric Pressure
  4. UV Index
  5. IR Radiation
  6. Visible Light

The parameters are displayed on the Blynk IoT platform both on the Web Dashboard as well the Phone App in the form of live values and charts.

This can be a good project to get started with IoT. Hardware and connection part is easy. Code and setup part is just a bit lengthy so read the instructions carefully till the end to make sure everything works.

You can also watch a complete video guide of this project on YouTube:


Subscribe to my YouTube Channel if you haven't.

Side Story:

I haven't been posting projects here for a very long time due to a few reasons. One of them is- I joined University and second- due to bad mental health. The last few years have been really bad so I almost gave up on this work. I couldn't get any ideas, lost all my will to work and my YouTube channel is also dead (I barely get views). Somehow, I gathered courage to post this very small and simple project. I hope I can be active again like I used to be when I was in school. :')

__________________

Thanks to NextPCB for helping me with this project.

NextPCB is one of the most experienced PCB manufacturers globally. It has specialized in the PCB and assembly industry for over 15 years. Not only could NextPCB provide the most innovative printed circuit boards and assembly technologies in the highest quality standards, the fastest delivery turnaround as fast as 24 hours.

If you have a PCB project, please visit their website and get exciting discounts and coupons!

Only 0$ for 5-10pcs PCB Prototypes: https://www.nextpcb.com/?code=Kumar

Register and get $100 from NextPCB: https://www.nextpcb.com/register?code=Kumar

See more info about PCB Assembly Capabilities: https://www.nextpcb.com/pcb-assembly-services

Supplies

Here's what you will need for this project:

  • ESP32 Development Board
  • DHT11/21/22 Temperature & Humidity Sensor
  • SI1145 Sunlight Sensor
  • BMP180 Barometric Pressure Sensor
  • Perfboard or Breadboard (to make connections)
  • Male to Female Jumper Wires
  • Grove Connector Cables (only if you're using grove sensors)
  • Micro USB Cable

Step 1: Connections

First we will start by connecting all the sensors to ESP32. I have attached the wiring diagram image so you can have a look. You can also download the pdf file below. Since I am using grove sensors, I first connected grove cables to all the sensors and used male to female jumper wires to further connect them to the ESP32.

You can either use a breaboard to place the ESP32 or solder a very simple shield like I did with male headers connected parallelly to each pin on ESP32. I added some extra male headers for some pins like Vin, Gnd, D21 and D22 as these will be used the most.

Although the connections are pretty straightforward, I have written them below for easier understanding. The Temperature & Humidity sensor uses only one digital pin for communication (which will be connected to pin D5), while the other two sensors use I2C communication so they both will be connected to D21 (SDA) and D22 (SCL) pins on the ESP32.

Connections for DHT Temperature & Humidity Sensor:

  • Vcc (red) → Vin on ESP32
  • Gnd (black) → Gnd on ESP32
  • NC (white) → Not Connected
  • SIG (yellow) → D5 on ESP32

Connections for BMP180 Barometric Pressure Sensor:

  • Vcc (red) → Vin on ESP32
  • Gnd (black) → Gnd on ESP32
  • SDA (white) → D21 on ESP32
  • SCL (yellow) → D22 on ESP32

Connections for SI1145 Sunlight Sensor:

  • Vcc (red) → Vin on ESP32
  • Gnd (black) → Gnd on ESP32
  • SDA (white) → D21 on ESP32
  • SCL (yellow) → D22 on ESP32

Step 2: Make Everything Look Neat

After I was done with the connections, I placed all the sensors and ESP32 on a piece of hard board with some double sided tape and then also cleaned up the wiring with tape such that everything looks neat.

Step 3: Code

Download the code for this project from below and open it in your Arduino IDE.

Step 4: Setup ESP32 on Arduino IDE

If you're using your ESP32 for the first time, it is not added by default on the Arduino IDE. So in order to make it compatible with Arduino IDE, we need to add it to the IDE first. You can skip this step if you've already used ESP32 with Arduino IDE before.

First go the official Espressif Documentation and copy the stable realease link. I have mentioned it below so you can copy from here as well:

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Now on your Arduino IDE:

  • Go to Files > Preferences
  • Under 'Additional Boards Manager URLs' paste the above copied link
  • If there are other link(s) mentioned already, separate this link and the others with a comma
  • Click on OK
  • Now go to Tools > Board > Boards Manager
  • Under the search bar, type ESP32 and hit enter
  • You will see ESP32 by Espressif Systems
  • Click on Install under it
  • Click on CLOSE

Now if you go to Tools > Board, you will now be able to see all the varients of ESP32 added.

Step 5: Install Required Libraries

In order to make our code work, we will have to install the necessary libraries.

Now go to Sketch > Include Library > Manage Libraries

In the search bar type 'Blynk' and install the Blynk Library by Volodymyr Shymanskyy

Similarly download the following libraries:

  • DHT Sensor Library by Adafruit
  • Grove Barometer Sensor by Seeed Studio
  • Grove Sunlight Sensor by Seeed Studio

Step 6: Create Blynk Account

Go to blynk.io and create a new account or login if you already have one.

Step 7: Create a New Template

After you're done logging in, it will say "Start by creating your first template" so click on 'New Template'.

  • Under the name you can type anything. I named it as 'Weather Station'
  • Under 'Hardware' select ESP32
  • Under 'Connection Type' select WiFi
  • You can also add a description if you want
  • Click on 'Done'
  • Now you will see a unique template ID and name provided

Step 8: Create Datastreams

To receive data on Blynk from ESP32, you will have to create datastreams.

  • First go to 'Datastreams'
  • Under 'New Datastream' select 'Virtual Pin'
  • The first datastream is named as 'Temperature'
  • Select 'V0' under Pin
  • Data Type will be 'Double' because we are receiving accurate values with decimal places
  • Unit will be Celcius (°C)
  • 'Minimum Value' can be kept as 0 and 'Maximum Value' as 100
  • Under 'Decimals' select '#.##' since we want our values to be upto 2 decimal places
  • Default Value can be kept as 0
  • Click on 'Create'

Now similarly we need to add 5 other datastreams:

Humidity:

  • Pin - V1
  • Data Type - Double
  • Unit - Percentage (%)
  • Mimimum Value - 0
  • Maximum Value - 100
  • Decimals - #.##
  • Default Value - 0

Pressure:

  • Pin - V2
  • Data Type - Double
  • Unit - None (since millibars is not available)
  • Mimimum Value - 0
  • Maximum Value - 2000
  • Decimals - #.##
  • Default Value - 0

Visible Light:

  • Pin - V3
  • Data Type - Double
  • Unit - None
  • Mimimum Value - 0
  • Maximum Value - 300
  • Decimals - #.##
  • Default Value - 0

IR Radiation:

  • Pin - V4
  • Data Type - Double
  • Unit - None
  • Mimimum Value - 0
  • Maximum Value - 300
  • Decimals - #.##
  • Default Value - 0

UV Index:

  • Pin - V5
  • Data Type - Double
  • Unit - None
  • Mimimum Value - 0
  • Maximum Value - 300
  • Decimals - #.##
  • Default Value - 0

After all these datastreams have been added, click on Save.

Step 9: Configure Web Dashboard: Add Labels

Web Dashboard is a place where all the data will be displayed. There are many types of widgets we can add to the Web Dashboard- some are for displaying data and some are for providing a signal

  • First we have to place a label
  • Drag and drop it on the dashboard
  • You can even resize it
  • Now place 5 more label widgets for each of the values
  • To configure a widget, click on the gear icon on it
  • First one will be named as Temperature
  • Then select datastream (from which the data will be taken and displayed) as Temperature (V0)
  • There are several other options available that you can explore
  • You can set a label colour and make it change depending on the value
  • You can select 'Show Level' which will show the temperature level beside the label
  • After it's done, click on Save
  • Now repeat the same process for the other 5 labels
  • Just select the right datastream for the respective labels

Step 10: Configure Web Dashboard: Add Charts

Now that we're done with labels, we can add charts to see past data.

  • Drag and drop a chart widget
  • We cannot show 2 different datastreams on the same chart in the free version of Blynk, so we need to add 6 different charts for all the values
  • Configuring a chart is also similar,
  • Just select the right datastream
  • Select chart colour
  • Select chart type such as Line, Area, Column or Stepline
  • Turn on 'Show Y Axis' and 'Autoscale' if you want
  • Click on Save
  • Repeat the same for all other charts

Now that our Web Dashboard is complete, we can click on Save.

Keep in mind that the values it will be displaying now will be completely random. It's just to show some sample data.

Step 11: Create New Device

After configuring the template, now we will add our device to it. For this:

  • First go to 'Search' at the left side
  • Click on 'New Device'
  • Select 'From Template'
  • Choose the template that we created (Weather Station in my case)
  • Device Name can be kept as per your choice (I kept it the same as template name)
  • Click on Create
  • Now our device has been created but it will show was offline as it is still not connected

Step 12: Copy Credentials to Code

When you have created a device, you will see some credentials at the top right corner of the page. These are:

  • Template ID
  • Device Name
  • Auth Token

Copy them one by one and replace them in the code in their respective place.

  • After #define BLYNK_TEMPLATE_ID add your template id under " "
  • After #define BLYNK_DEVICE_NAME add your device name under " "
  • Under char auth[] = " "; add your auth token under " "
  • Under char ssid[] = " "; add your WiFi name (SSID) under " "
  • Under char pass[] = " "; add your WiFi password under " "

Step 13: Set DHT Sensor Type

There is just one more little change in the code that may be required. If you're using DHT11 or DHT21 Temperature & Humidity Sensor instead of DHT22 (which is set as default), uncomment the respective line.

Example if you're using DHT11:

#define DHTTYPE DHT11
// #define DHTTYPE DHT22
// #define DHTTYPE DHT21

And if you're using DHT21:

// #define DHTTYPE DHT11
// #define DHTTYPE DHT22
#define DHTTYPE DHT21

If you're using DHT22, do not make any change!

Step 14: Upload the Code to ESP32

Connect your ESP32 to your PC with a Micro-USB cable. Select the right board and com port in tools. Board depends on what model of ESP32 you're using, like mine was ESP32 Dev Kit.

Finally, click on upload.

You know the drill, as soon as it shows 'connecting...', press and hold the boot button on ESP32 for a few seconds and release. It should upload without problems.

Step 15: Testing & Troubleshooting

After you're done uploading:

  • Open the Serial Monitor
  • Set Baud Rate to 115200
  • Press Reset button on ESP32
  • You should see the Blynk logo being displayed on the Serial Monitor if it has connected to WiFi successfully
  • If it does not connect, check if you have entered WiFi credentials on the code correctly along with Auth Token, Device Name and Template ID
  • Now go back to the Blynk Dashboard
  • You should see your device online and the values being displayed correctly
  • If some value is not displayed correctly, first edit the dashboard and check if you have chosen the right datastream
  • If it still doesn't work, check sensor connections and reconnect.
  • To make sure all your sensors are working, you can first upload an example sketch of each sensor to display values on Serial Monitor and check them one by one

You can have a look at the images above. In my case, everything was working like a charm. I even left the device on for hours and it worked without any connectivity issues. I have attached some pictures of chart data for past hour as well as past 6 hours.

To show that it's actually working, I blowed air on to the Temperature & Humidity Sensor. Since the air we breathe out is moist and warmer than surrounding, you can see a spike in values in one of the pictures. The chart also shows a sudden increase in Temperature & Humidity as well as some drop in Pressure since Temperature and Pressure are inversely related.

Step 16: Setup Mobile Dashboard

Here I will explain how you can connect your weather station to your phone. This is completely optional.

  • First download the Blynk IoT app from Play Store/App Store
  • Login using the same credentials that you used before
  • Our device will already be added by default because it syncs with the web but we still need to setup the mobile dashboard
  • This is almost similar to setting up the web dashboard
  • If you click on the top right corner of the screen on the widget box, you will see all the widgets available and some new ones as well that are not present in web dashboard
  • We are interested in Labelled Value so select that, place it on the dashboard and resize if needed
  • Now place 5 more of them
  • Click on the label to configure it and follow the same process as the Web Dashboard
  • Here you can add a text before and after the displayed value so I added respective units after the value
  • You can also add supercharts if you want
  • Again, for the free version you can show only one datastream in a chart
  • You can even edit range of past data such as 15 minutes, 30 minutes, 1 hour and so on..

Step 17: Testing Mobile Dashboard

Now if you go back, you will see all values being displayed if your device is on and you've done everything correctly. The app has some bugs like it automatically deletes widget name and datastream so keep a check on that. The app also works well in dark mode.

Step 18: Conclusion

You can try to add more sensors to this project once you understand the working (such as Wind Speed Sensor, Rain Sensor etc). It's not that difficult. You will just need more virtual pins and you will have to add more labels and charts. You can also add an LCD display to show data on the device itself. Data can also be stored on the cloud if you buy premium version of Blynk.

That's it from my side. I hope you found this project and guide useful. If you have any doubts, you can comment below or send me an email: saiyamagr@gmail.com

Any questions/suggestions are welcome.

Follow for more.

Thanks for reading!