Connecting the Pi to a weather station

New – Connecting the Pi to a weather station

Screen Shot 2014-12-14 at 3.16.47 PM

Following a number of requests I have created below the steps required to connect a Maplin USB Weather Station to a Raspberry Pi using the pywws python software. This latest update now uses python-usb instead of Cython making the settup easier and quicker.

What you need

or
  • A wireless ethernet connection with a powered USB hub

Notes

I found that a powered hub is not required with a direct ethernet cable to your router, keeping the power usage to a minimum, but you may require one when using a wireless dongle. Plus I had some issues with the wireless connection dropping out, and so reverted back to wired.

The weather sensors communicates to the weather station using radio frequencies, then via USB to the Raspberry Pi. The Raspberry runs a python program called pywws written and maintained by Jim Easterbrook found here. The instructions below explain the changes required in order to run on the Raspberry Pi.

I have had reports that the maplin weather station ID 1941:8021 Dream Link WH1080 Weather Station requires a powered USB hub, otherwise the Raspberry Pi can overheat. This is not the case with the Maplin USB Wireless Touchscreen Weather Forecaster.

Step 1 – Initial Setup

Create an SD card (at least 4GB) with the Raspbian “wheezy” image at http://www.raspberrypi.org/downloads.

I use a Medion 8Gb Class 4 purchased from Aldi. There will be plenty of space on this for many years of data.
Boot the Raspberry Pi with the SD card User pi Password raspberry.

Resize the partition to the full capacity of the card using the raspi-config menu. If it does not start automatically or you are using ssh, type sudo raspi-config at the command prompt.

Change the password to something other than the default.

Set the time zone correctly as an incorrect time will cause problems with synchronising to the weather station later. Make sure your weather station has the correct time as well.

As we won’t be using much GPU, set the memory split and change the value to 16.

I usually set ssh to enable so that I can control the Pi from another machine and therefore won’t require a monitor on the Pi in future.

Finally exit the menu and update the Pi using the commands:

  • sudo apt-get update
  • sudo apt-get upgrade

Reboot by typing

  • sudo reboot

Step 2 – Installing the software

Install pip and python-dev

  • sudo apt-get install python-pip
  • sudo apt-get install python-dev
  • sudo apt-get install python-usb

Step 3 – Install pywws

Install the latest version of pywws http://pypi.python.org/pypi/pywws/ to the weather directory

  • cd ~
  • sudo pip install pywws

Step 4 – Testing

Connect the weather station to the Pi with the USB connection.

Test the weatherstation

  • sudo python -m pywws.TestWeatherStation

You should see a series of Hex numbers if the connection is working.

How to configure pywws

Following on from the tutorial on how to install the libaries and connect a Maplin Weather station to the Raspberry Pi. I have included below some pointers on how to configure pywws. This does not replace the excellent instructions provided by Jim Easterbrook at https://code.google.com/p/pywws/ , but highlights some of the areas that may be missed or that are specific to the Raspberry Pi.

Assumptions

That you already have pywws installed and have successfully run the python script TestWeatherStation as shown in step 4 of the previous tutorial

Accessing the USB as pi user

When running TestWeatherStation we needed to add sudo before python. It is much better to run as the pi user. To give the pi user access to the usb port:

Create a new group called weather and add pi user to the group

  • sudo addgroup –system weather
  • sudo adduser pi weather

Check the idVendor and idProduct for the WeatherStation by disconnecting from the pi, running

  • tail -f -n 0 /var/log/kern.log

then reconnecting the station
Press Ctrl C to finish the kernal log monitoring
Look for idVendor=1941 and idProduct=8021. These may be different for different Weather Stations, but identify the station thats plugged into the usb port.
Create a rule that sets the usb port to the group weather

  • sudo nano /etc/udev/rules.d/39-weather-station.rules

Add the following lines changing the idVendor and idProduct if required

  • ACTION!=”add|change”, GOTO=”station_end”
  • SUBSYSTEM==”usb”, ATTRS{idVendor}==”1941″, ATTRS{idProduct}==”8021″, GROUP=”weather”
  • LABEL=”station_end”

Save and exit, then restart the RPi
Test the weather station connection again without sudo

  • python -m pywws.TestWeatherStation

Additional Libraries

In order to connect to twitter and produce graph plots you will need to install the following:

  • cd ~
  • sudo pip install python-twitter
  • sudo pip install oauth2

And to use the graphs you will need gnuplot

  • sudo apt-get install gnuplot 

To use secure ftp transfer you will need

  • sudo apt-get install python-paramiko
  • sudo apt-get install python-pycryptopp

Directories

