DIY Weather Station
**This project DOES require soldering.**
I decided to create my own weather station because I wanted to know a bit more about was going on at my house, for several reasons, and not just what it was like in the general area. I love to tinker and thought that this would be a bit of fun and also good to share. I did most of this on my own, but I did motify some of this from other projects. I've worked out all of the code and tested it by hand, but I am still waiting on some weather vane parts. When that is completed I will update this page.
This project is centered around the ESP32 and designed to display a web page with information formatted so that it can be collected by a program called Prometheus (I will not be covering setting up Prometheus in this instructable). This project is one where you can use all of it, or only the parts that you want. Also, after putting everything together I noticed a few things that I may have done differently and will share those at the end, which I hope may give you some ideas or make your weather station better.
Thanks to the following:
- Tecwyn Twmffat for inspiring the wind vane (although I heavily edited the code to shorten it and adapt it to my usecase)
- How2electronics for helping me figure out my code for the anemometer
- Random Nerd Tutorials for helping me understand a bit about how the ESP32 reads voltages (also for the anemometer). The ESP32 Documents were also very helpful with this.
Supplies
Main Board:
ESP32 DevKit ESP32-WROOM - It needs to be this pinout, or else you will have to adjust the wiring and/or code to match the board you get.
Temperature, Pressure, Humidity:
BME280 - Note: I used the Adafruit board but you can use any BME280 board. However, if you do, make sure the pins are connected properly.
Anemometer (Wind Speed):
Adafruit Anemometer - if you use a different one the code and board likely will not work so you would have to adjust them.
Wind Vane:
Bourns Encoder 8-bit (128 positions) - This is for the windvane. I will discuss an alternative at the end in my lessons learned section.
Steel Rods - I used these rods, along with the couplers, to connect the encoder to a wind vane. I am making my own and will be welding it to the rod shown here. However, you can connect whatever windvane you'd like; just make sure it will have enough leverage to turn the encoder.
Bearings - I used these to make sure that the wind vane had something solid to support it while being able to turn easily. If you use different steel rods make sure that the bearings will fit them.
Couplers - I used the ones listed here and had to drill one out slightly because the encoder dial is 1/4 inch in diameter which is 6.35mm and I used the 6mm rods. Alternatively you could use an adapter coupler like this one.
(optional) O-rings - While these are nice, and help improve the water resistance, you can't put them too tightly to the bearings or the friction will be too high to make the wind vane functional.
Power:
This is probably the most flexable section, so long as you meet minimum requirements. For more details see step six.
Battery and battery box for power. If you don't use this option and elect to plug it in directly to the main electrical, make sure to protect it from the weather.
Power converter - Changes 12V from the battery/solar panel to 5V USB for the ESP32 board. You can substitute this one for any that work for your use case.
Solar Panel - See step 6 for why I chose this one.
Housing/Mounting:
Weather sensor housing - I chose this one because it was inexpensive and would allow me to fit my own board in it. You can use any type you'd like, just make sure that it allows for air flow while blocking out rain. If you had a 3D printer you might even be able to print something like this.
This is where you will have to be creative and do what works for you. I used some 1.5" Black ABS pipe parts and a piece of packing foam to hold the rotary encoder. You may be able to find better options, or design/print one if you have a 3D printer or a friend with one.
Connecting everything/PCB:
If you use my PCB you will need these female headers, or some like them. If not, you may still find them useful.
(Optional) Pin headers - to connect the wind vane wires to the header.
(Optional) Breadboard wires - these can be used to connect and trouble shoot items, or can be used as part of the final project.
After soldering in the wires for the anemometer I discovered that it would have been much easier to have a connector on the board instead of placing them on the wires. So, I designed the board so that the wires could be soldered or you can use this male connector along with this female connector; or you can use this kit if you want to do wire to wire.
I designed a custom PCB for this as the wires would become an absolute mess otherwise, and I didn't want to perminently solder stuff together. You can use a prototype board or, if you'd like, I have all the files you need to order my board from JLCPCB in a single zip file here (instructable doesn't allow me to post these files). It cost me about $45 for 5 boards (their minimum order) and they mounted the resistors for me.
Location, Location, Location
The first thing that you will want to do is decide what you want to do with your weather station and where you would like to place it.
I chose a metal pole that was part of the fence in my backyard. This allowed for better wind detection and open view of the sky for the solar panel, but also means that the temperature sensor might get a bit hotter than if I placed it in the shade. If you don't plan on using a windvane you might be able to make the whole setup smaller, but you will still need to make sure the anemometer isn's sheltered from the wind or you won't get a good wind speed readings. If you don't care about anything but temp and humidity you can make the whole thing MUCH smaller and might even be able to use a different battery and charger, like a LiPo battery and charger.
The other issue that you might run into is space limitations. Not everyone has a large yard and may need to find alternative means of mounting the weather station. Having the weather station close to a building could have the added bonus of access to mains electrical and not having to use a battery and solar panel.
Ultimately, all of these things will effect what you do and allows you to personalize the setup.
Arduino IDE Setup & ESP32 Code Upload
Arduino IDE setup:
First you need to make sure that you have the Arduino IDE. If you do not, please download it from Arduino's Website.
Once you have the Arduino IDE you will have to configure it to work with ESP32 boards. In order to do this you will need to first go to "File>Preferences" and where it says "Additional Boards Manager URLs:" past this http://arduino.esp8266.com/stable/package_esp8266com_index.json,https://dl.espressif.com/dl/package_esp32_index.json and click "OK".
Then, go to "Tools", hover over the part that says "Boards:(text here may vary)", click on "Boards Manager...", search for "ESP32", select the most recent version and install it. Once the board library is installed go back to the "Tools > Boards:..", hover over "ESP32 Arduino", find and select "DOIT ESP32 DEVKIT V1".
Code Upload:
Once you have the Arduino IDE setup download the arduino sketch in this instructable. You will have to make a few edits.
First, you need to decide what parts you are using and comment out lines if you omit some part (e.g. windvane or anemometer). I have attached text document that tells you which lines to comment out if you aren't using certain parts (note: if you substitute parts, not just omit them, the code may or may not work). Also, If you don't use Prometheus you can uncomment lines 321 through 393 to get a text based cardinal direction function ('cardinal_val("variable with wind degrees")') that you can use in your code.
Second, you will need to make sure that you update lines 33 through 36 for your wifi, desired port number which you will use later (make sure it's above 1024), and naming the device (a.k.a deviceID) (note: lines 33 & 34 are a minimum and must match your wifi).
Once you have updated the code you can plug in the ESP32 board and upload the code (note: IF you use a different version of ESP32 board this code may or may not work).
BME280 - Temperature, Humidity, Pressure
This board is fairly straight forward. All you have to do is solder the pins on and plug it in if you use my custom board ( make sure the VIN pin is in the bottom/square pin on the board). If not, you will need to make sure that the pins are correctly connected (see below).
- Adafruit BME280 --> ESP32
- pin 4 (SCK) to pin 22 (GPIO22)
- pin 6 (SDI) to pin 21 (GPIO21)
- VIN to 3.3V
- GND to GND
Anemometer
The anemometer is also fairly straight forward, but it does require a power supply greater than 7V so I just use the 12V from the battery/solar panel in my setup. If you use my board it is clearly labeled and you simply connect the corresponding wires and you're good to go. If not, follow the wiring below.
- Blue to ESP32 "VN" which is pin/GPIO39
- Black to the ground on the power source AND on the ESP32
- Brown to the positive of the power source
Wind Vane
Here is where things get a little more complicated, since it's not quite plug and play.
Connecting the rotary encoder:
You will have to identify the pins on the rotary encoder since they are not labeled, this took me a little to figure out and be confident about. The best way to do this is to find the little pla192stic bump on the side (visible in the picture) and make ure that is facing up, this is your index. From tere, the pins on th left (from top to bottom) are 1, 2, 9, 3, 4; the pins on the right (from top to bottom) are 8, 7, 10, 6, 5. If you'd like to double check this just double check it against the data sheet from where you purchased it.
Once you have the pins identified you will have to connect them. If you are using my board, all you have to do is connect the pins from top to bottom; pin 1 on the top and pin 10 on the bottom. I have also connected pins 9 & 10 on the board (since they are both ground) which allows you to connect the center pins (9 & 10) on the rotary encoder and run 1 wire to the board to save on wiring. I used a piece of network cable to connect the 8 data pins and then used 1 separate wire for the ground (see picture). You may consider soldering these to a pin header (listed as optional in parts list), or some that you have around, but keep in mind that if you keep them together it will make it more difficult to remove from the weather housing. For that reason I just soldered one per wire and kept them separate.
If you are not using my board, you need to make sure that you have a 10K ohm pull-up resistor (click here if you don't know what that is) to 3.3V for each signal wire (1 through 8) and make sure they are connected to the correct pin.
- rotary encoder --> ESP32
- 1 to pin/GPIO 32
- 2 to pin/GPIO 33
- 3 to pin/GPIO 25
- 4 to pin/GPIO 26
- 5 to pin/GPIO 27
- 6 to pin/GPIO 14
- 7 to pin/GPIO 12
- 8 to pin/GPIO 13
- 9 to GND
- 10 to GND
Testing the encoder:
After connecting the rotary encoder you should test it. There are two ways to test the encoder, one requires you to edit the script, which is most accurate and has a faster response, the other does not require you to edit the code but does require you to find out the divice IP address, is a little slower to respond, and you are less likely to catch issues.
- The first way to test it is through the serial port; to do this you will need to add some lines of code. Remember that you will have to delete this when you are finished.
- Go to line 166 and hit enter 2 times to create some space and then click on line 167 and hit TAB
- Paste the following: Serial.println(current_position);
- Save and upload to the ESP32
- Open the Serial Monitor by clicking the magnifying glass in the upper right hand corner (in the dark blue green area), nake sure the baud rate is 9600
- This allows you to see the position. Slowly turn the rotary dial clockwise making sure that the numbers increase without jumping around randomly. If the numbers jump around randomly then you have the wires mixed up and need to check your wiring.
- Once the rotary encoder is smoothly advancing and decreasing positions (0 through 127) I like to set it to 0 and try not to move it after that. You can confirm the position later without haveing to go through these steps again by just pulling up the website where the data is posted.
- now delete lines 167 & 168, save, and upload to the ESP32 again.
- The second way to do this is by pulling up the website where the data is served. We kind of have to skip a step or two to get there. REMINDER: this is NOT as accurate/reliable for trouble shooting.
- First you have to discover what the IP address is that got assigned to the ESP32
- Once you have the IP address (I will use 192.168.1.123 and port 9921 as an example) you have the IP address you will go to your web browser and type "192.168.1.123:9921/metrics"
- This gives you all of the data that your weather station will provide. You will be looking for "# HELP wind_degrees 8-bit DIY windvane direction in degrees"
- Slowly turn the rotary clockwise just a little and refresh the web page. Repeat and make sure that the degrees are increasing and not jumping around. If the degrees are jumping around and are not linearly increasing the wiring is likely messed up and needs to be double checked.
- Once you are satisfied you can move to the next step
Mounting:
Hopefully this is something that you already worked out in step 1. If not, this something that you need to think about and test now, but likely won't complete until you go to mount the whole system. I used 1.5" ABS pipe cap (looks like black PVC pipe) to contain the rotary encoder itself and made an "L" shape with the cap with the encoder pointing down to avoid having rain leak in. For the windvane itself I am using the steel rod and bearings through a 1/2" PVC pipe, the bearings happen to press into the 1/2" PVC nicely, and mounting the PVC pipe to a board with a pipe clamp. This will provide support while allowing the wind vane to spin freely.
Power
***DISCLAIMER: When using any kind of power there are dangers. DC power can be much more dangerous than many people think. I am by no means an expert and anything you do is at your own risk. Please make sure to take precautions and avoid shorting any power source or touching both contacts at the same time.***
This part is where you have the most options and what I list in this instructable is simply what I chose. I will step through my logic to help you make your own decisions.
Power considerations:
ESP32 - 5V (141mA max from everything I've read); either through the USB port (which is what I use) or through the VIN pin on the board. Note: if you use a different ESP32 board the VIN voltage could be different.
BME280 - 3.3V (usually from the ESP32). The sensor itself uses micro Amps, but I couldn't find exactly how much this specific board draws, probably a bit more with the converters and LED.
Anemometer - between 7V & 24V, and approximately 4-20mA. I chose 12V because it's common and I had a 12V battery.
Rotary Encoder for the Wind Vane - I have no idea how much power this draws since it basically acts like a bunch of resistors, but I suspect it's relatively small.
Total - I would estimate needing about 1.5 watts at a minimum. I have not actually measured the power consumption and don't plan to. If anyone builds this and measures the power consumption, please post in the comments below for the benefit of all.
Power Supply:
Mains Power - This option wasn't practical for me, but if it is for you just keep in mind the requirements above.
Battery - I first used an old Sealed Lead Acid (SLA) battery from a worn out Uninterruptable Power Supply (UPS), but then bought a new one for realiability and because it wasn't too expensive. Another consideration is opperation temperature. If it gets really hot where you are, some batteries can become hazerdous.
Charger - This is another reason I chose to go with an SLA battery. Many solar panels come with a charger that is compatible with SLA batteries, but not Lithium batteries. Using the wrong type of charger can be dangerous, resulting in damaged equipment, a fire, and possibly exploding the battery. So, if I wanted to use a battery that wasn't a lead acid battery I would need a different charger, which just added cost; which was a bigger factor than space for me.
Power Converter - I chose the power converter listed above because it was inexpensive, had a USB-A plug already built in, and had 2 inputs. This allowed me to hook the battery and solar panel to the input side, making it possible for the panel to charge the battery and power the circuit when making excess power while allowing the battery to kick in for the other times. Additionally, with two outputs, it made it possible for me to use a standard USB cable or to wire something directly if I chose. If you chose to go with a Lithium battery you may find a charger that fulfills these needs as well.
Solar panel - I chose this panel because it has a battery charger/tender built in and should supply enough power to keep everything charged (even on cloudy days) with extra capacity to add other things in the future. It also came with a mount that made it easy to use in my setup.
You may be able to get away with a different, and possibly cheaper, panel since the ESP32 and BME280 together use approximately 150mA at ~5V and the anemometer uses approximately 4-20 mA at 12V.
Storage/Protection:
Regardless of whether you use a battery or mains power you will need to protect your electronics from the weather. I chose a standard battery box for a trailer because it was cheap, easy to find, and left room to put the voltage converter out of the rain.
Collecting Data/project Notes
The code in the ESP32 module creates a web page for Prometheus to scrape and display in Grafana. I would outline how to do that, but I started this tutorial over a year ago and since lost the configuration. So, I will have to figure it out again and will update this post when/if I do.
An additional notes:
I had some issues with the battery lasting through the night. I don't know if something in my code was making the board pull a ton of power, if my wifi was too far away and making the program constantly work on the connection, if the polling rate is too frequent, or if there is parasitic draw somewhere. But, you may consider plugging the weather station into power or doing some trouble shooting to make the power consumption go lower.
I also couldn't get my wind vane to work properly. I think it was too heavy and didn't work with the resistance in the rotary encoder. The post I based mine on seemed like they had success, so they might have tips. Check the link at the top of the post.