Skip to content

Instantly share code, notes, and snippets.

@Coro365

Coro365/button.rb

Last active Nov 5, 2018
Embed
What would you like to do?
Emit IR signal when button is pushed
require "influxdb"
require "open3"
READ_SW_1_PIN = 1
SW_1_PIN = 2
LED_1_PIN = 3
INFLUXDB_ADRR = "http://hostname.local:8086/write?db=home-sensor"
LOCATION = 3
SYNC_INFLUXDB_SLEEPTIME = 5
RS_MONITORING_SLEEPTIME = 0.5
BT_MONITORING_SLEEPTIME = 0.01
FL_MONITORING_SLEEPTIME = 1
PREVENT_CHATTERING_TIME = 2
state_file_cache = ""
def door_open?
#io = WiringPi::GPIO.new
#io.pin_mode(READ_SW_1_PIN, WiringPi::INPUT)
%x(gpio mode #{READ_SW_1_PIN} in)
state = %x(gpio read #{READ_SW_1_PIN}).chr.to_i
if state == 0 or state == 1
return state
else
puts("ERROR:can not read pin.")
return nil
end
end
def door_monitoring(past)
loop do
now = door_open?
unless past == now
door_state_print(now)
door_state_post(now)
past = now
end
sleep RS_MONITORING_SLEEPTIME
end
end
def door_state_print(state)
state == 0 ? puts("=>OPEN") : puts("=>CLOSE")
end
def door_state_post(state)
payload = "ocaction,location=#{LOCATION} value=#{state}"
system("curl -i -XPOST '#{INFLUXDB_ADRR}' --data-binary '#{payload}'")
end
def button_monitoring
button = Thread.new do
puts("Start monitoring button")
loop do
%x(gpio mode #{SW_1_PIN} up)
state = %x(gpio read #{SW_1_PIN}).chr.to_i
print(state)
push_now if state == 0
sleep BT_MONITORING_SLEEPTIME
end
end
end
def push_now
state = state_file_cache
if state == "off"
p "now push on"
ir_light("on")
led(false)
update_state_file("on")
elsif state == "on"
p "now push off"
ir_light("off")
led(true)
update_state_file("off")
else
p "ERRPR:state file read error"
end
sleep PREVENT_CHATTERING_TIME
end
def update_state_file(state)
state_file_cache = state
File.open(__dir__ + "/state_file","w") {|f|f.print state }
p "update_state_file: " + state
end
def read_state_file
state = File.read(__dir__ + "/state_file")
state_file_cache = state
return state
end
def sync_state_file
sync_state = Thread.new do
puts("Start monitoring state file")
loop do
state_file_cache = read_state_file
p "state file: " + state_file_cache
sleep FL_MONITORING_SLEEPTIME
end
end
sleep 1
end
def refresh_led
refresh = Thread.new do
puts("Start refresh led")
loop do
p "refresh_led: " + state_file_cache
led(false) if state_file_cache == "on"
led(true) if state_file_cache == "off"
sleep 0.1
end
end
end
def ir_light(action)
if action == "on"
cmd = "irsend send_once 'light1' 'orange_on'"
Open3.popen3("#{cmd} > /dev/null 2>&1")
sw_action_post("orange_on")
p "on ok"
elsif action == "off"
cmd = "irsend send_once 'light1' 'off'"
Open3.popen3("#{cmd} > /dev/null 2>&1")
sw_action_post(action)
p "off ok"
end
end
def sw_action_post(action)
payload = "swaction,location=#{LOCATION},device='light1' value=\"#{action}\""
system("curl -i -XPOST '#{INFLUXDB_ADRR}' --data-binary '#{payload}'")
end
def led(state)
# state data type is bool
#led = WiringPi::GPIO
#led.pin_mode(SW_1_PIN, WiringPi::OUTPUT)
#led.digital_write(SW_1_PIN, WiringPi::HIGH) if state
#led.digital_write(SW_1_PIN, WiringPi::LOW) unless state
%x(gpio mode #{LED_1_PIN} out)
%x(gpio write #{LED_1_PIN} 1) if state
%x(gpio write #{LED_1_PIN} 0) unless state
end
p "hi"
sync_state_file
refresh_led
button_monitoring
p "end"
while 1;end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment