Xiaomi Thermostat (with display)

Others (MiLight, Hue, Toon etc...)
Chakkie
Posts: 37
Joined: Wednesday 23 December 2015 12:27
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands
Contact:

Re: Xiaomi Thermostat (with display)

Post by Chakkie » Sunday 11 March 2018 12:26

RadiQal wrote:
Sunday 04 February 2018 21:35
PaulFen wrote:
Tuesday 30 January 2018 15:40
Check out https://github.com/pFenners/mijia-sensor-domoticz

I got my 3 Xiaomi Mijia Bluetooth Temperature Humidity Sensors linked up to Domoticz using the built-in bluetooth on Raspberry Pi3
Works pretty well, however I need to set my Mijia BLE Temp + Humidity sensor in 'pairing' (flasing BT icon) mode before I get any readouts. When the RPI sets up the connection, the script updates Domoticz flawless though...
Maybe the sensor needs to be unpaired with any devices?

Else I receive connect error: Transport endpoint is not connected (107)
I have the same issue. Have you solved it yet? Basically I have to pressed and hold the pairing button to get the data readout.
I use the script from freibeuter

ayziaa
Posts: 4
Joined: Wednesday 21 September 2016 20:45
Target OS: -
Domoticz version:
Contact:

Re: Xiaomi Thermostat (with display)

Post by ayziaa » Wednesday 21 March 2018 13:48

Chakkie wrote:
Sunday 11 March 2018 12:26
RadiQal wrote:
Sunday 04 February 2018 21:35
PaulFen wrote:
Tuesday 30 January 2018 15:40
Check out https://github.com/pFenners/mijia-sensor-domoticz

I got my 3 Xiaomi Mijia Bluetooth Temperature Humidity Sensors linked up to Domoticz using the built-in bluetooth on Raspberry Pi3
Works pretty well, however I need to set my Mijia BLE Temp + Humidity sensor in 'pairing' (flasing BT icon) mode before I get any readouts. When the RPI sets up the connection, the script updates Domoticz flawless though...
Maybe the sensor needs to be unpaired with any devices?

Else I receive connect error: Transport endpoint is not connected (107)
I have the same issue. Have you solved it yet? Basically I have to pressed and hold the pairing button to get the data readout.
I use the script from freibeuter
any news? I was planing on buying one device before I saw your post.

Chakkie
Posts: 37
Joined: Wednesday 23 December 2015 12:27
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands
Contact:

Re: Xiaomi Thermostat (with display)

Post by Chakkie » Wednesday 21 March 2018 17:59

[/quote]
any news? I was planing on buying one device before I saw your post.
[/quote]

It is working perfectly now.

First complete the tutorial for miflora

https://www.domoticz.com/wiki/Mi_Flora_Bluetooth_LE

Then run the domoticz_mijia.py script

https://github.com/pFenners/mijia-sensor-domoticz

Since I am running the old OS (Wheezy) Python3 will not work and got error when running the script

So you need to install Python 3.4

so insteads of
usr/bin/python3

usr/bin/python3.4

Furthermore you need to run the following commands at the start up of the system.

sudo tools/btmgmt le on
sudo tools/btmgmt connectable on
sudo tools/btmgmt power on

So put the command in a bash script and put the script in /etc/init.d/

mspider65
Posts: 1
Joined: Friday 06 April 2018 9:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Xiaomi Thermostat (with display)

Post by mspider65 » Friday 06 April 2018 11:58

I also use these sensors in my home automation system and starting from the source code of the hcitool command, i have created a simple tool that allows to obtain the measurements sent by the sensor.
https://github.com/mspider65/Xiaomi-Mij ... ity-Sensor

This is also what I learned by analyzing the Bluetooth messages sent by the device.
As soon as the battery is inserted, the sensor starts sending advertisement data every two seconds and the advertisement data contains all the useful information (Temperature, Humidity and Battery Level).
You can test it with the two Bluez commands hcitool and hcidump (or any other tool able to read advertisement data sent by BLE devices).
In order to show the raw data sent by the sensor, you should first start in background a BT le scan and then print the raw data.

Code: Select all

sudo hcitool lescan --passive &
hcidump --raw
The option '--passive' means that you are not sending any request to the sensor but you are just receiving the advertisements sent by the BLE devices.
If you scan without '--passive' option, you will send a scan request, and the device will respond with some other additional data (Sensor Name, available UUIDs) but it is not necessary and you will only waste the sensor battery.

