Installation of pywws on a Raspberry Pi

Weather StationToday, I will show how to use the Conrad weather station with a Raspberry Pi running Raspbian. We will discuss how to obtain weather data and create some charts. These are pre-requisites if you later want to upload the data to Weather Underground, or to other weather services.

Installing Dependencies

pywws is written in Python and has a few dependencies we need to install. Beforehand, however, it is always a good idea to ensure that our system has the latest updates.

$ sudo apt-get update
$ sudo apt-get upgrade

The dependencies required by pywws are documented here. pywws requires at least version 2.5 of Python. At time of this writing, the Raspbian distribution included Python 2.6.

Python Package Index. Many of the dependencies can be installed from the Python Package Index. Yes, that means that some packages will be installed as deb packages while others are managed by pip. You could of course try to install all the Python packages with apt-get instead.

sudo apt-get install python-pip

USB Libraries. The weather station is connected to the Raspberry Pi via USB, hence we need the USB libraries.

sudo apt-get install libusb-1.0.0 python-usb

pywws Tasks. pywws executes many commands using a cron job like syntax. For this functionality, pywws requires the croniter library. If you want to have pywws generate reports about the weather past weather, you need this library.

$ sudo pip install croniter

Generating Graphs. We certainly want to generate weather graphs. Thankfully, pywws uses gnuplot rather than coming up with its own plotting solution. gnuplot is not a Python library, therefore we use apt-get for its installation.

$ sudo apt-get install gnuplot

Running pywws. There are two options to execute pywws. It can be executed from cron or it can be executed as its own standalone daemon. The latter seems to be the preferred choice but requires the python-daemon package.

$ sudo pip install python-daemon

Installing pywws and Setting permissions

Installation of pywws is straight forward.

$ sudo pip install pywws

If everything works fine, we can now test the connection to the weather station and should get a nice hex dump. The values in the hex dump output will be different for different invocations.

$ sudo pywws-testweatherstation 
15:47:33:pywws.Logger:pywws version 14.06.dev1238
0000 55 aa ff ff ff ff ff ff ff ff ff ff ff ff ff ff 05 20 02 50 09 00 00 00 00 00 00 f0 0f 00 40 1c
0020 7f 27 99 25 00 00 00 00 00 00 00 14 06 29 15 44 41 23 c8 00 00 00 46 2d 2c 01 64 80 c8 00 00 00
0040 64 00 64 80 a0 28 81 25 a0 28 81 25 00 b4 00 00 68 01 00 0a 00 f4 01 17 05 00 00 00 00 00 00 00
0060 00 00 3b 1d 61 17 1d 01 e2 00 33 01 7c 00 33 01 7c 00 bc 00 0e 00 43 26 3e 25 29 28 24 27 3d 00
0080 63 00 a8 00 ca 02 e1 03 e1 03 e1 03 00 14 05 03 13 02 12 01 01 12 07 14 06 24 00 31 14 06 21 16
00a0 32 14 06 12 17 12 14 04 20 05 22 14 05 29 10 29 14 06 25 05 50 14 05 29 10 29 14 06 15 05 14 14
00c0 06 12 18 32 14 04 17 18 47 14 05 15 22 09 14 04 20 17 22 14 05 15 22 09 14 04 20 17 22 14 06 17
00e0 16 38 14 06 17 16 38 14 06 23 16 12 14 06 24 11 23 14 06 29 14 32 14 06 29 14 32 14 06 29 14 32

Since we do not want to run pywws as root, we need to make sure that it can be executed from another user. First we create a group that should be allowed to connect to the weather station and later we add an existing account to this group. In my case, I have chosen the pi account. However, this is likely to change in the future.

$ sudo addgroup --system weather
$ sudo usermod --append -G weather pi

Log out and in again for the change to become active.

Now, we need to ensure that the group we have just created is associated with the weather station whenever it is plugged in again. For that we create the following udev rule (i.e. file) in /etc/udev/rules.d/38-weather-station.rules:

# Weather Station
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="1941", ATTRS{idProduct}=="8021", GROUP="weather", MODE="0660"
ACTION=="change", SUBSYSTEM=="usb", ATTRS{idVendor}=="1941", ATTRS{idProduct}=="8021", GROUP="weather", MODE="0660"

When creating the file, ensure that the quotes are “standard” quotes, or otherwise you will spend some time debugging the rule. Also, if you use a different weather station, the values for vendor and product id may differ and hence, you may have to adapt the rules. If you need to debug the udev rule or learn more about it, have a look here.

Now, the test command should work from your normal user account without sudo:

$ pywws-testweatherstation
15:47:33:pywws.Logger:pywws version 14.06.dev1238
[...]

Now that our connection to the weather station works, we want to create a directory to log all our weather data.

$ mkdir -p ~/weather/data

