Git Product home page Git Product logo

Comments (21)

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024 4

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.

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024 3

This is the flow
image
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.
image
Then there's the delay node because the sensor sends a message every ~2 seconds, so i pass 1 message every 5 minutes.
image
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 ;-)

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024 1

Hi Michele, what node and package are you using as BLE Scan?

I'm using node-red-contrib-ble-scan from @sjroe
image

from node-red-contrib-xiaomi-ble.

patou1981 avatar patou1981 commented on August 11, 2024

For me it's only battery value

from node-red-contrib-xiaomi-ble.

harrysingh1991 avatar harrysingh1991 commented on August 11, 2024

Hi Michele,

if you could post your Node Red function that would be very useful!

from node-red-contrib-xiaomi-ble.

hashcan avatar hashcan commented on August 11, 2024

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

Screenshot_20210307_004353

from node-red-contrib-xiaomi-ble.

Lu-Chok avatar Lu-Chok commented on August 11, 2024

This is the flow
image
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?

from node-red-contrib-xiaomi-ble.

Lu-Chok avatar Lu-Chok commented on August 11, 2024

I'm using node-red-contrib-ble-scan from @sjroe
image

Thanks a lot! Worked for me)

from node-red-contrib-xiaomi-ble.

CyrusThe avatar CyrusThe commented on August 11, 2024

This is the flow
image
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.
image
Then there's the delay node because the sensor sends a message every ~2 seconds, so i pass 1 message every 5 minutes.
image
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.
Unbenannt

Somtimes the right data appears for 1 periode of the timer.
What type of data send method you choose in the mija custom firmware?

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024

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.
Unbenannt

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:
image

From what I see int the ATC Thermometer Documentation, The bytes used by temperature and humidity and so on are theese:
image

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:
image
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)
image

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

from node-red-contrib-xiaomi-ble.

CyrusThe avatar CyrusThe commented on August 11, 2024

Here is mine:

Unbenannt

I set the method to mii. Ill change it to Custom and see.

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024

Here is mine:

Unbenannt

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.

from node-red-contrib-xiaomi-ble.

CyrusThe avatar CyrusThe commented on August 11, 2024

Thanks for your help! Ive set the Advertisement now to atc.... Now it works perfekt thank you.

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024

Nice to hear that!

from node-red-contrib-xiaomi-ble.

powerbua avatar powerbua commented on August 11, 2024

Hello, the code don't work if the temperature goes below 0 degree celsius. The temperature then is a strange number (6552)

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024

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.

from node-red-contrib-xiaomi-ble.

powerbua avatar powerbua commented on August 11, 2024

Thank you. Now it works. With this dedicatet function:
if (msg.payload.temperature > 2000) msg.payload.temperature = msg.payload.temperature-6553.7
return msg;

from node-red-contrib-xiaomi-ble.

QAnders avatar QAnders commented on August 11, 2024

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?

from node-red-contrib-xiaomi-ble.

holda29 avatar holda29 commented on August 11, 2024

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.
image

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024

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.
image

hi!
i don’t have any clue. i’ve never seen such behavior. maybe it’s the ble node that’s causing some problem?

from node-red-contrib-xiaomi-ble.

MicheleSilla810 avatar MicheleSilla810 commented on August 11, 2024

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?

from node-red-contrib-xiaomi-ble.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.