Looking at the hcidump output you can identify four type of messages.

- Temperature & Humidity:

Code: Select all

Header:			04 3E 25 02 01 00 00 XX XX XX XX XX XX 19
Flags AD:		02 01 06
Service Data AD:	15 16 95 FE 50 20 AA 01 17 XX XX XX XX XX XX 0D 10 04 B9 00 5C 02
RSSI:			A4
- Battery Level

Code: Select all

Header:			04 3E 22 02 01 00 00 XX XX XX XX XX XX 16
Flags AD:		02 01 06
Service Data AD:	12 16 95 FE 50 20 AA 01 12 XX XX XX XX XX XX 0A 10 01 64
RSSI:			A6
- Humidity Only:

Code: Select all

Header:			04 3E 23 02 01 00 00 XX XX XX XX XX XX 17
Flags AD:		02 01 06
Service Data AD:	13 16 95 FE 50 20 AA 01 14 XX XX XX XX XX XX 06 10 02 5C 02
RSSI:			A6
- Temperature Only:

Code: Select all

Header:			04 3E 23 02 01 00 00 XX XX XX XX XX XX 17
Flags AD:		02 01 06
Service Data AD:	13 16 95 FE 50 20 AA 01 19 XX XX XX XX XX XX 04 10 02 B9 00
RSSI:			A7
The "Service Data AD" is the part of the message that carries the useful information.
But first we will give a look to the whole message.

Message Description
As example we will use the Temperature & Humidity message.
04 3E 25 02 01 00 00 XX XX XX XX XX XX 19 02 01 06 15 16 95 FE 50 20 AA 01 17 XX XX XX XX XX XX 0D 10 04 B9 00 5C 02 A6

Code: Select all

	Pos.	Value(hex)	Description
Header
	0	04		HCI Packet Type HCI Event
	1	3E		LE Meta event
	2	25		Nr of following bytes (37 bytes)
	3	02		LE Advertising report sub-event
	4	01		Number of reports
	5	00		Event type (connectable and scannable undirected advertising)
	6	00		Public address type
	7..12	XX..XX		BT Address (6 bytes)
	13	19		length of data field (25 bytes)
Data
	14	02		length of first advertising data (AD) structure
	15	01		type of first AD structure (0x01 = Flags)
	16	06		first AD data (0x06=LE General Discoverable Mode and BR/EDR Not Supported)
	17	15		length of second advertising data (AD) structure
	18	16		type of second AD structure (0x16=Service Data)
	19..38	95..02		Second AD data. This is the field with all the useful informations (see next table for details)
RSSI
	39	A6		RSSI value (signed byte, 0xA6=-90db)
Service Data AD
The most important information is contained into the second AD structure (type 0x16).
The Bluetooth specification defines the AD Type 0x16 as “Service Data”. This AD must contain at least two bytes representing the 16 bit UUID of this Service Data. The data following the UUID is custom data and the meaning is not defined in the Bluetooth specification.
The table below shows the meaning of “Service Data” AD for our sensor. As example we will use the Service Data AD of the same Temperature & Humidity message as above.
95 FE 50 20 AA 01 17 XX XX XX XX XX XX 0D 10 04 B9 00 5C 02

Code: Select all

Pos.	Value (Hex)	Description
0..1	95 FE	        UUID 16 bit (0xFE95)
2..5	50 20 AA 01	TBD (always 50 20 AA 01 for my three sensors)
6	17		Message counter (increments by 1 every message and then restart from 0 when reaches FF)
7..12	XX .. XX	Device BT address (6 bytes)
13	0D 	        Data type:
				0D -> Temperature & Humidity (4 bytes TL,TH,HL,HH)
				0A -> battery % (1 byte)
				06 -> only Humidity (2 bytes HL,HH)
				04 -> only Temperature (2 bytes TL, TH)
14	10		TBD (always 0x10 for my three sensors)
15	04		Nr of following bytes (depends on Data Type)
16..N	B9 00 5C 02	Data values:
				0x00B9 = 185 -> 18.5 C (Temperature)
				0x025C = 604 -> 60.4 % (Humidity)

k0nstantin
Posts: 1
Joined: Saturday 28 April 2018 14:33
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Xiaomi Thermostat (with display)

Post by k0nstantin » Saturday 28 April 2018 14:42

Hi
Can anyone help a beginner?
I installed this script:

https://github.com/pFenners/mijia-sensor-domoticz

When I run the script, the sensor gives the correct data, and then errors appear.

