Installation of Pywws on Raspberry Pi 2 for Weather Station

This is the process I followed to install pywws on a Raspberry Pi 2 on Raspian.  I have a 3080 compatible weather station connected to the Raspberry Pi 2 via USB.

Install Dependencies

As pywws is written in Python, it has a few dependencies which need to be installed on the Raspberry Pi 2.  I like to make sure that before this I update to the latest updates.

sudo apt-get update
sudo apt-get upgrade

Now install the python, libusb and gnuplot.  Libusb as we need to access the weather station via USB and gnuplot in order for draw graphs with pywws.

sudo apt-get install python-pip python-dev python-usb python-daemon gnuplot libusb-1.0.0

There are other dependencies if you want to use other features or pywws (eg. twitter), but these will give you the basics to get pywws up and running.  Other dependencies can be found here.

Install Pywws and permissions

After all the required software is installed, you can install pywws.

cd ~
sudo pip install pywws

After it has installed, plug in the internal weather station unit to the Raspberry Pi 2 via USB. Now you can test to ensure it can talk to the weather station.

sudo pywws-testweatherstation

This command should produce some output that looks like this.

pi@pi2 ~ $ sudo pywws-testweatherstation
22:09:41:pywws.Logger:pywws version 15.01.0
0000 55 aa df f9 00 27 15 01 28 09 36 15 01 28 11 37 05 20 02 53 11 00 00 00 10 00 00 c0 0c 00 d8 a0
0020 c8 27 f4 27 00 00 00 00 00 00 00 15 04 17 22 09 41 23 c8 00 00 00 45 2d 2c 01 64 81 c8 00 00 00
0040 64 40 64 80 a0 28 80 25 a0 28 80 25 03 c3 00 05 81 01 00 0a 00 f4 01 12 00 b9 c5 2d 0a 0f bf 63
0060 19 00 58 18 63 13 69 01 a0 00 76 01 e0 80 76 01 e0 80 09 01 fd 80 35 28 28 27 fa 27 ed 26 3d 00
0080 63 00 10 02 b0 04 98 0a 04 0e 42 0f 00 15 02 21 09 06 14 08 03 13 07 15 02 21 14 22 14 08 03 12
00a0 58 15 01 16 16 28 14 08 01 07 10 15 01 01 06 35 14 08 29 02 10 15 01 01 06 35 14 08 29 02 10 1d
00c0 01 27 11 14 15 02 08 01 23 14 08 06 20 28 15 01 16 16 23 14 08 06 20 28 15 01 16 16 23 14 11 11
00e0 05 08 15 02 07 13 23 15 02 08 02 00 15 02 21 00 00 15 02 22 00 00 15 03 01 00 00 15 02 27 09 33
pi@pi2 ~ $

If this does not work, further information is available at http://jim-easterbrook.github.io/pywws/doc/en/html/guides/getstarted.html which may help. You have to run pywws in python using sudo. It is much better to run it as the user Pi and the give the Pi user access to the USB port. First create a group to grant the access and add the Pi user to the group

sudo addgroup --system weather
sudo adduser pi weather

You need to identify the idVendor and idProduct for your WeatherStation that is plugged into the USB port. Look into the kernal log file.

cat /var/log/kern.log | grep hid

Look for idVendor and idProduct, mine were idVendor=1941 and idProduct=8021. My kern.log look like this.

[254954.344198] hid-generic 0003:1941:8021.0003: hiddev0,hidraw0: USB HID v1.00 Device [HID 1941:8021] on usb-bcm2708_usb-1.3/input0

To modify the USB, create a rule that sets the usb port to the group weather

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

Copy and paste the following into this file. Make sure you update the idVendor and idProduct to suit your USB.

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

After you’ve saved and exit, reboot the Pi. Once the Pi has started again, test connecting to the weatherstation as user Pi, without sudo.

python -m pywws.TestWeatherStation

You should get the same results as you did above when you tested using the command with sudo. As we’ll be running this as user Pi, ensure that all the weather data is owned by user Pi.

sudo chown pi.pi -R /home/pi/data/weather

Configuration of Pywws

I wanted readings every 5 minutes from the weather station and as pywws can easily read and log the weather station data every 5 minutes (recommended setting is 5min too), this was ideal. So, I set the weather station to log every 5 minutes.

sudo pywws-setweatherstation -r 5

It’s worth noting that the memory in the internal unit is not very large and although it will buffer some data, it cannot hold very much weather data. Now to setup where to log the data.  I chose /home/pi/data/weather, but it could be anywhere.  If a different directory is used, update the url through the rest of this page to the select directory.

mkdir /home/pi/data
mkdir /home/pi/data/weather
mkdir /home/pi/data/weather/data

All the weather station’s logged data should go into this directory. As per the getting started doco above for pywws, we now need to setup the timezone and weather station type. We will need to run the following command and it will create the configuration files the first time it is run.

sudo python -m pywws.LogData -vvv /home/pi/data/weather/data

The configuration files are found in the data directory /home/pi/data/weather/data. I adjusted the following in the weather.ini configuration file.

ws type = 3080
logdata sync = 1
day end hour = 0
asynchronous = False

There is a full page in the doco about the weather.ini configuration settings here. I also update the configuration paths in the weather.ini file.

work = /tmp/weather
templates = /root/weather/templates/
graph_templates = /root/weather/graph_templates/
local_files = /root/weather/results/

My full weather.ini looks like this.

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

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

