Class: MyFirmataPluginPir

Inherits:
Object
  • Object
show all
Defined in:
lib/myfirmata-plugin-pir.rb

Overview

note: the conditional statement for publishing a notice was based on the code from the humble_rpi-plugin-pir gem

Instance Method Summary collapse

Constructor Details

#initialize(arduino, settings: {}, variables: {}) ⇒ MyFirmataPluginPir

Returns a new instance of MyFirmataPluginPir.



17
18
19
20
21
22
23
24
25
26
# File 'lib/myfirmata-plugin-pir.rb', line 17

def initialize(arduino, settings: {}, variables: {})
  
  @arduino = arduino
  @settings, @variables = settings, variables
  @pinx = @settings[:pin] || 2

  @arduino.pin_mode @pinx, ArduinoFirmata::INPUT
  @duration = settings[:duration] || '1 minute'
  
end

Instance Method Details

#startObject Also known as: on_start



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/myfirmata-plugin-pir.rb', line 28

def start()
       
  count = 0
  
  duration = @duration    
  notifier = @variables[:notifier]
  topic = @variables[:device_id]
  msg = @settings[:msg] || 'motion'
  pinx = @pinx
  
  t1 = Time.now - (ChronicDuration.parse(duration) + 10)   
  
  puts 'ready to detect PIR sensor'    
  
  @arduino.on :digital_read do |pin, high|

    if pin == pinx and high then
              
      count += 1

      if Time.now > t1 + ChronicDuration.parse(duration)  then

        notifier.notice \
            "%s/motion: detected %s times within the past %s" % \
            [topic, count, duration]
        t1 = Time.now
        count = 0
      end        
      
    end
  end

end