1: updating
Mi Sensor: 4C:65:A8:DD:66:33
Firmware: 00.00.66
Name: MJ_HT_V1
Temperature: 24.0°C
Humidity: 39.7%
Battery: 90%
http://127.0.0.1:8000/json.htm?type=com ... battery=90
Traceback (most recent call last):
File "/usr/lib/python3.5/urllib/request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.5/http/client.py", line 1107, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.5/http/client.py", line 1152, in _send_request
self.endheaders(body)
File "/usr/lib/python3.5/http/client.py", line 1103, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/usr/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/usr/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.5/socket.py", line 712, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 703, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "domoticz_mijia.py", line 92, in
update("4C:65:A8:DD:66:33","41")
File "domoticz_mijia.py", line 88, in update
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_temp + "&nval ue=0&svalue=" + val_temp + ";" + val_hum + ";"+ val_comfort + "&battery=" + val_bat)
File "domoticz_mijia.py", line 29, in domoticzrequest
response = urllib.request.urlopen(request)
File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.5/urllib/request.py", line 466, in open
response = self._open(req, data)
File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
'_open', req)
File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/usr/lib/python3.5/urllib/request.py", line 1282, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/lib/python3.5/urllib/request.py", line 1256, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 111] Connection refused>

Maybe you can tell me what to do to remove these errors, and, the data was transmitted to the virtual adapter domoticz?

P.S. I also install the script of miflora (for Xiaomi Flower Monitor). It works without any problems.

wbsch
Posts: 1
Joined: Sunday 03 June 2018 9:45
Target OS: -
Domoticz version:
Contact:

Re: Xiaomi Thermostat (with display)

Post by wbsch » Sunday 03 June 2018 10:18

mspider65 wrote:
Friday 06 April 2018 11:58
I also use these sensors in my home automation system and starting from the source code of the hcitool command, i have created a simple tool that allows to obtain the measurements sent by the sensor.
https://github.com/mspider65/Xiaomi-Mij ... ity-Sensor
Thank you so much for the work you put into that, and thank you even more for making it available to others! The protocol description saved me a lot of time.
I tried sending an e-mail to the address you used in those git commits, but that appears to be a dead end. Since it was basically just this, i.e. saying "Thank you!", I guess this forum will do.

I ported this over to Python and bluepy for my own use, but that's probably not relevant here since domoticz appears to be written in C++ (I've never actually used it or looked at its source, mind you).

oxygen
Posts: 3
Joined: Tuesday 02 October 2018 11:38
Target OS: -
Domoticz version:
Contact:

Re: Xiaomi Thermostat (with display)

Post by oxygen » Tuesday 02 October 2018 11:44

Seems strange.
I'm fighting a bit against those toys.
I can't manage to get them to push back data via bluethooth.

It seems that they act as if they were requesting to be paired as they refuse to get pooled by my system.

Whatever I do, I face a "connection refused".
I've tried connecting via the python script, then gatttool directly, then bluethoothctl.
Everytime, I can't get any data out of the device.

Do you need to do something specific to let it broadcast/push back the values via bluethooth ?
I haven't checked via phone, just via plain old Linux.

oxygen
Posts: 3
Joined: Tuesday 02 October 2018 11:38
Target OS: -
Domoticz version:
Contact:

Re: Xiaomi Thermostat (with display)

Post by oxygen » Tuesday 02 October 2018 12:12

I'm finally getting something by using the

Code: Select all

hcitool lescan --passive &
hcidump --raw
But it seems a bit unpractical, and only allows me to get one single read out of the device.

I'm loosing my head around this thing :(

oxygen
Posts: 3
Joined: Tuesday 02 October 2018 11:38
Target OS: -
Domoticz version:
Contact:

Re: Xiaomi Thermostat (with display)

Post by oxygen » Wednesday 03 October 2018 11:01

Alright.
Good news, I've managed to fix the issue :)

It all goes down to an issue on the bluetooth stack configuration and most importantly the pnat plugin.

I'm running on an Ubuntu 16.04 LTS:

Code: Select all

 dpkg --status bluez | grep '^Version:'
Version: 5.37-0ubuntu5.1
And this version has a known bug on the pnat plugin:
https://bugs.debian.org/cgi-bin/bugrepo ... bug=690749

Disabling the pnat plugin fix this, by editing /etc/bluetooth/main.conf with

Code: Select all

DisablePlugins=pnat
All goes well now.
:D

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests