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.
2) Make this script run every minute with crontab:
chown +777 xiomi.sh
crontab -e
add this line:
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..
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 ]"
Number Battery "Battery [%.1f ]"
Number Xiomi_Sensor_Temp "Xiomi_Sensor_Temp [%.1f ]"
All done now you can read the sensor data and display ,log, automate etc..
Comments
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"]
connect error: Transport endpoint is not connected (107)
connect error: Transport endpoint is not connected (107)
Made my day :-)
Works perfectly with openhab now