Andrew Wilkinson

Random Ramblings on Programming

Back Garden Weather in CouchDB (Part 1)

with 7 comments

RainWhen 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.

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.


Photo of Rain by Moyan Brenn.

Advertisements

Written by Andrew Wilkinson

December 2, 2011 at 12:00 pm

Posted in couchdb

Tagged with , ,

7 Responses

Subscribe to comments with RSS.

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

  2. […] 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 […]

  3. 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

    June 8, 2012 at 11:49 am

    • 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

      June 8, 2012 at 12:56 pm

  4. […] 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 […]

  5. 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

    February 2, 2013 at 4:55 pm

    • 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

      February 2, 2013 at 6:04 pm


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

%d bloggers like this: