Controlling a fan with live weather updates

Spring has arrived, this means rooms get uncomfortably hot. A fan helps, but manually turning a fan on and off doesn’t work when I’m asleep or out. This results in waking up or arriving home to a sweltering room. I’ve written a little ruby script that puts Controlling the mains via the parallel port with data from a weather station in Southampton that publishes the weather live. They provide a ‘feels like’ temperature which is adjusted for wind and humidity.

The default threshold temperature is defined at the top, along with the location of the parallel control binary and which data bit to use. Once started, the script checks the temperature every 5 minutes and controls the fan accordingly. It also gives an option to change the threshold temperature, if this is done then the temperature is checked immediately and the fan state updated.

In addition to being left open to allow further temperature adjustments, it can be paused with Ctrl+Z then restarted in the background with bg. You will get your terminal back; if you want to call the auto_fan script back to the foreground, use fg. Alternatively, once the process is running in the background you can use disown; you will no longer be able to call it back to the foreground but the process will not be stopped by closing the terminal or logging off.

auto_fan.rb:

require 'hpricot'
require 'open-uri'

#Get this from http://www.mike-worth.com/2012/02/24/controlling-the-mains-via-the-parallel-port/
PARALLEL_CONTROL='/home/mike/parallel_control/parallel_control'
ADDRESS_BIT=1
#Set up a default temperature
threshold_temperature=18

#This is where the clever stuff happens, scrape the current temperature adjusted for wind
# and humidity from a weather station in Southampton and adjust the fan state accordingly
def check_temperature(threshold)
  doc = Hpricot(open("http://www.southamptonweather.co.uk/"))
  temperature=doc.search("span[@id=ajaxfeelslike]")[0].innerHTML.to_i
  if (temperature<threshold)
    system(PARALLEL_CONTROL+' '+ADDRESS_BIT.to_s+' 0 > /dev/null')
  else
    system(PARALLEL_CONTROL+' '+ADDRESS_BIT.to_s+' 1 > /dev/null')
  end
end

#Start a separate thread to check the temperature and adjust the fan state every 5 minutes
Thread.new{
  while true
    check_temperature(threshold_temperature)
    sleep 300
  end
}

#Give a basic UI that allows the threshold temperature to be adjusted
puts 'threshold temperature is currently set to '+threshold_temperature.to_s+'°C'
while true
  puts 'Set threshold temperature'
  threshold_temperature = gets.chomp.to_i
  #We don't want to have to wait 5 minutes for the fan state to update
  check_temperature(threshold_temperature)
end

One thought on “Controlling a fan with live weather updates

  1. agneau Belanyek

    🙂

    This is up there with the toothpaste mixer which tells you the temperature forecast for the day when you brush your teeth – colder more mint, hotter more cinnamon.

    Reply

Leave a Reply