Class: IoT::DHTxxSensor

Inherits:
HumitureSensor show all
Defined in:
lib/iot/dht_sensor.rb

Overview

DHT11/22 humidity & temperature sensor family

Direct Known Subclasses

DHT11, DHT22

Instance Method Summary collapse

Methods inherited from HumitureSensor

#humidity, #to_s

Methods inherited from TemperatureSensor

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

Methods inherited from DigitalReceptor

one_wire_file

Methods inherited from Receptor

#model_name, #read

Constructor Details

#initialize(pin, sensor_model) ⇒ DHTxxSensor

Returns a new instance of DHTxxSensor.



8
9
10
11
12
13
# File 'lib/iot/dht_sensor.rb', line 8

def initialize(pin, sensor_model)
  @pin = pin
  @sensor_name = 'DHT'+sensor_model.to_s
  @sensor_model = sensor_model.to_i
  @temperature, @humidity = read_data
end

Instance Method Details

#nameObject



15
16
17
# File 'lib/iot/dht_sensor.rb', line 15

def name
  @sensor_name
end

#read_dataObject

Read values by ‘DhtSensor.read’ from ‘dht-sensor-ffi’



20
21
22
23
24
25
# File 'lib/iot/dht_sensor.rb', line 20

def read_data
  sensor_data = DhtSensor.read(@pin, @sensor_model)
  @temperature = sensor_data.temp
  @humidity = sensor_data.humidity
  [@temperature, @humidity]
end