Some time ago I created a weather station using a Raspberry PI and an off the shelf weather station, connecting the two via USB.
However, for some time not I’ve been meaning to create a weather station from scratch – i.e. one or more Raspberry PI’s which connect to the network (via Ethernet or WiFi) and directly monitor the sensors directly.
Now the problem here is that some sensors are analog – for example the leaf, soil and UV sensors I have generate an analog signal so we need an ADC (Analogue to Digital Converter) which the Raspberry PI doesn’t have.
So we have two possible solutions:
Add a Raspberry PI compatible ADC
Use an Arduino
With the parts I have available, the Arduino won, not just on available ADC channels but also with the additional digital ports available.
Now how to connect it to the PI? Well the easiest way is to use USB, however the PI only has two USB ports (one for the Model A) and as I’m intending to use Model A’s for the final station I need that for WiFi (there won’t be room or power for hubs) so USB is out.
There’s RS232 which both support, however the PI runs on 3v3 whilst the Arduino (UNO) is 5v so I need to add a level converter between the two. It also limits me to just one arduino and I might need to use more than one so another solution is needed.
Weather webcams are always popular and it is easy and free to set one up yourself. This article will show how to setup a simple USB webcam to produce still images and serve them on a local apache webserver.
At the end of the file you need to add the following:
0 * * * * root /usr/local/bin/weatherupload
Save it and now every hour it will now run.
Note: I’ve placed it there and not under /etc/cron.hourly because
cron.hourly runs once an hour, but not necessarily at the top of the hour. I a later article when I add twitter you want the tweet to be close to 0m rather than at a random time.
I’ve found in the past that using crontab -e doesn’t always work, even for root but in the core crontab it just works
Uploading to a website
First to generate the graphs pywws uses gnuplot so we need to install it:
pi@kell: ~/sudo apt-get install gnuplot
Next we need to add some details to the stations weather.ini file. At the top theres a section called paths. Make sure those point to the relevant templates and graph_templates directories in your pywws install.
Next under the hourly section you should see a pair of lines:
plot = 
text = 
You need to add the plots and pages that need to be generated:
One of the reasons for having multiple PI’s was to have one take over duties from an aging ITX based Linux box of reading from a USB Weather station and uploading the data to both it’s website and WeatherUnderground.
Unfortunately this project got pushed forward when, last Thursday morning, the ITX box decided to die on me. I think it’s power supply finally gave out after 9 years of service.
Whilst doing my daily read through Hansard on the train is morning I came across this written answer in the House of Lords about monitoring Space Weather:
Asked by Lord Hunt of Chesterton
To ask Her Majesty’s Government what plans they have, in collaboration with other European countries and the European Space Agency, to establish a European system for monitoring, forecasting and disseminating information on space weather. [HL839]
The Parliamentary Under-Secretary of State, Department for Business, Innovation and Skills (Baroness Wilcox): The UK Space Agency is working with the European Space Agency (ESA) to develop a space situational awareness programme. An element of this programme covers the development of operational space weather monitoring facilities, including space borne instruments, ground based sensors and telescopes. A further element of the programme addresses the provision and dissemination of space weather forecasts and event warnings by setting up a pilot space weather service centre with expert advisers, a support helpdesk, an archive and a web portal.
This is an optional ESA programme and UK participation must be subject to review against other calls on the space agency budget. A draft business case supporting participation has been prepared by agency staff in consultation with the UK space weather community and interested stakeholders.
One of the tasks I want to use a Raspberry PI for is to take over the duties of an existing ITX based linux box running my weather station. Now in theory that should be pretty simple as the current setup uses pywws to connect to the station and as that’s written in python it should work.
Now the Raspberry PI has no onboard Real time clock – which means it needs to use an NTP server to get the time when it starts. Usually you would use the default settings and allow the PI to connect to thenet for it’s time. Now this is fine if you have a working net connection but what if you are not connected to the net? You might be in the field running the PI on batteries.
As the other projects I have lined up for it is to connect my Meade LX200GPS telescope to the local network or to work with my (in prototype) radio telescopes so having an accurate clock is going to be required.
Now the obvious solution here is to use GPS as a time source. GPS works by having a constellation of satellites in orbit and each one carries a highly accurate atomic clock & broadcast both their current position and the time. A GPS receiver then receives these signals and, as long as it has enough satellites and workout where you are by comparing the times from those clocks.
So this article shows how to use A GPS receiver with the Rasperry PI – although these instructions are not specific to the PI.
For this experiment I’m using a USB GPS receiver from Maplin – product code A73KF. I bought this several months ago when they had it on special offer for £19.99 – it usually retails for £29.99.
Now it comes with a CD for Windows machines but we don’t need it – as the majority of GPS receivers I know of use serial & this is no exception. When plugged in it appears as a serial port.
Plug it in and run lsusb
pi@raspberrypi:~$ sudo lsusb
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
There the Prolific Technology entry is the GPS appearing as a serial port. If you look in /var/log/syslog you will also notice it will have created the port as /dev/ttyUSB0 as it’s the first serial port.
Using the PI as a GPS Receiver
Now the next step is to get the pi receiving data from the satellites. Now there is a suite of tools available for Linux called gpsd which we’ll install:
Ignore any messages from the console or in the log files, you may see it complaining about IPv6 but you can ignore that.
Viewing whats in the sky & your location
Now GPS doesn’t work indoors – as it needs a clear view of the sky so for this I’ve placed the PI on the window sill. Next I ssh into the pi and run cgps.
pi@raspberrypi:~$ cgps -s
The -s flag is there to tell the command not to write raw data to the screen as well as the processed data.
You should then get the following output:
│ Time: 2012-06-18T15:05:10.0Z ││PRN: Elev: Azim: SNR: Used: │
│ Latitude: 51.231848 N ││ 14 43 249 40 Y │
│ Longitude: 0.514014 E ││ 25 75 283 37 Y │
│ Altitude: 132.3 m ││ 2 26 085 31 Y │
│ Speed: 0.0 kph ││ 12 56 070 18 Y │
│ Heading: 0.0 deg (true) ││ 9 19 133 22 Y │
│ Climb: 0.0 m/min ││ 27 09 133 17 Y │
│ Status: 3D FIX (1 secs) ││ 4 17 045 31 Y │
│ GPS Type: ││ 32 05 321 20 Y │
│ Longitude Err: +/- 8 m ││ 29 41 192 18 Y │
│ Latitude Err: +/- 9 m ││ 31 28 304 42 Y │
│ Altitude Err: +/- 27 m ││ │
│ Course Err: n/a ││ │
│ Speed Err: +/- 68 kph ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
Here you can see it’s receiving from 10 satellites and it has the time and your location. The 3D FIX section tells you it has enough data for a 3D fix on your location (i.e. altitude). The Err lines tell you the error in your position. If you leave it running you should see the Err values change every second or so.
Viewing GPS under X-Windows
Now above I showed how the GPS looks from an SSH connection but you can get a graphical display as well using the xgps client thats also been installed. Now if you have a monitor connected to the pi simply open a terminal and run xgps. However as I’ve not got a monitor against the window I’ve used ssh to connect to it from another machine. To get this to work you need to add -Y to the ssh command.
You should now get a window like the following open on your local machine – don’t worry if it takes a little while, it might take a second or two:
Setting the computer time using GPS
Now we have a working GPS we can now get the PI to use it for setting the time. To do this we need to configure ntp to use the GPS satellites as a time source. Now you should already have ntp installed but if not then you need to install it:
pi@raspberrypi:~$ sudo apt-get install ntp
Next you need to edit the file: /etc/ntp.conf and add a few lines to it defining the GPS. This can be either before or after the existing lines beginning with server: