树莓派建一个气象监测站

来源:互联网 发布:python 字符串求和 编辑:程序博客网 时间:2024/05/08 21:53

SEP 17TH, 2015

<iframe frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" tabindex="0" vspace="0" width="100%" id="I0_1452694374356" name="I0_1452694374356" src="https://apis.google.com/se/0/_/+1/fastbutton?usegapi=1&amp;annotation=inline&amp;width=300&amp;origin=https%3A%2F%2Fwww.jeremymorgan.com&amp;url=https%3A%2F%2Fwww.jeremymorgan.com%2Ftutorials%2Fraspberry-pi%2Fhow-to-weather-station-raspberry-pi%2F&amp;gsrc=3p&amp;jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.Q9_4YsJn3jI.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCOPZH9U_wNmAW7dvZl37z8fbyPa1Q#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh%2Conload&amp;id=I0_1452694374356&amp;parent=https%3A%2F%2Fwww.jeremymorgan.com&amp;pfname=&amp;rpctoken=19837292" data-gapiattached="true" title="+1" style="margin: 0px; padding: 0px; border: 0px none; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; position: static; top: 0px; width: 300px; left: 0px; visibility: visible; height: 24px;"></iframe>
 

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.

How to build a Weather Station Raspberry Pi 2

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:

How to build a Weather Station Raspberry Pi 2

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.

  • How to wire up AM2302
  • How to wire up BMP180
  • How to wire up DS18B20
  • How to wire up TSL2561

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.

123
cd ~mkdir sourcescd 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:

123456789
git clone https://github.com/adafruit/Adafruit_Python_DHT.gitcd Adafruit_Python_DHTsudo apt-get updatesudo apt-get install build-essential python-dev python-opensslsudo python setup.py install

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.

12
cd examplessudo ./AdafruitDHT.py 2302 22

You should see something that looks like this:

How to build a Weather Station Raspberry Pi 2

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:

1
sudo nano /boot/config.txt

Add the following text to that file: dtoverlay=w1-gpio

Save the file and reboot:

1
sudo reboot

Testing the DSB18B20

To test it you’ll need to load the following modules into your kernel:

12
sudo modprobe w1-gpiosudo modprobe w1-therm

Note: you can do the following to add them to your kernel on boot:

1
sudo nano /etc/modules

add the following lines:

12
w1-gpiow1-therm

Save the file and the next time you start they will be loaded automatically.

To check your device:

12
cd /sys/bus/w1/devicesls -la 

You will see a folder starting with 28- this is your device based on the serial number.

12
cd 28-xxxx (change this to match what serial number pops up)cat w1_slave

You should see something like the following:

How to build a Weather Station Raspberry Pi 2

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:

12
sudo apt-get install python-smbussudo apt-get install i2c-tools

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

1
sudo nano /etc/modules

Add the following to it:

12
i2c-bcm2708 i2c-dev

Now you will need to modify your boot config:

1
sudo nano /boot/config.txt

Add the following:

12
dtparam=i2c1=ondtparam=i2c_arm=on

Reboot your Pi:

1
sudo reboot

Testing I2C

You can see what devices are connected to your I2C bus by running the following command:

1
sudo i2cdetect -y 1

You should see something like this:

How to build a Weather Station Raspberry Pi 2

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:

123
cd ~/sources wget https://raw.githubusercontent.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/master/Adafruit_I2C/Adafruit_I2C.pywget https://raw.githubusercontent.com/seanbechhofer/raspberrypi/master/python/TSL2561.py

Testing the TSL2561

To test out your sensor run the following:

12
sudo python Adafruit_I2C.pysudo python TSL2561.py

You should see something that looks like this:

How to build a Weather Station Raspberry Pi 2

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:

123
git clone https://github.com/adafruit/Adafruit_Python_BMP.gitcd Adafruit_Python_BMPsudo python setup.py install

Testing the BMP180

To test it:

12
cd examplessudo python simpletest.py

It should look like this:

How to build a Weather Station Raspberry Pi 2

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.

12
cd ~git clone https://github.com/JeremyMorgan/Raspberry_Pi_Weather_Station.git reader

This will copy the scripts into a folder called “reader”. To test them out, type in:

1
sudo python readings.py dryrun

This does a “dry run” that does not send the data anywhere, but displays it on your screen:

How to build a Weather Station Raspberry Pi 2

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:


0 0
原创粉丝点击