Archive for the ‘Code Projects’ category

Meteoalarm Weather Warnings PHP WebService

January 1, 2018

Accurate, localised weather warnings are a useful addition to any weather website. Historically I have used Wunderground’s Alerts API for this purpose on my own weather website. However, at some point last year Wunderground’s Alerts API stopped returning any weather warnings for the UK even during major weather events. At the time of writing this is still the case.

I noted that Wunderground were receiving their European weather warning data from Meteoalarm. Looking at their website I found that weather warnings from the UK were being reported accurately matching exactly what the UK MetOffice (the source of Meteoalarm’s UK data) were publishing on any given day.

To my disappointment neither Meteoalarm nor the MetOffice published a webservice API for weather warnings. Meteoalarm do, however, provide RSS feeds of weather warnings for each country and region in Europe that they support.

I decided to use Meteoalarm’s RSS feeds to solve my own issue with publishing weather warnings to my website for my local area. However, I knew that directly publishing raw RSS feeds would not be not flexible enough for my needs. I would instead prefer a RESTful JSON formatted web service API which I could call via AJAX.

To this end I’ve written meteo-alarm-weather-warnings. This is a PHP API available on GitHub under the MIT License. It’s primary use case is to make it easy to incorporate the latest MeteoAlarm weather warnings into a web site while not requiring users to directly engage with the underlying RSS feeds. The web service fetches the appropriate RSS feed for a given request and parses and converts it into a consistent JSON formatted reply.

It’s functionality includes:

  1. The ability to query for all weather warnings for all regions within any European country covered by Meteoalarm for today and tomorrow.
  2. The ability to query for weather warnings for any individual region for today and tomorrow.
  3. Custom date/time formats to use for results.
  4. Custom time zone to use for date/times in results (Meteoalarm reports all date/times in CET).

For example, here is a portion of the the raw JSON results from meteo-alarm-weather-warnings for the UK on 31/Dec/2017 when several warnings were in effect:

meteo-alarm-weather-warnings

Click to Enlarge

meteo-alarm-weather-warnings is now incorporated into my own weather web site’s weather warnings page. Here is how it looked rendering results from the web service for my local area on 31/Dec/2017:

meteo-alarm-weather-warnings on website

Click to Enlarge

 

 

Met Office Weather Maps PHP API

October 17, 2015

For those who want to add detailed weather maps of the UK to their websites an excellent option is to use the Met Office’s DataPoint web service API. Among many other features the API makes available several different types of observation and forecast time series weather maps as detailed images.

However, DataPoint still leaves users with a lot of work to do if they want to utilise the maps on their websites. For example the DataPoint API only provides the map images as simple layers with no underlying map of the UK. This gives users the flexibility of adding their own UK maps but creates one of many common problems that all DataPoint users have to solve individually.

To address this and other website integration issues I’ve made the met-office-weather-maps PHP API available on GitHub under the MIT License. It’s primary use case is to make it easier to incorporate the latest Met Office weather maps into a web site while not requiring users to directly engage with the Datapoint API.

It’s functionality includes:

  1. Wrapping the complexity of underlying JSON web service calls by exposing one simple PHP class for each available map type.
  2. Adding tailored UK map layers to fetched images that obey the required Mercator projection boundary box.
  3. Adding UK timestamps to map images where they are not already present.
  4. Operating caching to allow maps to be requested on a regular schedule. This ensures that the latest maps are fetched without making excessive calls to DataPoint which may breach their fair use agreement.

The met-office-weather-maps API supports all of the DataPoint map types:

  • Infrared satellite
  • Lightning strikes
  • Precipitation forecast
  • Pressure forecast
  • Rainfall radar
  • Surface pressure charts
  • Temperature forecast
  • Total cloud cover forecast
  • Total cloud cover and precipitation forecast
  • Visible satellite

See the met-office-weather-maps Git Hub project page for details of its requirements, installation and API documentation.

