Back Garden Weather in CouchDB (Part 1)

 class=

When she was younger my wife wanted to be a meteorologist. That didn’t pan out, but our recent move found us with a garden, which we’ve not had before. This gave me the opportunity to buy her a weather station. I didn’t just choose any old station though, I wanted one that did wind and rain as well as the usual temperature, pressure and humidity. And, the deciding factor, a USB interface with Linux support. Fortunately the excellent PyWWS supports a range of weather stations, including the one I brought.

I’m not going to go into how I mounted the system, or configured PyWWS. That’s all covered in the documentation. PyWWS can produce a static website, but as someone who earns his living building websites I wanted something a bit better. Continuing my experiments with CouchDB I decided to build the website as a CouchApp.

As well as allowing you to query your data with Javascript, CouchDB lets you display webpages directly out of your database. If you visit welwynweather.co.uk you’ll notice that you’re redirected to a url that contains url arguments that look a lot like those used to query a view. That’s because that’s exactly what’s going on. Things become clearer when you discover that that http://www.welwynweather.co.uk is an alias for http://db.welwynweather.co.uk/_design/weather/_rewrite/. Now you can see a more complete CouchDB URL, albeit without the database name. db.welwynweather.co.uk points to an Apache reverse proxy that routes requests through to CouchDB.

Over the next few posts I’ll detail how the CouchApp works, but to get started you can clone my app and poke it yourself. Once you’ve installed the couchapp command line client simply run couchapp clone http://db.welwynweather.co.uk/_design/weather. This will give you a directory, weather, that contains a number of subdirectories including templates and views which contain the complete website.n To deploy the site to your own server you need to create a database and then run couchapp push weather http://localhost:5984/welwynweather. Visiting http://localhost:5984/welwynweather/_design/weather/_rewrite/ should show you the site. You’ll need some data though, and you can use CouchDB replication to pull my data to your server. Using Futon simply set http://db.welwynweather.co.uk/ as the replication source and your database as the destination and you’ll quickly get a complete copy of the database.

When replicating my data you currently cannot use continuous replication. When it completes replication CouchDB calls POST /_ensure_full_commit, but obviously I’ve disabled POST, PUT and DELETE on my server. This causes replication to fail and to restart from the beginning. The data will already have been copied, but CouchDB will copy it again. If you have any ideas on how to avoid this, please answer my StackOverflow question.

The website consists of four main pages. When you visit you are redirected to a page that shows the weather for the current day. Clicking on the date at the top of the page lets you also view the weather by month and by year. The daily weather pages show as much detail as is recorded by the station, in my case this is an update every five minutes. The monthly page is much the same except that the values are averaged across an hour. The yearly page is a bit different as it shows a single point for each day. An average temperature for each day is not that useful so we calculate the high and low for each day and display that.

The final page is the records page. This displays information like the highest and lowest temperature ever recorded and the heaviest rain by hour and by day. The previous three pages are all fully generated by the server. The records page is a bit different though as calculating the records in one step is a bit complicated, instead we use AJAX to load each record individually. This means we can focus on each record keeping the code simple.

In the next post I’ll discuss how I import data into CouchDB and the basics of rendering a page in a CouchApp.


If you visit the site you may find that there is no recent weather data. This is because I run PyWWS on my MythTV box. Rather than running the PC all the time the weather data only updates when a programme is being recorded, or I’m watching TV.

Want to read more like this? Follow me with your favourite feed reader (e.g. Feedly), or subscribe to my SubStack newsletter.

Comments

[...] Tagged with couchapp, couchdb, javascript, mustache, weather « Back Garden Weather in CouchDB (Part 1) [...]

Back Garden Weather in CouchDB (Part 2) « Andrew Wilkinson

05 Jan 2012

[...] CouchApp to display the weather data collected by a weather station in my back garden. In the first post I described CouchApps and how to get a copy of the site. In the next post we looked at how to [...]

Back Garden Weather in CouchDB (Part 3) « Andrew Wilkinson

12 Jan 2012

Hello, thanks, useful site - I live in hertford and have found about 10! weather stations around within 10 miles or so, eg in Stevenage, Epping, Harlow, Sawbrideworth, Puckerdege and of course Welwyn, tho' none in Hertford or Hoddesdon yet.

Pity yours is not on all the time. Have you thought about hosting it on a 'free' web site?

If one googles welwyn weather station there seems to be another site there, hosted by virgin. Unfortunately there is no contact info eg email for it so I can't let them know if it goes awol. Given fact that Welwyn is small place, must be most weather stations/sq km anywhere, outside big cities!

John

john lee

08 Jun 2012

Hi John,

Glad you like the site. You're right, there is a lot of interest in weather sites in the Welwyn area!

Unfortunately my site is available all the time, it's just that I don't want to have a computer on 24/7 to talk to the weather station. At the moment it only updates when I'm recording tv. This is usual around 2pm and most evenings after 7pm.

I use the site to check how much ran we've had and how cold it was last night so I don't need it to be up to date all the time.

Andrew

Andrew Wilkinson

08 Jun 2012

[...] you can retrieve it and put it in a webpage. I started out trying to follow the excellent writeup about the welwynweather site, but there were just enough rewrites and mustache and dynamic urls [...]

Streaming data, part 2 « Blogojeff

07 Dec 2012

Hi,
today I tried to clone your couchapp according your description, but I always get an "unauthorized access" error.

Is there anything I'm doing wrong or have you changed the permissions for the design document?

Jochen

Jochen

02 Feb 2013

Hi,

Unfortunately my blog post is out of date and I have rewritten the site using Django and MySQL. CouchDB is no longer used.

Cheers,
Andrew

Andrew Wilkinson

02 Feb 2013