This is the Asterisk dialplan for triggering the 4 channels ON and OFF.
;; Home Automation Box - 4 channels relay board
;; Asterisk 11 & OpenWRT
;; see https://forum.openwrt.org/viewtopic.php?id=53874
;; By pilovis - parknat12@yahoo.com
;
; put this code in "/etc/asterisk/extensions.conf" under [default] section
;
;Relay 1 = key 1 on , key 5 off
;Relay 2 = key 2 on , key 6 off
;Relay 3 = key 3 on , key 7 off
;Relay 4 = key 4 on , key 8 off
;IVR password: 1234
;
; IVR with vocal guide.
; find asterisk sound files (.gsm) on your language named:
;"1,2,3,4,5,6,7,8,agent-pass,auth-incorrect,auth-thankyou,activated,de-activated,vm-goodbye"
; and copy them to /usr/lib/asterisk/sounds of your OpenWRT router
;
exten => xxxxxxxxxx,1,Answer()
exten => xxxxxxxxxx,n,Ringing
exten => xxxxxxxxxx,n,Set(TIMEOUT(digit)=7)
exten => xxxxxxxxxx,n,Set(TIMEOUT(response)=21)
; password "1234" required to continue
exten => xxxxxxxxxx,n,Authenticate(1234,,4)
; IVR vocal guide
exten => xxxxxxxxxx,n,Background(1&2&3&4&activated&5&6&7&8&de-activated)
exten => xxxxxxxxxx,n,WaitExten(5,4)
exten => xxxxxxxxxx,n,Hangup()
;;
exten => 1,1,Playback(1&activated)
exten => 1,2,System(/root/on1.sh &)
exten => 1,3,Wait(2)
exten => 1,4,Hangup()
;;
exten => 2,1,Playback(2&activated)
exten => 2,2,System(/root/on2.sh &)
exten => 2,3,Wait(2)
exten => 2,4,Hangup()
;;
exten => 3,1,Playback(3&activated)
exten => 3,2,System(/root/on3.sh &)
exten => 3,3,Wait(2)
exten => 3,4,Hangup()
;;
exten => 4,1,Playback(4&activated)
exten => 4,2,System(/root/on4.sh &)
exten => 4,3,Wait(2)
exten => 4,4,Hangup()
;;
exten => 5,1,Playback(1&de-activated)
exten => 5,2,System(/root/off1.sh &)
exten => 5,3,Wait(2)
exten => 5,4,Hangup()
;;
exten => 6,1,Playback(2&de-activated)
exten => 6,2,System(/root/off2.sh &)
exten => 6,3,Wait(2)
exten => 6,4,Hangup()
;;
exten => 7,1,Playback(3&de-activated)
exten => 7,2,System(/root/off3.sh &)
exten => 7,3,Wait(2)
exten => 7,4,Hangup()
;;
exten => 8,1,Playback(4&de-activated)
exten => 8,2,System(/root/off4.sh &)
exten => 8,3,Wait(2)
exten => 8,4,Hangup()
; eof
Note: change "xxxxxxxxxx," with your extension.
- Examples of the ON & OFF scripts:
(don't forget to give "chmod 755" to the files)
/root/on1.sh
#!/bin/sh
# switch on relay 1
/bin/echo 1 > /sys/devices/platform/leds-gpio.0/leds/DGND3700v1_3800B:blue:wifi5g/brightness
# Insert channel status (1=on, 0=off) for channel recovering after a reboot/crash
/bin/rm /root/status-relay1
/bin/cat /sys/devices/platform/leds-gpio.0/leds/DGND3700v1_3800B:blue:wifi5g/brightness >> /root/status-relay1
/root/off1.sh
#!/bin/sh
# swith off relay 1
/bin/echo 0 > /sys/devices/platform/leds-gpio.0/leds/DGND3700v1_3800B:blue:wifi5g/brightness
# Insert channel status (1=on, 0=off) for channel recovering after a reboot/crash
/bin/rm /root/status-relay1
/bin/cat /sys/devices/platform/leds-gpio.0/leds/DGND3700v1_3800B:blue:wifi5g/brightness >> /root/status-relay1
- Script to extract channel status and restore it at reboot
(put the line: "/bin/sh /root/extract_and_restore-status-relay1.sh" in "/etc/rc.local" right before the "exit 0" line)
/root/extract_and_restore-status-relay1.sh
#!/bin/sh
# extract and restore status of relay1
#
while IFS=: read relay1
do
/bin/echo ${relay1} > /sys/devices/platform/leds-gpio.0/leds/DGND3700v1_3800B:blue:wifi5g/brightness
done < /root/status-relay1
- Configuration file for triggerhappy (/etc/triggerhappy/triggers.d/example.conf) to control the 4 relay by an USB keypad.
# This is an example configuration for the triggerhappy daemon (thd)
# please note that every file to be processed must end in ".conf"
#
# To view a list of supported event codes, use "thd --listevents" or
# "thd --dump /dev/input/event*"
#
# Format:
# <eventcode> <value> <command>
#
# values for key events are 1 (pressed), 0 (released) or 2 (held)
#
# Relay 1 = key 1 on , key 5 off
# Relay 2 = key 2 on , key 6 off
# Relay 3 = key 3 on , key 7 off
# Relay 4 = key 4 on , key 8 off
#
KEY_KP1 1 /bin/sh /root/on1.sh
KEY_KP2 1 /bin/sh /root/on2.sh
KEY_KP3 1 /bin/sh /root/on3.sh
KEY_KP4 1 /bin/sh /root/on4.sh
KEY_KP5 1 /bin/sh /root/off1.sh
KEY_KP6 1 /bin/sh /root/off2.sh
KEY_KP7 1 /bin/sh /root/off3.sh
KEY_KP8 1 /bin/sh /root/off4.sh
# switch off all 4 channels
KEY_NUMLOCK 1 /root/off1.sh ; /root/off2.sh ; /root/off3.sh ; /root/off4.sh
# switch on all 4 channels
KEY_KPDOT 1 /root/on1.sh ; /root/on2.sh ; /root/on3.sh ; /root/on4.sh
(Last edited by pilovis on 15 Sep 2016, 21:34)