Skip to content

Instantly share code, notes, and snippets.

@ross-nordstrom
Last active August 29, 2015 14:25
Show Gist options
  • Save ross-nordstrom/00bfe9f9f50e784ef2a6 to your computer and use it in GitHub Desktop.
Save ross-nordstrom/00bfe9f9f50e784ef2a6 to your computer and use it in GitHub Desktop.
Trend temperature and humidity. Note: only works on a Raspberry Pi with a DHT22 temp/humidity sensor.
#!/usr/bin/env ruby
require 'json'
NORMAL = 1
DEGRADED = 2
EXCESSIVE = 3
DOWN = 4
def is_number? string
true if Float(string) rescue false
end
dht22reading = `sudo AdafruitDHT.py 22 4`
tempC = dht22reading.split(' ').first.split('=').last.to_f
tempF = (tempC * 1.8000 + 32.00).round(2)
humidity = dht22reading.split(' ').last.split('=').last.to_f.round(2)
# TODO: Drive the numbers off config
tempState = !is_number?(tempF) ? DOWN : (tempF > 78 ? EXCESSIVE : (tempF > 72 ? DEGRADED : NORMAL))
humidityState = !is_number?(humidity) ? DOWN : (humidity > 50 ? EXCESSIVE : (humidity > 40 ? DEGRADED : NORMAL))
if tempC == 0 && humidity == 0
data = {
:availability => 0,
:temperature_state => 3,
:humidity_state => 3,
}
else
data = {
:temperature => tempF,
:humidity => humidity,
:temperature_state => tempState,
:humidity_state => humidityState
}
end
puts data.to_json.gsub(/"/, "'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment