Class: RPi::Dht::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rpi/dht/base.rb

Direct Known Subclasses

Dht11, Dht22

Constant Summary collapse

CLEAR_SIGNALS =
500 / 1000.to_f
START_SIGNAL =

DHT11 Microprocessor I / O set to output at the same time output low, and low hold time can not be less than 18ms, then the microprocessor I / O is set to input state DHT22 Microprocessor I/O set to output, while output low, and low hold time can not be less than 800us, typical values are down 1MS, then the microprocessor I/O is set to input state

min  typ  max

Tbe Host the start signal down time 0.8 < 1 > 20 mS

19 / 1000.to_f
VALID_BYTE_SIZE =

humidity_high, humidity_low, temp_high, temp_low, parity

5
BITS_IN_BYTE =
8
HUMIDITY_PRECISION =
10.to_f
TEMPERATURE_PRECISION =
10.to_f
ENOUGH_TO_CAPTURE_COUNT =

collecting high/low this many times should be enough (rough estimate…)

1000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pin) ⇒ Base

Returns a new instance of Base.



53
54
55
# File 'lib/rpi/dht/base.rb', line 53

def initialize(pin)
  @pin = pin
end

Class Method Details

.read(pin, tries: 50) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rpi/dht/base.rb', line 35

def read(pin, tries: 50)
  tries.to_i.times do
    value = ignore_exception { read!(pin) }
    return value if value.is_a?(Hash)
    sleep 0.1
  end

  nil
end

.read!(pin) ⇒ Object



28
29
30
31
32
33
# File 'lib/rpi/dht/base.rb', line 28

def read!(pin)
  dht = new(pin)
  dht.send_start_signal
  dht.collect_response_bits
  dht.convert
end

Instance Method Details

#collect_response_bitsObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/rpi/dht/base.rb', line 67

def collect_response_bits
  # https://i.gyazo.com/a3e13113125d56326517fa936c6011fc.png
  # The INPUT part (until the release just below)
  RPi::GPIO.setup pin, as: :input, pull: :up
  @bits = ENOUGH_TO_CAPTURE_COUNT.times.collect { RPi::GPIO.high?(pin) }
  release

  break_into_byte_strings
  check_parity!
end

#send_start_signalObject



57
58
59
60
61
62
63
64
65
# File 'lib/rpi/dht/base.rb', line 57

def send_start_signal
  # https://i.gyazo.com/a3e13113125d56326517fa936c6011fc.png
  # The OUTPUT part
  RPi::GPIO.setup pin, as: :output, initialize: :high
  sleep(CLEAR_SIGNALS)

  RPi::GPIO.set_low pin
  sleep(START_SIGNAL)
end