Today I’m going to show you how to make sort of a mini weather station with a Raspberry Pi. This is an ongoing project of mine that I’ve been tinkering with so I’ll share my learnings with you. It started with a project I wrote about last month, and is an improvement on it.
A core part of the “Internet of Things” movement is the idea of devices that gather data and send it to the Internet. That data is then acted on or observed for later. It’s a simple concept and has been going on for a while but lately it’s been getting cheaper and easier to do. This project is a great example of that.
While this seems like a long tutorial, it only takes about 30 minutes to complete. Let’s get started.
Once you complete this your Raspberry Pi will measure:
- Temperature
- Humidity
- Atmospheric Pressure
- Lux
You can send your results to:
- Google Spreadsheet on your Google Drive
- ASP.Net Web API on your website
What you’ll need
For this project you will need:
- Raspberry Pi 2 or Model B - (Get one here for $35!)
- AM2302 Temperature / Humidity Sensor
- BMP180 Temperature / Barometric Pressure Sensor
- DS18B20 Waterproof Temperature Sensor
- TSL2561 Digital Lumosity Sensor
Optional:
- Breadboard and jumpers (Get a kit here)
None of this stuff would be anywhere near as easy if it weren’t for companies like Adafruit. I recommend getting your sensors from them to support their innovation in this space. Newarkis one of the original distributors of the Raspberry Pi, they are fast and very reputable.
Wiring
The wiring for this project is shown below:
While this may look confusing at first, it’s a pretty standard setup for each sensor. Here’s some additional information on how to wire up each one if the diagram isn’t helpful.
Installing the software
This tutorial has been tested with a fresh install of Raspian. The instructions should be fairly accurate for other Linux distributions as well.
SSH into the pi (or open a command window) and get’s start out in your home directory and make a folder for your sources.
Now we can start installing sensors.
Setup the AM2302
The AM2302 is a temperature humidity sensor. It is a DHT22 sensor that’s wired with a pullup resistor and casing. I found it easier to use this one than just the DHT22 kit.
For this sensor we’ll be using the Adafruit Python DHT library:
This will install the software necessary to interact with the AM2302
Test the AM2302
There is a test script you can use to verify the sensor is working. Run AdafruitDHT.py and “2302” is the sensor, and “22” is the pin you’re using. Change it if you hooked it to a different pin.
You should see something that looks like this:
DSB18B20
The DSB18B20 is a digital one wire (Dallas one wire protocol) thermometer. If you get it “with extras” it comes with a cord and waterproof casing which is what I went with.
You will need to add one wire Support. Open up your boot config:
Add the following text to that file: dtoverlay=w1-gpio
Save the file and reboot:
Testing the DSB18B20
To test it you’ll need to load the following modules into your kernel:
Note: you can do the following to add them to your kernel on boot:
add the following lines:
Save the file and the next time you start they will be loaded automatically.
To check your device:
You will see a folder starting with 28- this is your device based on the serial number.
You should see something like the following:
If it says “YES” it’s working. If not, you may need to check your wiring.
Configuring I2C
Next you will need to configure I2C for your system:
You may get “already installed” messages from this, depending on how your system is setup.
Add the modules to your kernel on startup. Open up /etc/modules
Add the following to it:
Now you will need to modify your boot config:
Add the following:
Reboot your Pi:
Testing I2C
You can see what devices are connected to your I2C bus by running the following command:
You should see something like this:
You’re ready to go!
TSL2561 Sensor
The TSL2561 is a Lux Sensor, it measures the amount of light it detects, useful to see how bright the sky is that day.
Let’s grab some sources to help out with this:
Testing the TSL2561
To test out your sensor run the following:
You should see something that looks like this:
Its up!
BMP180
The BMP180 is a barometric pressure sensor with a thermometer. It’s also really easy to get working. Once again we’ll be using some Adafruit libraries:
Testing the BMP180
To test it:
It should look like this:
Gather data from all the sensors
Here we will gather the data from all the sensors using an application I wrote. This reader will gather all the information and display it to the console, or send it out to the internet.
This will copy the scripts into a folder called “reader”. To test them out, type in:
This does a “dry run” that does not send the data anywhere, but displays it on your screen:
You’re ready to go! If you run it without the “dryrun” parameter it will build a JSON object and attempt to send to a web endpoint, which I will show you how to build later in the tutorial.
Optional: Google Spreadsheets
You can output data to a Google Spreadsheet using my application you downloaded from GitHub. You will need to setup OAuth with Google, and create a JSON file. Instructions are here:
http://gspread.readthedocs.org/en/latest/oauth2.html
You will want to store the generated JSON file in the /home/pi/reader/ folder.
One thing you will need to is open up that OAuth JSON file and look for “client_email”. It should look like this:
“client_email”: “1233453343244-asdlkjried8ss98eeEic@developer.gserviceaccount.com”,
After you create your spreadsheet, you will need to add that email address as one who can access the sheet under sharing settings: