Inital commit

This commit is contained in:
2021-12-11 18:47:29 +01:00
commit abbd005e8a
10 changed files with 331 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
config/*.cfg
!config/Pinger.cfg

49
AddPinger.sh Normal file
View File

@@ -0,0 +1,49 @@
#!/bin/ash
###############################################################
# @scriptname: AddPinger #
# @description: #
# Creates configuration and service files #
# #
# @project: SmartPinger #
# @author: Bram Prieshof #
###############################################################
#Goto ProjectRoot
cd "$( dirname "$0" )"
#Request argument for type of new pinger
case $1 in
-s|--smart)
NewPingerType=Smart
;;
-b|-bt|--bt|--bluetooth)
NewPingerType=Bt
;;
*)
echo "Pinger type missing or incorrect."
echo "Use '-s' or '--smart' for a smart pinger"
echo "or use '-b', '--bt' or '--bluetooth' for a bluetooth pinger."
exit 10
;;
esac
#Ask for name for new pinger
read -p "Enter name of pinger to be added: " NewPingerName
#Create service file
cp ./templates/"$NewPingerType"Ping.service.template /etc/init.d/$NewPingerType'Ping'$NewPingerName
chmod +x /etc/init.d/$NewPingerType'Ping'$NewPingerName
sed -i -e "/PingerName=/c\PingerName=$NewPingerName" /etc/init.d/$NewPingerType'Ping'$NewPingerName
#Create config file
cp ./templates/"$NewPingerType"Ping.cfg.template ./config/$NewPingerType'Ping_'$NewPingerName.cfg
#Instructions
echo "Before stating pinger edit the following config $(pwd)/config/"$NewPingerType"Ping_"$NewPingerName".cfg"
echo "Start pinger using 'service "$NewPingerType'Ping'$NewPingerName" start'"
echo "Enable pinger on boot using 'rc-update add "$NewPingerType'Ping'$NewPingerName" '"
echo "also don't forget 'lbu commit'"

20
README.md Normal file
View File

@@ -0,0 +1,20 @@
# Smart and BT pinger
## Support for Domoticz or Mqtt
## Installation
General requirements: `fping bluez`.
Requirements Domoticz communication: `curl`.
Requirements mqtt communication: `mosquitto-clients`.
Expected installation directory `/opt/Pinger`.
Setup permissions using `chmod +x /opt/Pinger/bin/*`.
## Configuration
Use 'AddPinger.sh' to setup the pinger.
## Enable Bluetooth on raspberry pi's running alpine linux
Source: https://wiki.alpinelinux.org/wiki/Raspberry_Pi_3_-_Setting_Up_Bluetooth
* By default the UART Bluetooth controller is not automatically discovered,
enable it by uncommenting the line after `rpi bluetooth` in `/etc/mdev.conf`.
* Enable the bluetooth service on boot `rc-update add bluetooth`.
* Set `AutoEnable` to `true` under Policy in /etc/bluetooth/main.conf.
* Then tun'lbu commit' and reboot.

91
bin/BtPing.sh Normal file
View File

@@ -0,0 +1,91 @@
#!/bin/ash
###############################################################
# @scriptname: BtPinger #
# @description: #
# Checks if device is nearby using Bluetooth #
# and reports status to Home automation system #
# #
# @project: SmartPinger #
# @author: Bram Prieshof #
# @author: Branco van de Waal #
###############################################################
. $( dirname "$0" )/../config/BtPing_"$1".cfg
. $( dirname "$0" )/../config/Pinger.cfg
#Default disable log
: ${LogFile:="/dev/null"}
sendDomoticz() {
#Set messages to send to Domoticz
if [ "$DeviceStatus" = Online ]; then
DomoSendMSG=On
elif [ "$DeviceStatus" = Offline ]; then
DomoSendMSG=On
else
echo "WARN: device status is unset" | tee -a $LogFile
fi
# Check Online / Offline state of Domoticz device
if [ "$DomoSendMSG" != "$(curl -s "http://"$DomoticzAddr"/json.htm?type=devices&rid="$DomoticzIDX"" | grep '"Data" :' | awk '{ print $3 }' | sed 's/[!@#\$%",^&*()]//g')" ] ; then
echo "Updating Domoticz device" | tee -a $LogFile
curl -s "http://"$DomoticzAddr"/json.htm?type=command&param=switchlight&idx="$DomoticzIDX"&switchcmd="$DomoSendMSG"" 2>&1 1>/dev/null
fi
#Clean up
unset DomoSendMSG
}
sendMqtt() {
#Send MQTT device status message
mosquitto_pub -h $MqttIP -u $MqttUser -p $MqttPassword -r -q 1 -t $MqttBaseTopic/$DeviceName/Status -m "$(eval echo \$Mqtt"$Status"Msg)"
mosquitto_pub -h $MqttIP -u $MqttUser -p $MqttPassword -r -q 1 -t $MqttBaseTopic/$DeviceName/Technology -m "$Technology"
}
while [ 1 ]
do
#Prepair for run
DeviceStatus=Offline
# First bluetooth ping attempt
if [ $DeviceStatus = Offline ]; then
if [[ "$(l2ping -c5 -s32 -t1000 "$DeviceBTMac" > /dev/null 2>&1 && echo "On" || echo "Off")" == 'On' ]]; then
DeviceStatus="Online"
Technology="Bluetooth"
Attempt="1"
fi
fi
# Second bluetooth ping attempt
if [ $DeviceStatus = Offline ]; then
if [[ "$(l2ping -c5 -s32 -t1000 "$DeviceBTMac" > /dev/null 2>&1 && echo "On" || echo "Off")" == 'On' ]]; then
DeviceStatus="Online"
Technology="Bluetooth"
Attempt="2"
fi
fi
# If the device is still offline, set Technology and Attempt to None
if [ $DeviceStatus = Offline ]; then
Technology="N/A"
Attempt="N/A"
fi
#Report Current status
echo "$(date '+%d/%m/%Y %H:%M') | Device: $DeviceName | Status: $DeviceStatus | Technology: $Technology | Attempt: $Attempt |" | tee -a $LogFile
#SendMsg to automation system
if [[ "$DeviceStatus" != "$PreviousDeviceStatus" ]]; then
send$MsgProtocol
fi
#Wait for next run if device is online
if [ "$DeviceStatus" = Online ]; then
echo "Online, Waiting"
sleep 20
fi
#Save current status and clean up for next run
PreviousDeviceStatus=$DeviceStatus
unset DeviceStatus Technology Attempt
done

111
bin/SmartPing.sh Normal file
View File

@@ -0,0 +1,111 @@
#!/bin/ash
###############################################################
# @scriptname: SmartPinger #
# @description: #
# Checks if device is nearby first using WiFi, #
# if not it will check using Bluetooth #
# and reports status to Home automation system #
# #
# @project: SmartPinger #
# @author: Bram Prieshof #
# @author: Branco van de Waal #
###############################################################
. $( dirname "$0" )/../config/SmartPing_"$1".cfg
. $( dirname "$0" )/../config/Pinger.cfg
unset $1
#Default disable log
: ${LogFile:="/dev/null"}
sendDomoticz() {
#Set messages to send to Domoticz
if [ "$DeviceStatus" = Online ]; then
DomoSendMSG=On
elif [ "$DeviceStatus" = Offline ]; then
DomoSendMSG=Off
else
echo "WARN: device status is unset" | tee -a $LogFile
fi
# Check Online / Offline state of Domoticz device
if [ "$DomoSendMSG" != "$(curl -s "http://"$DomoticzIP"/json.htm?type=devices&rid="$DomoticzIDX"" | grep '"Data" :' | awk '{ print $3 }' | sed 's/[!@#\$%",^&*()]//g')" ] ; then
echo "Updating Domoticz device" | tee -a $LogFile
curl -s "http://"$DomoticzIP"/json.htm?type=command&param=switchlight&idx="$DomoticzIDX"&switchcmd="$DomoSendMSG"" 2>&1 1>/dev/null
fi
#Clean up
unset DomoSendMSG
}
sendMqtt() {
#Send MQTT device status message
mosquitto_pub -h $MqttIP -u $MqttUser -p $MqttPassword -r -q 1 -t $MqttBaseTopic/$DeviceName/Status -m "$(eval echo \$Mqtt"$Status"Msg)"
mosquitto_pub -h $MqttIP -u $MqttUser -p $MqttPassword -r -q 1 -t $MqttBaseTopic/$DeviceName/Technology -m "$Technology"
}
while [ 1 ]
do
#Prepair for run
DeviceStatus=Offline
# First network ping attempt
fping -c5 -b 32 -t1000 $DeviceIP > /dev/null 2>&1
if [ "$?" = 0 ] ; then
DeviceStatus="Online"
Technology="Wifi"
Attempt="1"
fi
# First bluetooth ping attempt
if [ $DeviceStatus = Offline ]; then
if [[ "$(l2ping -c5 -s32 -t1000 "$DeviceBTMac" > /dev/null 2>&1 && echo "On" || echo "Off")" == 'On' ]]; then
DeviceStatus="Online"
Technology="Bluetooth"
Attempt="1"
fi
fi
# Second network ping attempt
if [ $DeviceStatus = Offline ]; then
fping -c5 -b 32 -t1000 $DeviceIP > /dev/null 2>&1
if [ "$?" = 0 ] ; then
DeviceStatus="Online"
Technology="Wifi"
Attempt="2"
fi
fi
# Second bluetooth ping attempt
if [ $DeviceStatus = Offline ]; then
if [[ "$(l2ping -c5 -s32 -t1000 "$DeviceBTMac" > /dev/null 2>&1 && echo "On" || echo "Off")" == 'On' ]]; then
DeviceStatus="Online"
Technology="Bluetooth"
Attempt="2"
fi
fi
# If the device is still offline, set Technology and Attempt to None
if [ $DeviceStatus = Offline ]; then
Technology="N/A"
Attempt="N/A"
fi
#Report Current status
echo "$(date '+%d/%m/%Y %H:%M') | Device: $DeviceName | Status: $DeviceStatus | Technology: $Technology | Attempt: $Attempt |" | tee -a $LogFile
#SendMsg to automation system
if [[ "$DeviceStatus" != "$PreviousDeviceStatus" ]]; then
send$MsgProtocol
fi
#Wait for next run if device is online
if [ "$DeviceStatus" = Online ]; then
echo "Online, Waiting"
sleep 60
fi
#Save current status and clean up for next run
PreviousDeviceStatus=$DeviceStatus
unset DeviceStatus Technology Attempt
done

21
config/Pinger.cfg Normal file
View File

@@ -0,0 +1,21 @@
#Smart/BT Pinger general configuration
##Message Protocol
### Set to Mqtt or Domoticz
MsgProtocol=Mqtt
##Mqtt connection settings
MqttIP="127.0.0.1"
MqttUser=""
MqttPassword=""
##Mqtt message settings
MqttBaseTopic="PingPi"
MqttOnlineMsg="home"
MqttOfflineMsg="not_home"
##Domoticz connection settings
DomoticzAddr='127.0.0.1:8080'
##Uncomment/Comment to Enable/Disable logging
#LogFile=/opt/out.log

View File

@@ -0,0 +1,5 @@
# Set device parameters
DeviceName=''
DeviceBTMac=''
#Domoticz settings(Leave empty if using mqtt)
DomoticzIDX=''

View File

@@ -0,0 +1,13 @@
#!/sbin/openrc-run
PingerName=<NAME>
name=$RC_SVCNAME
description="Smartpinger ${PingerName}"
supervisor="supervise-daemon"
command="/opt/Pinger/bin/BtPing.sh"
command_args="${PingerName}"
command_user="root"
depend() {
after net bluetooth
}

View File

@@ -0,0 +1,6 @@
# Set device parameters
DeviceName=''
DeviceIP=''
DeviceBTMac=''
#Domoticz settings(Leave empty if using mqtt)
DomoticzIDX=''

View File

@@ -0,0 +1,13 @@
#!/sbin/openrc-run
PingerName=<NAME>
name=$RC_SVCNAME
description="Smartpinger ${PingerName}"
supervisor="supervise-daemon"
command="/opt/Pinger/bin/SmartPing.sh"
command_args="${PingerName}"
command_user="root"
depend() {
after net bluetooth
}