At some point in time, we want to reduce the logging interval. By default the logging interval is 30 minutes which is a lot. However, when reducing this interval, pywws for some time thought my station was not logging data when executing the log commands. Ways to solve this is to wait for a night or supposedly to remove and reinsert the batteries.

pywws-setweatherstation -r 5

Now, let us get the data the weather station has been collecting so far. This step will create a lot (depending on how long the weather station has been running already) of data in ~/weather/data/raw.

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

Configuration

Before configuring pywws, let us copy the pywws templates and graph_templates examples into our own template directories. pywws-version -v shows, among others, where the example template directories are located.

$ pywws-version -v
14.06.dev1238
commit: 59caf3e
Python: 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3]
USB:    pywws.device_pyusb
examples:
   /usr/local/lib/python2.7/dist-packages/pywws/examples
docs:
   file:///usr/local/lib/python2.7/dist-packages/pywws/doc/index.html
$ cp -pr /usr/local/lib/python2.7/dist-packages/pywws/examples/templates ~/weather/templates
$ cp -pr /usr/local/lib/python2.7/dist-packages/pywws/examples/graph_templates ~/weather/graph_templates

The pywws.LogData command created a configuration file for us in ~/weather/data/weather.ini. This configuration file is relatively bare and needs to be populated. The following shows my weather.ini file. A complete overview of this file can be found here.

[config]
ws type = 1080
usb activity margin = 3.0
pressure offset = 48.6
day end hour = 0
logdata sync = 1
rain day threshold = 0.2
frequent writes = False
template encoding = iso-8859-1
gnuplot version = 4.2
gnuplot encoding = iso_8859_1
asynchronous = False

[paths]
templates = /home/pi/weather/templates/
graph_templates = /home/pi/weather/graph_templates/
work = /home/pi/weather/tmp
local_files = /home/pi/weather/results/

[ftp]
local site = True
secure = False
site = ftp.your_isp.co.uk
user = username
password = userpassword
directory = /var/www/weather/data
port = 21

[live]
services = []
plot = []
text = []

[logged]
services = []
text = []
plot = []

[hourly]
services = []
plot = ['7days.png.xml', '24hrs.png.xml', 'rose_24hrs.png.xml']
text = ['24hrs.txt', '6hrs.txt', '7days.txt']

[12 hourly]
services = []
plot = []
text = []

[daily]
services = []
plot = ['28days.png.xml']
text = ['allmonths.txt']

This file essentially sets the meteorological day to be from 0:00 to 24:00 (day end hour) and ensures that the data files generated from the templates directories (paths section) are copied to the local web server (local site and directory). The services entries in weather.ini specify which weather services to report data to (not yet used in my case), the text entries indicate the templates to be generated, and the plot entries indicate the graph_templates to be generated.

One important note, if you run pywws in daemon mode, you have to restart it, whenever you change this configuration file.

Running pywws

To run pywws as daemon, execute the following command

$ pywws-livelog-daemon -v ~/weather/data ~/weather/data/pywws.log start

After some time, we can find the generated reports in /var/www/weather/data.

Other pywws Commands

To process the raw files, execute the following command.

python -m pywws.Process ~/weather/data

If you change settings that relate to how weather statistics are compounded such as the start of the meteorological day (day end hour) or the amount of rain required for a day to be counted as a rain day (rain day threshold), you need to reprocess the raw data using the following command:

python -m pywws.Reprocess ~/weather/data

References

This entry was posted in Raspberry, Technology and tagged , , . Bookmark the permalink.

2 Responses to Installation of pywws on a Raspberry Pi

  1. NoobHacker says:

    Hi Tom,

    Thanks very much for this post – it’s the most explanatory regarding setting up the weather station.

    I seem to have run into a glitch though…after running the pywws daemon command, nothing seems to appear in the /var/www/weather/data directory.

    Any ideas around this?

  2. tom says:

    First, the obvious suggestions. Check in the [ftp] section, the “local site” and “directory” entries, they should be “True” and point to “/var/www/weather/data” and restart the daemon as well as that the user under which you run the daemon has write access to “/var/www/weather/data”. There is also a log file in ~/weather/data/pywws.log which may indicate any troubles

    If that does not work, try the process command whether (python -m pywws.Process ~/weather/data) that generates any data. If still out of luck, try generating individual reports manually, like so (also explained in the follow-up entry of this blog):
    $ python -m pywws.Plot ~/weather/data/ ~/weather/tmp/ ~/weather/graph_templates/24h_temp.png.xml /var/www/weather/data/24h_temp.png
    $ python -m pywws.Template ~/weather/data/ ~/weather/templates/24h.html /var/www/weather/data/24h.html

    If you are still out of luck, try adding the “-v” (verbose) option to the Process, Plot, or Template commands, that should give you an idea.

    Good luck, Thomas

Leave a Reply

Your email address will not be published.