this is something that actually NUTSpace does not provide either (sorry guys, but this is a great goal for us )
here are the specs:
Nut Mini is a simple BLE device.
it does nothing more that its presence AND now we were able to identify the battery service and its value.
Here is how it works:
This script just check the battery and write the value to Domoticz
you have an array to populate with all the datas you need
Because the battery inquiry could drain it, it would be better to run the script once/twice a day (via cron)
in my specific scenario I have a beacon service that check the presence of NUTs... but both script cannot cohexist, so the service must be disabled first if you do not have a service you will be able to skip this part)
Here are the Domoticz requirements:
- String Variable for each NUT where to store the value
- ON/OFF Switch linked to the NUT to check state for continuing (this can be avoided)
- NUT MAC ADDRESS
here is the script:
Code: Select all
#!/bin/bash
# SETTING UP PARAMETERS
## DOMOTICZ SETUP (no authentication required, ensure that you have IP in whitelist)
DOMOIP="127.0.0.1"
DOMOPORT="8080"
DAWVAR="/json.htm?type=command¶m=updateuservariable&vname=VARIABLENAME&vtype=2&vvalue=VARIABLEVALUE"
DARDEV="/json.htm?type=devices&rid=DEVICEIDX"
DCHECKSTS="1"
#Check status of switches before continuing
#Set to 0 to force all configured NUT
BATTUUID="0x2a19"
BATTAWAYMSG="UNAVAILABLE"
# Creating Main array (MAC Address - Variable Name - Switch Name)
NUTBATT=(
"00:00:00:00:00:00" "Variabile_Nome_Batteria" "IDX switch ON/OFF"
"00:00:00:00:00:00" "Variabile_Nome_Batteria" "IDX switch ON/OFF"
"00:00:00:00:00:00" "Variabile_Nome_Batteria" "IDX switch ON/OFF"
)
# Other Parameters
USEBEACONSERVICE="1"
#Set to 0 if you do not have a beaconing serive deamon running
BEACONSERVICENAME="NomeServizioBeacon.service"
HCIINTERFACE="hci0"
function domoWriteVar () {
URLREQ="http://"$DOMOIP":"$DOMOPORT$DAWVAR
URLREQ="${URLREQ/VARIABLENAME/${NUTBATT[arrNut+1]}}"
URLREQ="${URLREQ/VARIABLEVALUE/$1}"
dzAPIWriteVar=$(curl -s "$URLREQ" )
}
function domoGetStatus {
URLREQ="http://"$DOMOIP":"$DOMOPORT$DARDEV
#echo $URLREQ
dzAPIStatus="${URLREQ/DEVICEIDX/${NUTBATT[arrNut+2]}}"
# Getting switch status prom Domoticz
dzDevRAW=$(curl -s "$dzAPIStatus")
dzDevJSON=$(echo ${dzDevRAW} | jq .result[0].Data)
dzDevSTATUS=$(echo $dzDevJSON | sed "s/\"//g")
}
function svcBeacon() {
if [[ $1 == "stop" ]]; then
echo "Stopping Beaconing Service"
sudo systemctl stop ${BEACONSERVICENAME}
fi
if [[ $1 == "start" ]]; then
echo "Starting Beaconing Service"
sudo systemctl start ${BEACONSERVICENAME}
fi
}
function restartHCI () {
sudo hciconfig ${HCIINTERFACE} down
sleep 1
sudo hciconfig ${HCIINTERFACE} up
}
function getBLEBat (){
restartHCI
HANDLE=$(sudo hcitool lecc --random ${NUTBATT[arrNut]} | awk '{print $3}')
sleep 1
sudo hcitool ledc $HANDLE
BATHEX=$(sudo gatttool -t random --char-read --uuid $BATTUUID -b ${NUTBATT[arrNut]} | awk '{print $4}')
BATDEC=$((0x$BATHEX))
if [ "$BATDEC" == "0" ]; then
BATDEC=$BATTAWAYMSG
fi
echo "Risultato finale: HEX :"$BATHEX" DEC: "$BATDEC
domoWriteVar $BATDEC
}
# BEGINNING MAIN SCRIPT
if [[ $USEBEACONSERVICE == "1" ]]; then
svcBeacon "stop"
fi
printf "\n- - - - - - - - - - - - - - -\n"
for arrNut in $(seq 0 3 $((${#NUTBATT[@]} - 1))); do
if [[ $DCHECKSTS == "1" ]]; then
domoGetStatus
echo "Analyzing NUT: "${NUTBATT[arrNut+1]}" Domoticz State: "$dzDevSTATUS
else
dzDevSTATUS="On"
fi
if [[ $dzDevSTATUS == "On" ]]; then
echo "Proceeding seeking for Battery Info"
restartHCI
getBLEBat
dzDevSTATUS="Off"
else
echo "NUT Unavailable, skipping"
fi
printf "\n- - - - - - - - - - - - - - -\n"
done
if [[ $USEBEACONSERVICE == "1" ]]; then
svcBeacon "start"
fi