Class: DHT11::Sensor
- Inherits:
-
Object
- Object
- DHT11::Sensor
- Defined in:
- lib/dht11.rb
Instance Method Summary collapse
-
#initialize(pin, tries: 10, interval: 0.1) ⇒ Sensor
constructor
A new instance of Sensor.
- #read ⇒ Object
- #read_once ⇒ Object
Constructor Details
#initialize(pin, tries: 10, interval: 0.1) ⇒ Sensor
Returns a new instance of Sensor.
21 22 23 24 25 26 |
# File 'lib/dht11.rb', line 21 def initialize(pin, tries: 10, interval: 0.1) @pin = pin @tries = tries @interval = interval RPi::GPIO.set_numbering(:bcm) end |
Instance Method Details
#read ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/dht11.rb', line 28 def read @tries.times do @last_result = read_once return @last_result if @last_result.valid? sleep @interval end @last_result end |
#read_once ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dht11.rb', line 37 def read_once send_initial_signal raw_inputs = collect_raw_input periods = parse_inputs_into_pull_up_periods(raw_inputs) if periods.length != 40 return Result.new(Err::MISSING_DATA, Float::NAN, Float::NAN) end bits = calculate_bits(periods) bytes = bits_to_bytes(bits) checksum = calculate_checksum(bytes) if bytes[4] != checksum return Result.new(Err::CRC, Float::NAN, Float::NAN) end temperature = bytes_to_temperature(bytes).to_f humidity = bytes_to_humidity(bytes).to_f Result.new(Err::NO_ERROR, temperature, humidity) end |