From abbd005e8afa7466ef20411e4d28b84e8891cb79 Mon Sep 17 00:00:00 2001 From: Bram Prieshof Date: Sat, 11 Dec 2021 18:47:29 +0100 Subject: [PATCH] Inital commit --- .gitignore | 2 + AddPinger.sh | 49 ++++++++++++ README.md | 20 +++++ bin/BtPing.sh | 91 ++++++++++++++++++++++ bin/SmartPing.sh | 111 +++++++++++++++++++++++++++ config/Pinger.cfg | 21 +++++ templates/BtPing.cfg.template | 5 ++ templates/BtPing.service.template | 13 ++++ templates/SmartPing.cfg.template | 6 ++ templates/SmartPing.service.template | 13 ++++ 10 files changed, 331 insertions(+) create mode 100644 .gitignore create mode 100644 AddPinger.sh create mode 100644 README.md create mode 100644 bin/BtPing.sh create mode 100644 bin/SmartPing.sh create mode 100644 config/Pinger.cfg create mode 100644 templates/BtPing.cfg.template create mode 100644 templates/BtPing.service.template create mode 100644 templates/SmartPing.cfg.template create mode 100644 templates/SmartPing.service.template diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e88f1d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config/*.cfg +!config/Pinger.cfg \ No newline at end of file diff --git a/AddPinger.sh b/AddPinger.sh new file mode 100644 index 0000000..622018a --- /dev/null +++ b/AddPinger.sh @@ -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'" \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e0fa9b1 --- /dev/null +++ b/README.md @@ -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. diff --git a/bin/BtPing.sh b/bin/BtPing.sh new file mode 100644 index 0000000..d67b413 --- /dev/null +++ b/bin/BtPing.sh @@ -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¶m=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 diff --git a/bin/SmartPing.sh b/bin/SmartPing.sh new file mode 100644 index 0000000..69f9e4f --- /dev/null +++ b/bin/SmartPing.sh @@ -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¶m=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 diff --git a/config/Pinger.cfg b/config/Pinger.cfg new file mode 100644 index 0000000..178b301 --- /dev/null +++ b/config/Pinger.cfg @@ -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 diff --git a/templates/BtPing.cfg.template b/templates/BtPing.cfg.template new file mode 100644 index 0000000..d819a48 --- /dev/null +++ b/templates/BtPing.cfg.template @@ -0,0 +1,5 @@ +# Set device parameters +DeviceName='' +DeviceBTMac='' +#Domoticz settings(Leave empty if using mqtt) +DomoticzIDX='' diff --git a/templates/BtPing.service.template b/templates/BtPing.service.template new file mode 100644 index 0000000..9c16113 --- /dev/null +++ b/templates/BtPing.service.template @@ -0,0 +1,13 @@ +#!/sbin/openrc-run +PingerName= + +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 +} diff --git a/templates/SmartPing.cfg.template b/templates/SmartPing.cfg.template new file mode 100644 index 0000000..69b4a50 --- /dev/null +++ b/templates/SmartPing.cfg.template @@ -0,0 +1,6 @@ +# Set device parameters +DeviceName='' +DeviceIP='' +DeviceBTMac='' +#Domoticz settings(Leave empty if using mqtt) +DomoticzIDX='' diff --git a/templates/SmartPing.service.template b/templates/SmartPing.service.template new file mode 100644 index 0000000..8bade43 --- /dev/null +++ b/templates/SmartPing.service.template @@ -0,0 +1,13 @@ +#!/sbin/openrc-run +PingerName= + +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 +}