Open MicheleSilla810 opened 3 years ago
For me it's only battery value
The issue i discovered is that this sensor transmits encrypted data. Maybe only if you connect to it directly it transmits unencrypted data but still that's annoying and tedious to do. I resolved by flashing the modified firmware from this repo and then installing the ble scanner featured in #12 . This new firmware transmits all the data without encryption in the ble broadcast message so it doesn't even need to be directly connected via bluetooth to the raspberry. This also allows to read the sensor data from other devices such as esp32's at the same time. The firmware transmits the temperature, humidity, signal and battery percentage and mVolts as well as the mac address. With the tool in the linked repo you can even set custom values for the temperature and humidity offset, you can manually change the face and you can choose to show the battery falue every 5 seconds. A cool feature is also that when you reinsert the battery it shows the last 3 bytes of the MAC address so you can identify the sensor. If anyone needs help i can post my node red function that i use filter the sensor from all other ble boardcast messages and format all the values.
Hi Michele,
if you could post your Node Red function that would be very useful!
This is the flow
The first function is only used to parse the JSON string as node-red's JSON convert node gives an error when converting the output of the BLEscanner. Here's the code.
return {"payload":JSON.parse(msg.payload)};
Then there's the switch node used to filter the BLE messages and only pass the ones coming from the sensor. I use the .id value and search for the MAC address.
Then there's the delay node because the sensor sends a message every ~2 seconds, so i pass 1 message every 5 minutes.
And finally the function code:
buf = new Buffer(msg.payload.advertisement.serviceData[0].data);
temp = (buf.readUInt8(6) << 8) | buf.readUInt8(7);
temp /= 10;
humi = buf.readUInt8(8);
batt = buf.readUInt8(9);
vbat = (buf.readUInt8(10) << 8) | buf.readUInt8(11);
rssi = msg.payload.rssi;
sig = 2*(rssi+100);
if (sig > 100) sig = 100;
fill = batt > 15 ? "green" : batt > 5 ? "yellow" : "red";
text = "Temp: " + temp + "°C, Hum: " + humi + "%, Bat: " + batt + "%, mV: " + vbat + ", Sig: " + sig + "%";
node.status({fill:fill,shape:"dot",text:text});
return {"payload":{"temperature":temp, "humidity":humi, "battery": batt, "battery_mv": vbat, "rssi": rssi}};
Hope this helps. PS: i know i could have used the function to parse a 16 bit integer directly from the buffer, but i'm lazy and prefer to use bit shifting instead ;-)
Same problem. I managed to read the data without changing the firmware using the following shell command taken from http://www.d0wn.com/using-bash-and-gatttool-to-get-readings-from-xiaomi-mijia-lywsd03mmc-temperature-humidity-sensor/ as I'm very new to node-red, any advice on doing this better is much appreciated.
#!/bin/bash
bt=$(timeout 5 gatttool -b XX:XX:XX:XX:XX:XX --char-write-req --handle='0x0038' --value="0100" --listen)
if [ -z "$bt" ]
then
echo "The reading failed"
else
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 the flow
The first function is only used to parse the JSON string as node-red's JSON convert node gives an error when converting the output of the BLEscanner. Here's the code.
Hi Michele, what node and package are you using as BLE Scan?
Hi Michele, what node and package are you using as BLE Scan?
I'm using node-red-contrib-ble-scan from @sjroe
I'm using node-red-contrib-ble-scan from @sjroe
Thanks a lot! Worked for me)
This is the flow
The first function is only used to parse the JSON string as node-red's JSON convert node gives an error when converting the output of the BLEscanner. Here's the code.
return {"payload":JSON.parse(msg.payload)};
Then there's the switch node used to filter the BLE messages and only pass the ones coming from the sensor. I use the .id value and search for the MAC address.Then there's the delay node because the sensor sends a message every ~2 seconds, so i pass 1 message every 5 minutes.
And finally the function code:
buf = new Buffer(msg.payload.advertisement.serviceData[0].data); temp = buf.readUInt8(6) << 8; temp += buf.readUInt8(7); temp /= 10; humi = buf.readUInt8(8); batt = buf.readUInt8(9); vbat = buf.readUInt8(10) << 8; vbat += buf.readUInt8(11); rssi = msg.payload.rssi; fill = batt > 15 ? "green" : batt > 5 ? "yellow" : "red"; text = "Temp: " + temp + "°C, Hum: " + humi + "%, Bat: " + batt + "%, mV: " + vbat + ", Sig: " + 2*(rssi+100) + "%"; node.status({fill:fill,shape:"dot",text:text}); return {"payload":{"temperature":temp, "humidity":humi, "battery": batt, "battery_mv": vbat, "rssi": rssi}};
Hope this helps. PS: i know i could have used the function to parse a 16 bit integer directly from the buffer, but i'm lazy and prefer to use bit shifting instead ;-)
Hi Michele,
i managed to flash and read the data from the sensors with ur help. Thanks for that!
I only got the problem that the sensors show wierd data.
Somtimes the right data appears for 1 periode of the timer. What type of data send method you choose in the mija custom firmware?
Hi Michele, i managed to flash and read the data from the sensors with ur help. Thanks for that! I only got the problem that the sensors show wierd data.
Somtimes the right data appears for 1 periode of the timer. What type of data send method you choose in the mija custom firmware?
I've chosen the "Custom" type advertisment. Try opening up the payload and see if the service data look something like this:
From what I see int the ATC Thermometer Documentation, The bytes used by temperature and humidity and so on are theese:
My thermometer howevers doesn't send the first few bytes or so and thus in the function node i read the data from byte 6 onwards. Maybe that's the problem you are having. So i suggest to open the payload and see the service data. My code works with:
0-5: MAC Address
6-7: Temperature
8: Humidity
9: Battery percentage
10-11: Battery voltage
So you should see those values in the debug window:
hint: clicking on the HEX value in the debug windows converts it to a decimal number. If the temperature in your room is less than 25.5°C, you should see the full temperature multiplied by 10 directly in byte 7 (here 22.1°C)
If the temperature on the display is less than 25.5°C and you don't see the full number in the 7th byte, there is a problem with the advertisment data. Try putting a picture of the service data
Here is mine:
I set the method to mii. Ill change it to Custom and see.
Here is mine:
From what i see, that isn't a ATC Custom broadcast message. You can see it by the MAC address which is sent backwards from byte 5 to byte 10. Are you sure you flashed the firmware correctly and/or enabled Custom type broadcasting? When the battery is re-inserted, the sensor shows "Atc" and flashed the last 3 bytes of the MAC address in the humidity section, so in your case it will show f3, then 7b and then 0a. By default the firmware also flashes the battery percentage every 5 seconds or so.
Thanks for your help! Ive set the Advertisement now to atc.... Now it works perfekt thank you.
Nice to hear that!
Hello, the code don't work if the temperature goes below 0 degree celsius. The temperature then is a strange number (6552)
Hello, the code don't work if the temperature goes below 0 degree celsius. The temperature then is a strange number (6552)
I've never tried and honestly never thought about that, because in my scenario that never happens. Maybe trying to use a dedicated function to parse a 16 bit integer could help, but i can't test it.
Thank you. Now it works. With this dedicatet function: if (msg.payload.temperature > 2000) msg.payload.temperature = msg.payload.temperature-6553.7 return msg;
Thanks for this, @MicheleSilla810, I've been tearing my hair out trying to get it working...
My sensor too only reported the battery (as 109) and about every second/third attempt reports an error.
I implemented your solution but I am getting a weird temp. value:
temperature: 4520.3
humidity: 56
battery: 193
battery_mv: 42024
rssi: -33
The display itself shows 21.6 C so seems the sensor is working... Any clue?
Hi,
I have a problem recreating this solution. In the serviceData I have an array, which expands everytime when node red receives new message. When the node red service is restarted, it starts from the beginning.
Any help please?
Thank you.
Hi, I have a problem recreating this solution. In the serviceData I have an array, which expands everytime when node red receives new message. When the node red service is restarted, it starts from the beginning. Any help please? Thank you.
hi! i don’t have any clue. i’ve never seen such behavior. maybe it’s the ble node that’s causing some problem?
Thanks for this, @MicheleSilla810, I've been tearing my hair out trying to get it working...
My sensor too only reported the battery (as 109) and about every second/third attempt reports an error.
I implemented your solution but I am getting a weird temp. value:
temperature: 4520.3 humidity: 56 battery: 193 battery_mv: 42024 rssi: -33
The display itself shows 21.6 C so seems the sensor is working... Any clue?
hi! i’m super sorry for the late response, i somehow didn’t see the email. have you got it working?
When i try to retrieve data from the sensor, the first time it fails in about 5 seconds, the "requesting" status on the node has a green dot.
Then, the little bluetooth indicator on the sensor lights up and when trying to get data again, this time the "requesting" pill has a yellow color and it lasts for about 20 seconds, after which the bluetooth indicator on the sensor turns off and the node returns only the battery status, nothing more
Do i need to do something with the sensor? Do I have to connect it in some way to the pi before running the node?
I'm using a Pi 3A+, maybe that's the problem? I also just tried on the Pi 4 B and didn't work.