I originally wrote met-office-weather-maps for incorporation into my own weather website where I regularly fetch a selection of weather maps. I previously used Weather Underground’s map API but I find DataPoint to be far superior in terms of map selection and the high frequency of map update.

See below for some real examples that demonstrate met-office-weather-map’s capabilities.

Example 1: Surface Pressure Forecast Map

The Surface Pressure Forecast image available from DataPoint is a transparent layer depicting isobars and fronts:

Click to Enlarge

The same image after processing by met-office-weather-maps. Note the addition of a timestamp and a colour base image of the UK:

Click to Enlarge

Example 2: Visible Satellite Map

The Visible Satellite image available from DataPoint is an opaque layer depicting cloud cover:

Click to Enlarge

The same image after processing by met-office-weather-maps. Note the addition of a yellow outline overlay image of the UK which does not obscure the existing image layer’s timestamp and key:

Click to Enlarge

Example 3: Temperature Forecast Map

The Temperature Forecast image available from DataPoint is a partially transparent layer depicting colour-coded screen temperature:

Click to Enlarge

The same image after processing by met-office-weather-maps. Note the addition of a timestamp, a colour base image and a black outline overlay image of the UK:

Click to Enlarge

Example 4: Cloud Cover and Precipitation Forecast Map

The Cloud Cover and Precipitation Forecast image available from DataPoint is a transparent layer depicting cloud cover and colour-coded precipitation rates:

Click to Enlarge

The same image after processing by met-office-weather-maps. Note the addition of a timestamp, a colour base image and a black outline overlay image of the UK:

Click to Enlarge

“F Weather” Pebble Watch Face

March 21, 2015

Hot on the heels of my Cirrus Pebble WatchApp I’ve just completed another creation for the Pebble: “F Weather”. F Weather is my homage to one of my favourite web sites: THE FUCKING WEATHER. THE FUCKING WEATHER tells the weather like it is and so does F Weather except it does so for your current location:

Click to Enlarge

Click to Enlarge

There are a few differences between F Weather and my Cirrus WatchApp. First of all this is a watch face rather than a watch app so it’s not interactive (but then again it doesn’t need to be interactive to relate the current temperature through the medium of swearing). Secondly F Weather is written in C and PebbleKit JS. This makes it much faster than the PebbleJS based Cirrus WatchApp. I haven’t written any C for 13 years or so and thoroughly enjoyed getting back into it.

Current conditions are looked up from Yahoo’s RESTful query services which involves two calls.

The first call takes the current longitude/latitude position available to the Pebble and looks up the corresponding WOEID and city name:

https://query.yahooapis.com/v1/public/yql?q=select city, woeid from geo.placefinder where text=”40.87,-74.16″ and gflags=”R”&format=json

The second call uses the WOEID to look up the current temperature:

https://query.yahooapis.com/v1/public/yql?q=select item.condition.temp from weather.forecast where woeid=12760611 and u=”c”&format=json

This process is repeated every 15 minutes.

My code is open source and hosted on github. It can be found here: pebblec-watchface-fweather. The install PBW can be found under Releases.

Cirrus Pebble WatchApp for Weather Display Live

January 17, 2015

I don’t mind admitting that I am a bit obsessed about the weather. I am especially fastidious in keeping up with the current conditions where I live. This has manifested itself in several of my recent coding projects that display data from my weather station including my own web site, a web service, a PiFaceCAD based console and an android app widget.

I bought a Pebble smart watch last year on a whim and have found it useful for its notifications of texts, emails and phone calls (I keep my phone on silent so the Pebble’s vibration based notifications are especially handy). Given I have access to my weather station’s data on all of my other devices doing the same with the Pebble was a logical choice for a new project.

Previous weather projects I have written have directly fetched and processed Weather Display Live’s clientraw.txt file. For my Pebble watch application I wanted to make more use of my own web service. This would simplify the coding process as the service, unlike clientraw.txt, exposes weather data in a well-defined structure and many different measurement units. In addition PebbleJS toolkit has built-in support for making http calls to JSON-based web services.

