Quantcast
Channel: Moorescloud Dev Blog
Viewing all articles
Browse latest Browse all 34

Demo: display the UV Index

$
0
0

Here’s a little something that was adapted from a URL in a comment thread posted to this blog.  The real-time UV indices for various points in Australia are posted by the Australian Radiation Protection and Nuclear Safety Agency.  It’s a fairly simple matter to extract that information from an XML file, and set the Light an appropriate color:

#!/usr/bin/python
#
import xml.etree.ElementTree as ET
import sys, urllib2

f = urllib2.urlopen('http://www.arpansa.gov.au/uvindex/realtime/xml/uvvalues.xml')
d = f.read()

root = ET.fromstring(d)

uvalue = 0.0
for child in root:
	if (child[0].text == 'syd'):  # For Sydney
		uvalue = float(child[1].text)

colorval = 0x808080

# Map the value based on the chart given at http://en.wikipedia.org/wiki/Ultraviolet_index
# Which is, you know, easier than visualizing it.
if (uvalue < 2.9): 	
   colorval = 0xFF8080 
if ((uvalue > 2.9) and (uvalue < 6.0)): 	
   colorval = 0xFFFF80 
if ((uvalue > 5.9) and (uvalue < 8.0)): 	
   colorval = 0xBFFF80
if ((uvalue > 7.9) and (uvalue < 11.0)): 	
   colorval = 0x80FF80 
if (uvalue > 10.9):
	colorval = 0x80FFFF

colorstr = "http://localhost/cgi-bin/ajaxcolor?color=0x%6x" % colorval
print colorstr
e = urllib2.urlopen(colorstr)

There’s a lot more that could be done here to make this easy to use, handle all the various cities, etc. But it’s a good basic example of how to use external data sources to drive the Light.


Viewing all articles
Browse latest Browse all 34

Trending Articles