hannseman / homebridge-mi-hygrothermograph

Homebridge plugin for the Xiaomi Mi Bluetooth Temperature and Humidity Sensor
MIT License
267 stars 52 forks source link

Can i use this library to read the sensor in my custom app ? #53

Closed muscaiu closed 5 years ago

muscaiu commented 5 years ago

Hello, js dev here. I'm currently using this method (https://zsiti.eu/xiaomi-mijia-hygrothermo-v2-sensor-data-on-raspberry-pi/) to read the sensor for my custom made home app. It works by giving it the mac address, but after a while it crashes and i hate it cose it's python. It's the only working way i could find to read the sensor.

So i've been looking for a nodejs library that could read the sensor and i'm happy to find this one. Happy was the first feeling, but after reading the documentation i'm not so sure anymore because of the integration with the homebridge that seems to be a big thing on it's own and i don't really wanna learn it or use it just for 1 sensor type.

TLDR: So what i'm looking for is a js library that can read the sensor and use the data in my own app.

Can this library do it? Or do you know any other js library that can do this?

Thank you.

hannseman commented 5 years ago

Yes you can just skip using the homebridge part of this code and use the Scanner.

const { Scanner } = require("homebridge-mi-hygrothermograph/lib/scanner");
const scanner = new Scanner();
scanner.on("temperatureChange", function (value, peripheral) {
    console.log("Temperature", value);
});

scanner.on("humidityChange", function (value, peripheral) {
    console.log("Humidity", value);
});

scanner.on("batteryChange", function (value, peripheral) {
    console.log("Battery", value);
});

Good luck!

muscaiu commented 5 years ago

Your example was great for 1 device, but right now i have a total of 4 sensors and it got pretty unstable.

This is is how the server looks right now and sometimes it's crashing and it was not hapening with 1 device.

I want to ask for a suggestionof a better way of reading from multiple devices. Thanks!

const io = require('socket.io')();

const { Scanner } = require("homebridge-mi-hygrothermograph/lib/scanner");
const scanner = new Scanner();

io.set("origins", "*:*");

const bedroomId = '4c65a8daa726'
const kitchenId = '4c65a8dd7c37'
const vladId = '4c65a8dae1b7'
const livingId = '4c65a8dd7fc9'

io.on('connection', (client) => {
  scanner.on("temperatureChange", function (temperature, peripheral) {
    if (peripheral.id === bedroomId) {
      client.emit('temperatureBedroom', temperature)
    } else if (peripheral.id === kitchenId) {
      client.emit('temperatureKitchen', temperature)
    } else if (peripheral.id === vladId) {
      client.emit('temperatureVlad', temperature)
    } else if (peripheral.id === livingId) {
      client.emit('temperatureLiving', temperature)
    }
  });
  scanner.on("humidityChange", function (humidity, peripheral) {
    if (peripheral.id === bedroomId) {
      client.emit('humidityBedroom', humidity)
    } else if (peripheral.id === kitchenId) {
      client.emit('humidityKitchen', humidity)
    } else if (peripheral.id === vladId) {
      client.emit('humidityVlad', humidity)
    } else if (peripheral.id === livingId) {
      client.emit('humidityLiving', humidity)
    }
  });
});

const port = 4001;
io.listen(port);
hannseman commented 5 years ago

Really hard to help you without you providing more information than that it's unstable and crashing. But one thing you could try is to have one Scanner per sensor i.e new Scanner(bedroomId) etc.

muscaiu commented 5 years ago

This is the error before crashing, probbaly won't help with anything:

uncaughtException: Unknown event type: 1037. 5020aa0149377cdda8654c0d040020015202
Error: Unknown event type: 1037. 5020aa0149377cdda8654c0d040020015202
    at Parser.parseEventData

I will try your example.

hannseman commented 5 years ago

This error is actually a known issue: https://github.com/hannseman/homebridge-mi-hygrothermograph/issues/56

Did you upgrade your system or anything like that around the time you started seeing these errors?

Also you could listen for the error-event to not crash the node-process on errors emitted by the scanner:

scanner.on("error", error =>  console.log(error));
muscaiu commented 5 years ago

I did completely reinstalled my RPi3 recently. I am getting similar error to #56 . At first i thought it's because i use multiple sensors, but just tested and it's crashing even with 1 sensor.

Error: Unknown event type: 525. 5020aa018cc97fdda8654c0d0200ae043e26
    at Parser.parseEventData (/home/pi/soft/home-automation-api/node_modules/homebridge-mi-hygrothermograph/lib/parser.js:174:15)
    at Parser.parse (/home/pi/soft/home-automation-api/node_modules/homebridge-mi-hygrothermograph/lib/parser.js:56:23)
    at Scanner.parseServiceData (/home/pi/soft/home-automation-api/node_modules/homebridge-mi-hygrothermograph/lib/scanner.js:161:38)
    at Scanner.onDiscover (/home/pi/soft/home-automation-api/node_modules/homebridge-mi-hygrothermograph/lib/scanner.js:90:25)
    at Noble.emit (events.js:198:13)
    at Noble.onDiscover (/home/pi/soft/home-automation-api/node_modules/@abandonware/noble/lib/noble.js:178:10)
    at NobleBindings.emit (events.js:198:13)
    at NobleBindings.onDiscover (/home/pi/soft/home-automation-api/node_modules/@abandonware/noble/lib/hci-socket/bindings.js:174:10)
    at Gap.emit (events.js:198:13)
    at Gap.onHciLeAdvertisingReport (/home/pi/soft/home-automation-api/node_modules/@abandonware/noble/lib/hci-socket/gap.js:256:10)
Emitted 'error' event at:
    at Scanner.parseServiceData (/home/pi/soft/home-automation-api/node_modules/homebridge-mi-hygrothermograph/lib/scanner.js:163:12)
    at Scanner.onDiscover (/home/pi/soft/home-automation-api/node_modules/homebridge-mi-hygrothermograph/lib/scanner.js:90:25)
    [... lines matching original stack trace ...]
    at Gap.onHciLeAdvertisingReport (/home/pi/soft/home-automation-api/node_modules/@abandonware/noble/lib/hci-socket/gap.js:256:10)
    at Hci.emit (events.js:198:13)
    at Hci.processLeAdvertisingReport (/home/pi/soft/home-automation-api/node_modules/@abandonware/noble/lib/hci-socket/hci.js:651:10)

The on error event doesn't help. It works for a while ..like minutes ..and then i'm seeing the error message and the app crashes.

 bedroomScanner.on("error", error => console.log(error));
muscaiu commented 4 years ago

Hmmm decided to give this another try, and now in version 2.3.2 it's not crashing anymore ...at least not yet! :D

Keep up the good work!

muscaiu commented 4 years ago

I was wrong. It's still throwing the same error. I posted a new issue https://github.com/hannseman/homebridge-mi-hygrothermograph/issues/85