An MQTT/Websocket based Thermometer using the html5 meter tag
05 Sep 2010In this post I will show the latest in a line of mini examples using html5 and websockets - an example of how future home monitoring dashboards should be created. At present they commonly use a combination of flash dials and JavaScript canvasy bits and pieces to create representations of data obtained via ajax. This has the inherent disadvantage that the browser polls the server rather than the server pushing values to the browser when available. Also there is very little in the way of accessibility as far as canvas and flash are concerned. In this post I will show a simple example of using data via a web socket and displays it via the meter.
While nothing demonstrated here is rocket science it’s something that’s not being focused on enough, developers seem to be getting lost in the shinyness of the canvas and forgetting the built in meter functionality along with it’s inbuilt accessibility. Having said all that it is only displayed by the more bleeding edge browsers and things may pick up as the html spec matures.
HTML5 Meter
Of the new features introduced by the HTML5 spec one that particularly sparks my interest is the meter tag, in the words of the spec:
"The meter element represents a scalar measurement within a known range, or a fractional value; for example disk usage, the relevance of a query result, or the fraction of a voting population to have selected a particular candidate."
However in simple terms (at present) it just produces a single bar meter with the length of the bar defined by the value attribute and maximum and minimum values defined thus, high and low values can also be defined which effect the colour of the bar depending on the value. They seem to have omited the possibility of dynamic content from their main definition. For browsers which don’t support the meter tag fall-back content can be inserted before the end tag.
The purpose of explaining this here is, as will most html elements, the attributes can be manipulated with javascript so here I will use a number sent via a web socket to set the value attribute of the meter (and also update the text).
Server Side
On the server I will be using pywebsocket and it’s included web server to subscribe to an MQTT broker topic which contains temperature data. The code is identical to that used in my previous example. The data could however come from any source which provides numerical data.
Client Side
The code used here again is very similar to my previous examples except values are take from the body of the message and then given to the value attribute of the meter element (using jquery). I have added some CSS to make it look a bit nicer too:
Moving On
Hopefully in the future developers both of browsers and web applications will make use of the meter element rather than non standard solutions especially once the html5 spec is finalised and there is (hopefully) going to be proper CSS hooks available to create more beautiful meters.