I coded the application in Javascript using the CloudPebble website. For a browser based IDE CloudPebble is very feature rich with syntax highlighting, github integration and logging. It also has the ability to deploy directly to a pebble watch provided you have a paired smart phone for the watch on the same wireless network as the computer running CloudPebble.

The only other code that was required was a web page to expose configuration settings for the application. This currently has to be hosted on the web. In my application’s case this page allows the configuration of the web service URL and a choice of metric or imperial weather measurement units.

The finished application makes use of the Pebble’s menu UI. It populates a top-level menu of current weather data including temperature, surface pressure, humidity, rainfall and wind. Selecting any of these top-level menu items displays a sub-menu of more in-depth information for the particular measurement including daily high and low values.

As usual my code is open source and hosted on github. It can be found here: pebblejs-watchapp-cirrus.

Click to Enlarge

Click to Enlarge

Cirrus Android Widget for Weather Display Live

December 13, 2014

One of my recent projects was to develop an Android widget to display the values found in online WD Live clientraw.txt files. It accesses the same data as my other weather related code projects this year: a JSON Web Service for Weather Display Live and a PiFaceCAD Weather Display Console.

I’ve name the Widget “Cirrus” after the cloud genus. The widget supports 18 different weather data points (and displays the current trend for many of these) and 16 different measurement units. It also has colour coding for easy recognition of different temperature ranges (using the BBC Weather temperature colours) and UV index (using the US EPA UV Index colours).

Click to Enlarge

Click to Enlarge

I use the widget to display the current conditions for both my own weather station and others when I travel. So far I have installed and run it successfully on a Samsung Note II (Android 4.4.2), Samsung Galaxy Express (Android 4.1.2) and a 2013 Nexus 7 (Android 5.0.1).

At this time I do not have any plans to publish the widget on the Google Play Store. I have, however, open sourced the code using the MIT License and made it available via GitHub here:

https://github.com/waynedgrant/android-appwidget-cirrus

See the read me for installation and use instructions.

 

JSON Web Service for Weather Display Live

October 26, 2014

I’ve been busy implementing a new website for my weather station over the last few weeks at Wayne’s Weather.

There are a number of new technologies being employed. I’ve ditched my bespoke (and ugly) HTML and instead use Bootstrap for a client responsive UI. Chart and image viewing is now more user-friendly thanks to Lightbox. I have also made extensive use of JQuery to pull in information from various sources and to provide a more dynamic experience.

The functionality of the site has also been extended. There are now more in-depth current readings supporting both metric and imperial measurements, forecasts and weather maps from Wunderground.com for the local area have been integrated and I have added a weather webcam and a weather almanac. I intend to write posts to cover the wunderground and webcam build outs soon.

For now this post will concentrate on an unexpected off-shoot of my efforts: a Web Service that exposes Weather Display Live data.

Weather Display Live

Weather Display (and its equivalents such as MeteoHub) can generate clientraw.txt files containing a weather station’s readings and can upload them to a web server periodically and frequently via FTP (in some cases every minute). The primary purpose of this is to drive a Weather Display Live (WDL) dashboard:

Click to Enlarge

The WDL clientraw.txt format is well documented and I have previously used the HTTP addressable clientraw.txt to drive a custom Android widget and my PiFaceCAD Weather Display Console. The disappointing aspect is WDL itself. Don’t get me wrong it’s an excellent weather dashboard and I still have it available in my new website. The problem with it is that is Flash based which is not as widely supported as it used to be. It’s pretty much not an option for mobile device clients running IOS or Android, for example.

To counter this I wanted my new website to make far greater use of the information contained in the clientraw.txt files outside of WDL dashboard itself. My old weather website made use of PHP to grab weather readings from clientraw.txt and place them into web pages before serving them up to the client. This was an approach I wanted to move away from.

I’d just cut my teeth on using JSON formatted web services with my experience as a client to the Wunderground API. I liked the idea of having a page load its own data from the client side where it could conceivably be mix and match data from various services. I figured it wouldn’t be too difficult to create my own web services to expose WDL data and call them from my own web site.