Assuming pywws is installed using pip
A directory is required to store the text and graphics templates, separate from the examples so that I could always refer to them and they won’t be over written.

  • mkdir ~/weather
  • cp -R /usr/local/lib/python2.7/dist-packages/pywws/examples/* ~/weather

We will also need a data directory to store the weather.ini and processed weather data

  • mkdir ~/weather/data
  • mkdir ~/webdata

Finally there needs to be a directory to store the web pages created from the templates

  • mkdir /home/pi/weather/temp

Configuring weather.ini

You should really be following the instructions by Jim Easterbrook, but here I describe the setup on my standard image.
I assume you have already completed the first 2 tutorials and the weather station can talk to the pi

The Objective

The objective is to configure the weather.ini file so that it writes example data to files on the Raspberry Pi. This does not require a web site setting up, but it is then quite easy to redirect the output to your web site instead.

Step 1 – Downloading the data

Connect your weather station and run the Logdata command to create create a dummy weather.ini file in ~/weather/data using

  • python -m pywws.LogData -vvv ~/weather/data

You can now edit this file using Jim Easterbook’s instructions or if you are using a Maplin Weather station, download one I created earlier using

Run the logdata command again to download stored weather information from the station. This can take some time.

  • python -m pywws.LogData -vvv ~/weather/data

You should now have some data files in the data directory
Check using

  • ls ~/weather/data/raw

These are just the raw data files and they need to be processed using

  • python -m pywws.Process ~/weather/data

Step 2 – Outputing the data

The supplied weather.ini file is designed to output text and graphics to the directory ~/webdata
The ini file specifies which templates are used and at what intervals.

In order to produce some output the Hourly command needs to be run at regular intervals.
Try the command manually first

  • python -m pywws.Hourly -vvv ~/weather/data

This should produce a number of output files in ~/webdata directory. To see them type

  • ls ~/webdata

These are the files that could go to your web site for publication to the world.
In order to do this the ~/weather/data/weather.ini file needs to be edited and the ftp section changed
[ftp]
local site = False
secure = False
site = ftp.your_isp.co.uk
user = username
password = userpassword

Step 3 – Automating the data

In order to run the Hourly command at regular intervals, a crontab needs to be setup.
This is achieved using

  • crontab -e

Then edit the file to include the line

  • 5 * * * * python -m pywws.Hourly ~/weather/data >> /home/pi/weather.log 2>&1

Save this and the pywws program will access your weather station every 10 mins and output html code or graphics to your web site or to ~/webdata

Using a prebuilt image

To assist those who do not want to following the instructions in the tutorial, I have created a Raspberry Pi image, based on the 2014-06-20 wheezy raspbian and pywws version 14.06.1.

The image can be downloaded from New Image Download . The image is 1.3Gb in size so may take some time

Assumptions

The image has been built on a Raspberry Pi Model B revison 2.0 and a 4gb SD card, with a direct ethernet connection. I would recommend using an 8gb card for additional storage capacity.

The weather.ini file is setup for a Maplin USB weather station, but could be easily edited for other supported stations

Installing

Download the zipped image from google drive New Image Download

Unzip the image and use the instructions at http://elinux.org/RPi_Easy_SD_Card_Setup#Easy_way to install the downloaded image to your Raspberry Pi

Boot the Raspberry Pi from the SD card with the weather station attached.

Login using the default user pi with password raspberry

Run ‘sudo raspi-config‘ to configure the Raspberry Pi setting the correct date and time, plus expand the image to fill the SD card.

Default Setup

The Raspberry Pi should boot normally. There is a crontab that runs the Hourly python script every 10 mins.

The first time Hourly.py is run it could take some time to download all the data from the Weather Station so leave it alone for a while.

Each time the script is run the output is written to a log file /home/pi/weather.log. Check this to see if everthing is running correctly using the command more /home/pi/weather.log

Use the space bar to move to the next screen of output.

The default setup is designed to download and process the weather station data every 10 mins. It will then collect summary data and publish it locally to the directory /home/pi/webdata

The published data contains examples of text, xml and graph images that would normally be used on your web site.

All this is controlled by editing the pywws configuration file /home/pi/weather/data/weather.ini

To edit it use the file editor nano. ‘nano /home/pi/weather/data/weather.ini’

In order to upload the data to your own web site, edit the ftp section of the weather.ini file, remembering to change local site = False

When making changes I would suggest only making small ones, then checking the logfile more /home/pi/weather.log for errors.

Or to see just the last 50 line type tail -n 50 /home/pi/weather.log

The image is setup with the libraries required to tweet if required

Conclusion

The image is designed to help those with little experience of the Raspberry Pi connect it to a weather station.

It still requires some configuration and will require a website to publish the information, unless you want the Raspberry Pi to act as the web server.

I use www.freevirtualserver.com which supports php.

Good luck!

Advertisements