Maxwell Terry

Arduino Thermometer

We previously looked at an Arduino class for Python. Now let's use it to create an Arduino thermometer.

#!/usr/bin/env python

# temperature
# Maxwell Terry
# MIT license

from xml.etree.ElementTree import *
from urllib2 import *

def temperature(url, scale):
  return int(parse(urlopen(url)).findtext('temp_'+scale))

def leds(temp):
  if fahr >= 120:  return 5
  elif fahr >= 90: return 4
  elif fahr >= 60: return 3
  elif fahr >= 32: return 2
  elif fahr > 0:   return 1

The temperature function takes in a url and scale (C or F) and returns an integer value. This is dependent on a feed that gives the temperature after "temp_" of course, so isn't a particularly general solution. The leds function takes in a temperature and returns the number of LED lights to turn on. This is arbitrary, but seems appropriate when only 5 LEDs are available.

#!/usr/bin/env python

# Old Town
# Maxwell Terry
# MIT license

import Arduino

connection = Arduino("/dev/tty.usbserial-A5002tRQ")

url = "http://www.weather.gov/data/current_obs/KOLD.xml"
unit = "f"

connection.writeLine(leds(temperature(url, unit))

Here's the Arduino class in action. This is specifically how we'd get the temperature of Old Town, Maine. As Weather.gov lists feeds by location (rather than, say, zipcode), it's hard to automate access. To do so, we'd have to create a table pairing city and location names, or seek out another collection of local feeds.