Last active
August 1, 2017 13:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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