json-webservice-wdlive

I settled on using PHP for the implementation for a couple of reasons. Firstly because I’ve used it for server-side processing on-and-off for a few years and know if fairly well. Secondly because I intended to write a general solution which I could open source for others to use and PHP is ubiquitous in the hosting world.

The result was json-webservice-wdlive, a JSON formatted web service API.

The API exposes two URLs. The first URL returns current weather conditions including Temperature, Pressure, Rainfall, Wind, Humidity, Dew Point, Wind Chill, Humidex, Heat Index and UV. The second URL returns a weather almanac for Month-to-Date, Year-to-Date and All Time records. Both JSON and JSONP (enabled with the addition of a callback attribute to the URL) are supported.

Besides exposing the data in the default units found in clientraw the responses also contain many alternative units. For example, clientraw files store wind speeds in knots. The Web Service responses, on the other hand, respond with Bft, knots, km/h, mph, and m/s.

json-webservice-wdlive-output

Click to Enlarge

Note that I only expose the data I need for my own purposes (i.e. what my weather station setup supports). However, it would be a simply matter to expand the service calls to add, say, solar measurements or extend the selection of almanac measurements. Anything else WDL clientraw.txt files provide can be exposed, if required, with minor code additions.

I have made the json-webservice-wdlive source code available as a GitHub project. Have a look in the project’s README for installation instructions. json-webservice-wdlive is also running live on my own weather website. You can try it at by clicking on the links below:

Refer to the GitHub project page for more details on the Web Service’s response fields including the different measurement units and number field formatting details.

I also make use of the service in three places on my own website: the Current Conditions, Weather Almanac (pictured below) and the Forecast page where I mix my service’s results with those from the Wunderground web service.

almanac

Click to Enlarge

If you have a WDL enabled website feel free to install json-webservice-wdlive to expose your weather data to others and/or to include your information in your own pages.

PiFaceCAD Weather Display Console Build Out

July 19, 2014

I bought a Raspberry Pi back when they were first released in February 2012. It was purchased with the intention of setting it up as data logger for my WMR88 weather station. I got it up and running for that purpose using wview. However, I soon found a better solution for my data logging requirements in the Meteo Sheeva.

The Pi then found its way into a box and remained there unpowered for over two years. I wanted to put it to good use but no ideas were forthcoming to utilise it. Then inspiration struck.

I had just completed an Android application widget that could poll the WD Live files hosted on a website and display the current weather conditions therein. I use it on my smart phone to see the current conditions at home. Could I use the same principle to create a custom weather display console? It seemed like a cool little project and would put the Pi to good use at last.

The first thing I needed was some kind of simple, dedicated display for the Pi. Trawling the web I found a few products on offer that did just that. Unfortunately they all required soldering which I’m just not brave enough to try. Then I discovered the PiFace Control and Display (PiFaceCAD).

The PiFaceCAD is a neat bit of kit. It attaches to a Pi via its GPIO ports and provides a small backlight 16×2 character screen, five buttons, a three position navigation switch and an IR receiver. All of these features are programmable via Python. I ordered it and the compatible Camden Boss case from MCM and they arrived a few days later.

Assembly

The assembly was a little fiddly as the Camden Boss case is a very tight fit around the Pi and PiFaceCAD. I’ve illustrated the process in the pictures below.

The unboxed PiFaceCAD:

20140421_192221

Click to Enlarge

The unboxed case with the included rubber feet. I prefer clear cases if I can get them as they are more interesting to look at than opaque cases:

20140421_191636

Click to Enlarge

My Pi ( a Model B Rev 1) maneuvered into the bottom half of the case. It’s a tight fit to get it in place, especially the composite video port:

20140421_192746

Click to Enlarge

The PiFaceCAD attached to the Pi via its GPIO pins. Getting the PiFaceCAD into the bottom half of the case is difficult as the five buttons have to fit into small cut outs. I was sure I was going to break something but fortunately I didn’t:

