Class: IoT::DS18B20

Inherits:
TemperatureSensor show all
Defined in:
lib/iot/ds18b20.rb

Instance Method Summary collapse

Methods inherited from TemperatureSensor

#celsius, #fahrenheit, #kelvin, #reaumur, #temperature, #to_s

Methods inherited from DigitalReceptor

one_wire_file

Methods inherited from Receptor

#model_name, #name, #read

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_dataObject



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