[ftp]
local site = False
site = ftp.username.your_isp.co.uk
user = username
password = secret
directory = public_html/weather/data/
secure = False
port = 21

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

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

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

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

[daily]
services = []
text = []
plot = []

Now create these directories and copy the pywws examples into them.

pi@pi2 ~/data/weather $ pywws-version -v
15.01.0
commit: 06fb57d
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:
   http://jim-easterbrook.github.com/pywws/
pi@pi2 ~/data/weather $ cp -r /usr/local/lib/python2.7/dist-packages/pywws/examples/templates /home/pi/data/weather/templates
pi@pi2 ~/data/weather $ cp -r /usr/local/lib/python2.7/dist-packages/pywws/examples/graph_templates /home/pi/data/weather/graph_templates
pi@pi2 ~/data/weather $ mkdir /home/pi/data/weather/results
pi@pi2 ~/data/weather $ ls -al
total 24
drwxr-xr-x 6 pi pi 4096 Apr 19 23:22 .
drwxrwxrwx 6 pi pi 4096 Apr 16 22:01 ..
drwxr-xr-x 7 pi pi 4096 Apr 19 22:44 data
drwxr-xr-x 2 pi pi 4096 Apr 19 23:20 graph_templates
drwxr-xr-x 2 pi pi 4096 Apr 19 23:22 results
drwxr-xr-x 3 pi pi 4096 Apr 19 23:19 templates

Then we run the command again.

python -m pywws.LogData -vvv /home/pi/data/weather/data

This will probably need to run for a while to download the data. It should have some output similar to this.

pi@pi2 ~/data/weather/data $ sudo python -m pywws.LogData -vvv /home/pi/data/weather/data
23:32:54:pywws.Logger:pywws version 15.01.0
23:32:54:pywws.Logger:Python version 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3]
23:32:54:pywws.WeatherStation.CUSBDrive:using pywws.device_pyusb
23:32:56:pywws.DataLogger:weather station type appears to be incorrect
23:32:57:pywws.DataLogger:weather station type appears to be incorrect
23:32:57:pywws.DataLogger:Synchronising to weather station
23:32:57:pywws.weather_station:delay 3, pause 17.6943
23:33:16:pywws.weather_station:avoid 5.72013122047
23:33:22:pywws.weather_station:live_data new data
23:33:22:pywws.DataLogger:Reading time 12:33:19
23:33:22:pywws.DataLogger:log time 12:29:37
23:33:22:pywws.DataLogger:Fetching data

So now that the weather station data can be downloaded, I want to setup a service that will run to download the weather data. There are two options, an hourly update or a live logger. I opted for the live logger as I wanted data updated as close to every 5min as possible. Pywws comes with a livelogger than can run as a daemon.  It can be run like this.

pywws-livelog-daemon -v /home/pi/data/weather/data /home/pi/data/weather/data/livelogger.log start

While this works well, it will not start automatically. A service init script is much easier to manage for me. Create a new init.d file by running the following.

sudo nano /etc/init.d/pywws

Now copy and paste the following into this file. Update the DAEMON, DATADIR and LOGFILE to suit your installation.

#!/bin/sh

### BEGIN INIT INFO
# Provides:        pywws-livelog-daemon
# Required-Start:  $all
# Required-Stop:   $all
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start pywws daemon
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

. /lib/lsb/init-functions

DAEMON=/usr/local/bin/pywws-livelog-daemon
DAEMON_NAME=pywws
DATADIR=/home/pi/data/weather/data
LOGFILE=$DATADIR/livelogger.log

DAEMONUSER=pi
UGID=$(getent passwd $DAEMONUSER | cut -f 3,4 -d:) || true

. /lib/lsb/init-functions

case $1 in
start)
        log_daemon_msg "Starting pywws service" "pywws"
        if [ -z "$UGID" ]; then
                log_failure_msg "user \"$DAEMONUSER\" does not exist"
                exit 1
        fi
        sudo -u $DAEMONUSER $DAEMON -v $DATADIR $LOGFILE start
        status=$?
        log_end_msg $status
        ;;
stop)
        log_daemon_msg "Stopping pywws service" "pywws"
        sudo -u $DAEMONUSER $DAEMON -v $DATADIR $LOGFILE stop
        log_end_msg $?
        ;;
restart|force-reload)
        $DAEMON -v $DATADIR $LOGFILE restart
        ;;
try-restart)
        if $0 status >/dev/null; then
                $0 restart
        else
                exit 0
        fi
        ;;
reload)
        exit 3
        ;;
status)
        status_of_proc $DAEMON "pywws service"
        ;;
*)
        echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
        exit 2
        ;;
esac

Once the init script has been saved, it needs to be made executable and added to rc.d so it will start and stop at startup and shutdown.

sudo chmod +x /etc/init.d/pywws
sudo update-rc.d pywws defaults

You can also start/stop pywws as a service.

sudo service pywws start
sudo service pywws stop

References

http://www.weather-watch.com/smf/index.php?topic=39257.0
https://jim-easterbrook.github.io/pywws/doc/en/html/index.html
https://groups.google.com/forum/#!topic/pywws/r9KfWM_AgKY
https://github.com/borpin/pywws-scripts/blob/master/pywws-livelog

Advertisements

One thought on “Installation of Pywws on Raspberry Pi 2 for Weather Station

  1. happy user June 28, 2016 / 2:36 AM

    works fine for me Thanks!

    all on Pi3

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s