how to connect xiomi temperature and humidity sensor to openhab without a gateway

I've a xiomi temperature and humidity sensor with bluetooth.

I'm Using openhab and MQTT so it's easy to connect:

1) Create this script (xiomi.sh) :

Replace BT with your sensor's mac address.

You can find it with:  sudo hcitool lescan

(look for MJ_HT_V1)

Change the IP address (192.168.1.100) to your MQQT server address.


#!/bin/bash

for pid in $(pidof -x xiomi.sh); do
    if [ $pid != $$ ]; then
        echo "[$(date)] : xiomi.sh : Process is already running with PID $pid"
        exit 1
    fi
done

bt="4C:65:A8:DC:AD:CF"
sensor="sensor"

RET=1
until [ ${RET} -eq 0 ]; do
    data=$(/usr/bin/timeout 20 /usr/bin/gatttool -b $bt --char-write-req --handle=0x10 -n 0100 --listen | grep "Notification handle" -m 2)
    RET=$?
    sleep 5
done

RET=1
until [ ${RET} -eq 0 ]; do
    battery=$(/usr/bin/gatttool -b $bt --char-read --handle=0x18 | cut -c 34-35)
    RET=$?
    sleep 5
done

temp=$(echo $data | tail -1 | cut -c 42-54 | xxd -r -p)
humid=$(echo $data | tail -1 | cut -c 64-74 | xxd -r -p)
batt=$(echo "ibase=16; $battery"  | bc)

if [[ "$temp" =~ ^[0-9]+(\.[0-9]+)?$ ]]
then
echo "$temp"
/usr/bin/mosquitto_pub -h 192.168.1.100 -V mqttv311 -t "xiaomi/$sensor/temp" -m "$temp"
fi

if [[ "$humid" =~ ^[0-9]+(\.[0-9]+)?$ ]]
then
/usr/bin/mosquitto_pub -h 192.168.1.100 -V mqttv311 -t "xiaomi/$sensor/humidity" -m "$humid"
fi

if [[ "$batt" =~ ^[0-9]+(\.[0-9]+)?$ ]]
then
/usr/bin/mosquitto_pub -h 192.168.1.100 -V mqttv311 -t "xiaomi/$sensor/battery" -m "$batt"


fi

2) Make this script run  every minute  with crontab:

chown +777 xiomi.sh

crontab -e

add this line:


* * * * * /home/openhabian/xiomi.sh

3) Define your things for mqtt:

Type number : Xiomi_Sensor_Humidity "Xiomi_Sensor_Humidity" [ stateTopic="xiaomi/sensor/humidity"]
        Type number : Xiomi_Sensor_Battery "Xiomi_Sensor_Battery" [ stateTopic="xiaomi/sensor/battery"]
        Type number : Xiomi_Sensor_Temp "Xiomi_Sensor_Temp" [ stateTopic="xiaomi/sensor/temp"]

4) define your items:

Number          Humidity    "Humidity [%.1f ]"      (gTemperature) { channel="mqtt:topic:myUnsecureBroker:mything:Xiomi_Sensor_Humidity" }
Number          Battery    "Battery [%.1f ]"      (gTemperature) { channel="mqtt:topic:myUnsecureBroker:mything:Xiomi_Sensor_Battery" }
Number          Xiomi_Sensor_Temp    "Xiomi_Sensor_Temp [%.1f ]"      (gTemperature) { channel="mqtt:topic:myUnsecureBroker:mything:Xiomi_Sensor_Temp" }

All done now you can read the sensor data and display ,log, automate etc..

Comments

bart said…
hi im new in scripts and oh but after run of this script i get :
connect error: Too many levels of symbolic links (40)
connect error: Too many levels of symbolic links (40)
connect error: Too many levels of symbolic links (40)

olso i changed things file :
Thing topic mi "mi" @ "xiaomi" {
Channels:
Type number : Xiomi_Sensor_Humidity "Xiomi_Sensor_Humidity" [ stateTopic="xiaomi/sensor/humidity"]
Type number : Xiomi_Sensor_Battery "Xiomi_Sensor_Battery" [ stateTopic="xiaomi/sensor/battery"]
Type number : Xiomi_Sensor_Temp "Xiomi_Sensor_Temp" [ stateTopic="xiaomi/sensor/temp"]
bart said…
and one more error :
connect error: Transport endpoint is not connected (107)
connect error: Transport endpoint is not connected (107)
Anonymous said…
Great how-to - thank you very much!
Anonymous said…
Thanks a lot!
Made my day :-)
Works perfectly with openhab now

Popular posts from this blog

how can you enable a windows firewall rule for a dynamic dns name or dns address

Bose speaker hack (companion 5) to turn on/off automatically

how to IR remote control via wemos d1 (esp8266) & mqtt & sonoff tasmota