I’m trying to make this device usable for our purposes. I read something using the message published through mosquitto, but it lacks of ble device.
So I took inspiration from the work of AlexxIT for Home Assistant Xiaomi Gateway 3 integration for Home Assistant.
I’ve also changed the firmware https://github.com/serrj-sv/lumi.gateway.mgl03, useful to run custom shell on startup.
My attempt is to convert log data from the “miio_client” program to be readable from mqtt binding of openHAB.
Now ble device write message like this:
log/ble/A4C138E763AE/1371/4102
And zigbee:
log/zb/lumi.158d0004384fb4
Reporting the value in this form
{ "pressure": 100920 }
{ "humidity": 6074}
Raw message are all published in:
log/raw
Like this:
{"method":"_sync.ble_query_dev","params":{"mac":"C4:7C:8D:6B:62:C2","pdid":152},"id":466}
Heartbeat and stat information:
log/heartbeat
log/stat
What I did is only for my few device, but could be useful for someone who wants to use this gateway for reading their. A lot of information is on AlexxIT github, as the key to decode eid.
Command instruction must be sent on usual form.
So what I did on run.sh (once telnet in the gateway):
#!/bin/sh
/bin/dropbear -R -B
#Uccido i demoni sottostanti i client miio
killall daemon_miio.sh; killall miio_client
#Carico il server FTP per scaricarsi db o log
/bin/sftp-server
#Riavvio il client, filtrando il testo e mappandolo su mqtt
(miio_client -l 0 -o FILE_STORE -n 128 -d /data/miio | awk '/miio_client_rpc/{print $0;fflush()}' | sh /data/log_mqtt.sh &)
#Riavvio il demone di sicurezza
daemon_miio.sh &
Regarding the “log_mqtt.sh” script:
#!/bin/sh
##Da copiare nel run.sh
#killall daemon_miio.sh
#killall miio_client
#killall inviomqtt.sh
#(miio_client -l 0 -o FILE_STORE -n 128 -d /data/miio | awk '/miio_client_rpc/{print $0;fflush()}' | sh /data/log_mqtt.sh &)
#daemon_miio.sh &
while IFS= read -r line; do
##Debug per vedere cosa filtra l'awk in ingresso
#iniz_sng="$line"
#echo $iniz_sng >> /var/log/inviomqtt.log
#Per togliere i caratteri tipo ESC che trovo alla fine della riga e sembra dar problemi al compilatore jshon
iniz_sng=$(echo $line | sed 's/[^[:print:]]//g' )
#Elimino inizio e fine che non ci interessano
jsonsng=$(echo $iniz_sng | sed 's/.*call=\(.*\)\[0m.*/\1/' )
#Nel caso di più json, aggiungo la ; per fare lo split
jsonsng_tmp=${jsonsng//\}\{/\};\{}
#Istruisco per lo split
OIFS=$IFS
IFS=";"
set $jsonsng_tmp
IFS=$OIFS
#Ciclo per tutti gli elementi dell'array
for item
do
##Debug per leggere il gingolo json in analisi
#echo $item >> /var/log/inviomqtt_dett.log
#Debug LOG
mosquitto_pub -t log/raw -m "$item"
method=$(echo $item | jshon -C -e method -u)
#Caso dei dispositivi ZigBee
if [ "$method" == "props" ]
then
sid=$(echo $item | jshon -C -e sid -u)
params=$(echo $item | jshon -C -e params | sed 's/[][]//g' )
#id=$(echo $item | jshon -C -e id -u)
#model=$(echo $item | jshon -e model -u)
#Se il campo è vuoto, per non mandare messaggi inutili
if [ ! -n "$sid" ]
then
sid="nd"
fi
#Invio il messaggio ZigBee
mosquitto_pub -t log/zb/$sid -m "$params"
fi
#Caso dispositivi BLE
if [ "$method" == "_async.ble_event" ]
then
pdid=$(echo $item | jshon -C -e params -e dev -e pdid -u)
mac=$(echo $item | jshon -C -e params -e dev -e mac -u | sed -e 's/://g')
evt=$(echo $item | jshon -C -e params -e evt )
if [ ! -n "$mac" ]
then
mac="nd"
fi
if [ ! -n "$pid" ]
then
pid="nd"
fi
if [ ! -n "$evt" ]
then
evt=""
else
#Pulizia della stringa
evt_tmp=$(echo $evt | sed 's/[][]//g')
eid=$(echo $evt_tmp | jshon -C -e eid )
edata=$(echo $evt_tmp | jshon -C -e edata -u)
if [ ! -n "$eid" ]
then
eid="nd"
fi
fi
#Invio in due formati
mosquitto_pub -t log/ble/$mac/$pdid -m "$evt"
mosquitto_pub -t log/ble/$mac/$pdid/$eid -m "$edata"
fi
#Caso HeartBeat - Stato del gateway
if [ "$method" == "event.gw.heartbeat" ]
then
params=$(echo $item | jshon -e params | sed 's/[][]//g' )
mosquitto_pub -t log/heartbeat -m "$params"
fi
#Caso statistiche d'utilizzo
if [ "$method" == "_async.stat" ]
then
params=$(echo $item | jshon -e params | sed 's/[][]//g' )
mosquitto_pub -t log/stat -m "$params"
fi
done
done
One question. I have to read hex data from ble deviced.
To do this, I should convert edata from HEX to byte and then to integer (and then /10).
A little bit difficult on shell.
So how to do this on openHAB ?
AlexxIT, using py, do it on this way:
data = bytes.fromhex(event['edata'])
return {'temperature': int.from_bytes(data, 'little', signed=True) / 10.0}