20140421_192916

Click to Enlarge

The top of the case snapped into place. Again it’s tricky to fit it around the Pi’s USB and Ethernet ports:

20140421_193119

Click to Enlarge

Finally the assembled unit powered up, with the PiFaceCAD software installed and running the supplied SysInfo demo program:

20140421_194321

Click to Enlarge

Coding in Python

With the hardware up and running the next step was to code the weather display application itself. The PiFaceCAD provides a functional, easy to use Python API. The API exposes the ability to write to the display, control its backlight and add event handlers to the various buttons and the IR receiver.

I had never coded in Python before (Java has been my mainstay for last few years with the occasional foray into PHP and C#) but found it to be a straightforward language to pick up with a gentle learning curve and excellent documentation. The language’s dynamic typing took a little getting used to given my background but I started the exercise prepared to learn new ways of doing things (I did not want to “code Java in Python”). Coming from Java I really appreciated the lack of boilerplate code required by Python and just how little code it took to get things done without sacrificing readability. I expect to be doing a lot more coding in Python in the future.

Python’s library support is also excellent. I was pleased to find a JUnit like capability in the “unittest” Module and made use of decimal, urllib, threading and time modules in my solution (as well as creating a few modules of my own to handle, transform and format weather data).

What I did not do was code on the Pi itself. I instead coded the project on my Macbook using the excellent PyCharm Community Edition IDE from JetBrains. I periodically uploaded my unit tested solution to my Pi via FTP for integration testing. The code targets Python 3.2.3 which was the version available on my current version of Raspbian (released Jan 9th 2014).

pifacecad-wdlive on GitHub

The result of my endeavours was the pifacecad-wdlive application which I have hosted on GitHub. If you are interested in installing pifacecad-wdlive then head to the README for setup instructions. If you want to fork pifacecad-wdlive to add extra capabilities then all of the source code and unit tests are available in the GitHub project.

The current version of pifacecad-wdlive supports 16 different weather displays which are selectable using the PiFaceCAD’s navigation switch (this switch also controls the backlight) and 16 different measurement units toggled using the PiFaceCAD’s buttons. Better yet all of these functions can be controlled via a normal IR remote control (I have mine working via my XBox 360 remote). The application can be configured on the command-line to point at any WD Live enabled website and polls for updated weather conditions every 60 seconds.

Finishing Touches

If my Pi ever lost power I did not want to have to start pifacecad-wdlive manually every time the Pi restarted. To execute pifacecad-wdlive on startup I added the following line to the Pi’s /etc/rc.local file:

su - pi -c "python3 /home/pi/pifacecad-wdlive/pifacecad-wdlive.py
http://www.waynedgrant.com/weather/clientraw.txt" &

Those wanting to do the same will want to adapt these lines to match their particular pifacecad-wdlive installation and clientraw.txt URL.

A normal weather display unit such as the WMR88 or WMR200 can display weather anywhere it can receive mains power and wireless sensor data. My PiFaceCAD, on the other hand, was shackled to an Ethernet cable. I decided to make my setup a little more portable by adding a wireless adapter to the Pi. I picked up an Edimax Wifi USB from Amazon to accomplish this.

A wasted hour of following various (conflicting) manual instructions on the web ensued as I tried and failed to set the wireless adapter up on the Pi. I then stumbled on and installed wicd-curses and promptly got the Wifi working.

My one concern in using a Wifi USB adapter was that of power. I feared that having the Wifi USB adapter and the PiFaceCAD connected to the Pi at the same time may require me to use a powered USB hub. Fortunately my 0.7A Samsung phone charger was up to the job so a powered hub was not required.

Here is the finished build out complete with Wifi adapter and displaying pifacecad-wdlive’s summary weather display screen for my own weather station:

Click to Enlarge

Click to Enlarge

I’m quite pleased with how the build out has turned out. While it would probably have cost around the same money to buy a new weather display console from Oregon creating this was a lot more fun and satisfying.