Class: PiSensor::SHT15

Inherits:
Object
  • Object
show all
Defined in:
lib/pi_sensor/sht15.rb

Constant Summary collapse

HIGH =
1
LOW =
0
COMMANDS =
{ :temperature  => 0x03,
:humidity     => 0x05,
:read_status  => 0x07,
:write_status => 0x06,
:reset        => 0x1E }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pins) ⇒ SHT15

Returns a new instance of SHT15.

Raises:

  • (StandardError)


16
17
18
19
20
# File 'lib/pi_sensor/sht15.rb', line 16

def initialize(pins)
  @clock = pins[:clock]
  @data = pins[:data]
  raise StandardError, "You must assign both data and clock pins. Ex: `sensor = SHT15.new :clock => 0, :data => 1`" unless @clock and @data
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



14
15
16
# File 'lib/pi_sensor/sht15.rb', line 14

def clock
  @clock
end

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/pi_sensor/sht15.rb', line 14

def data
  @data
end

Instance Method Details

#celcius_to_fahrenheit(temp) ⇒ Object



59
60
61
# File 'lib/pi_sensor/sht15.rb', line 59

def celcius_to_fahrenheit(temp)
  return temp * 9.0 / 5.0 + 32.0
end

#fahrenheit_to_celcius(temp) ⇒ Object



64
65
66
# File 'lib/pi_sensor/sht15.rb', line 64

def fahrenheit_to_celcius(temp)
  return (temp - 32.0) * 5.0 / 9.0
end

#humidityObject

Returns the current relative humidity (from 0.0 to 100.0)



47
48
49
50
51
52
53
54
55
56
# File 'lib/pi_sensor/sht15.rb', line 47

def humidity
  send_command(COMMANDS[:humidity])
  humid = read_result

  if humid.nil?
    return nil
  else
    return -2.0468 + (0.0367 * humid) + (-1.5955E-6 * humid)
  end
end

#temperature(scale = :c) ⇒ Object

Reads the current temperature from the sensor, defaulting to celcius. Pass in an :f to return fahrenheit instead:

sensor = SHT15.new :clock => 0, :data => 1
sensor.temperature(:f)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pi_sensor/sht15.rb', line 29

def temperature(scale=:c)
  send_command(COMMANDS[:temperature])
  temp = read_result

  if temp.nil?
    return nil
  else
    converted_temp = -39.65 + (0.01 * temp)
    if scale == :f
      return celcius_to_fahrenheit(converted_temp)
    else
      return converted_temp
    end
  end
end