Skip to content

Instantly share code, notes, and snippets.

@jgaxn
Last active August 1, 2017 13:43
Show Gist options
  • Select an option

  • Save jgaxn/0a808191812d4e79b179bffb81c4803c to your computer and use it in GitHub Desktop.

Select an option

Save jgaxn/0a808191812d4e79b179bffb81c4803c to your computer and use it in GitHub Desktop.
A very simple ruby module to read from a DS18B20 thermometer on a Raspberry Pi
module Thermometer
def self.read
serial = ::THERMOMETER_SERIAL
file = File.open("/sys/bus/w1/devices/#{serial}/w1_slave")
lines = file.read.split("\n")
raise ThermometerReadError unless lines[0].end_with? "YES"
/t=(?<reading>\d+)/ =~ lines[1]
celsius = reading.to_f / 1000
celsius * 1.8 + 32
end
end
class ThermometerReadError < StandardError
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment