Class: IoT::DS18B20
- Inherits:
-
TemperatureSensor
- Object
- Receptor
- DigitalReceptor
- DigitalSensor
- TemperatureSensor
- IoT::DS18B20
- Defined in:
- lib/iot/ds18b20.rb
Instance Method Summary collapse
-
#initialize(device = 'w1_slave') ⇒ DS18B20
constructor
A new instance of DS18B20.
- #read_data ⇒ Object
Methods inherited from TemperatureSensor
#celsius, #fahrenheit, #kelvin, #reaumur, #temperature, #to_s
Methods inherited from DigitalReceptor
Methods inherited from Receptor
Constructor Details
#initialize(device = 'w1_slave') ⇒ DS18B20
Returns a new instance of DS18B20.
7 8 9 10 11 12 13 14 15 |
# File 'lib/iot/ds18b20.rb', line 7 def initialize(device='w1_slave') self.name = 'DS18B20' @sensor_file = DigitalReceptor.one_wire_file('28*', device) if @sensor_file @temperature = read_data else raise "#{self.name} - Sensor NOT FOUND!" end end |
Instance Method Details
#read_data ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/iot/ds18b20.rb', line 17 def read_data File.open(@sensor_file, 'r') do |f| value = f.read @temperature = value.scan(/t=(\d+)/).flatten[0].to_i/1000.0 end @temperature end |