Class: DS18B20

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

Overview

DS18B20 - high precision temperature sensor with 1-wire interface

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

Constructor Details

#initialize(device = 'w1_slave') ⇒ DS18B20

Returns a new instance of DS18B20.



6
7
8
9
10
11
12
13
14
# File 'lib/ds18b20.rb', line 6

def initialize(device='w1_slave')
  self.name = 'DS18B20'
  @sensor_file = DigitalReceptor.one_wire_file('28*', device)
  if @sensor_file
    @temperature = read_data
  else
    raise "#{@sensor_name} - Sensor NOT FOUND!"
  end
end

Instance Method Details

#read_dataObject



16
17
18
19
20
21
22
# File 'lib/ds18b20.rb', line 16

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