Tag: bluetooth

Qingping ClearGrass CGDK2 factory reset

The Qingping Temp & RH Monitor Lite is a cool little BLE device to monitor the temperature inside your home ,

If you are like me , you don’t really like the mihome app and want to use Home Assistant to automate your place.
Unlike the LYWSDCGQ/01ZM the CGDK2 encrypt the message broadcasted , so you can’t just add the device to home assistant and get readings, you need the encryption key or pairing key ,
The problem that I encountered to get that key is that you need to use a modified version of mihome , create a logs folder in the /vevs/ folder in your phone storage , and then, add the device to mihome for the key to be written in /vevs/logs/misc/pairings.txt
in my case , i added the device before creating the logs folder , so i didn’t have the pairing key .
I figured I just needed to delete and re-add the device to mi-home , but it didn’t go that way !
I tried and tried again and all I got was the error standard verification failed (-29).

Since I resolved this issue , I figured I share how i managed to re-add the Qingping Temp & RH Monitor Lite to mi-home .
I needed to do a factory reset on the device .
to do that you need to :
– Keep the pairing button pressed for 8 sec until all the segments of the lcd become black.
– Release the button for 1 sec ,
– Then push the pairing button again for 2 sec ,

The display flash then get back to displaying the temperature as usual, but now you device is factory reseted .
You can now add the device to mihome app and fetch the pairing key in /vevs/logs/misc/pairings.txt

now continue your configuration in home assistant using the Passive BLE Monitor integration

Using bash and gatttool to get readings from Xiaomi Mijia LYWSD03MMC Temperature Humidity sensor

There is a new inexpensive Temperature and humidity sensor by xiaomi.
This time is no longer round ,

Xiaomi Mijia LYWSD03MMC Bluetooth 4.2 Temperature Humidity sensor

if you like me would like to get the temperature and humidity data from time to time to import in a graphing tool like grafana, there is a simple solution using classic bash tools and gatttool.
First you have to indentify the mac-address of your little sensor, for this , just make sure the sensor is in range of your linux device ans launch a
hcitool lescann

this command will spit out all the bluetooth devices in range
just find the line with the name of the device and copy the mac address A4:C1:38:8C:77:CA LYWSD03MMC

then you must start a little bash script like this :

#!/bin/bash
bt=$(timeout 15 gatttool -b A4:C1:38:8C:77:CA --char-write-req --handle='0x0038' --value="0100" --listen)
if [ -z "$bt" ]
then
echo "The reading failed"
else
echo "Got data"
echo $bt temphexa=$(echo $bt | awk -F ' ' '{print $12$11}'| tr [:lower:] [:upper:] )

humhexa=$(echo $bt | awk -F ' ' '{print $13}'| tr [:lower:] [:upper:])
temperature100=$(echo "ibase=16; $temphexa" | bc)
humidity=$(echo "ibase=16; $humhexa" | bc)
echo "scale=2;$temperature100/100"|bc
echo $humidity
fi

this is a skeleton that you can improve , but for now it pretty much work like that ,

first it use gatttol to connect to the sensor and listen for 15 sec
During these fifteen seconds , you can be pretty much sure to receive at least some data like this :

Characteristic value was written successfully Notification handle = 0x0036 value: 58 03 47 a0 0b Notification handle = 0x0036 value: 55 03 47 a0 0b

this tell me that during the 15 sec of connection i received the information that i need two times.
what i’m after are the value 58 03 for the temperature , and 47 for the humidity.

the temperature is little endian format its mean that the two group must be inverted before decoding the data. i invert the values with awk and decode them using bc.
bc doesn’t like when the hex values are not capitalised so tr is used to do that.

bc then give you the temperature multiplied by 100. relaunch bc to divice per 100,

For the humidity its simpler , you get the value in one step without inverting anyting

Then you can do what you need with theses two vars , insert then in some database etc ..

there is room from improvement: for example this script is not capable of decoding negative temperature.
i will post an improved version when i figure out how to do it