WebThingsIO / addon-list

List of installable add-ons for WebThings Gateway
Mozilla Public License 2.0
76 stars 54 forks source link

MIPOW PLAYBULB adapter #677

Open KaiRo-at opened 5 years ago

KaiRo-at commented 5 years ago

I saw in mozilla-iot/gateway#165 that it's hard to get generic BLE support working because devices have different APIs behind the BLE connection. Niels Leenheer, creator of html5test, currently does a lot of presentations about WebBluetooth (e.g. the one at DACHFest I saw) and touting that e.g. MiPow Playbulb devices are nice to work with there. They have a nice collection of lights that work via BLE. Niels has demo JS code (working with Chrome, which ships WebBluetooth) at https://github.com/BluetoothRocks/Lightbulb so following that it should not be hard to get an adapter done (and FWIW, he references the Open GATT Registry that has docs for more BLE devices). I have got me some of those devices, so I'd be able to test, and as a Tech Speaker this would be great for doing demos as it works with what the RPi3 has on board by default - and the fewer pieces I need in a demo, the fewer points where things can fail. ;-)

KaiRo-at commented 5 years ago

Note that Niels' code at https://github.com/BluetoothRocks/Lightbulb/blob/master/lib/bluetooth-bulb.js#L8 only cares about reading/writing the color, which would be a great first step to get working, but depending on what model it is (I have a candle, sphere and comet available here), those devices have additional functions available, which would be also nice to support if we can figure out how.

If I wanted to look into getting something working there myself, how would I start?

mrstegeman commented 5 years ago

I'd recommend copying an existing javascript-based adapter and using that as a starting point:

You'll probably want to use noble to do the Bluetooth comms. If you need any help with that, please let us know. I've been working with it a great deal lately.

Here are a couple general resources, as well:

KaiRo-at commented 5 years ago

I started some work based on a fork of the example-adapter and put it into https://github.com/KaiRo-at/playbulb-adapter - it can do device discovery somewhat decently but the device functionality is very much faked still, and that's where I pretty much got stuck. Also, I found out that things very much break down when I try to enable the homekit and this playbulb adapters at the same time as either noble or BLE itself is not really setup to handle things like that.

From what I've seen, we IMHO probably need 2 things going forward:

  1. Better documentation/examples for writing adapters, esp. where the discovery code would go, where we'd set up secure communication (bluetooth pairing or similar), where code for discovering, reading or setting properties and their values would go, etc.
  2. For Bluetooth specifically, a central service that talks to the bluetooth stack of the OS, and which several adapters can use to perform their BLE/GATT communications (over some sort of central API, REST or otherwise).
mrstegeman commented 5 years ago

I think the biggest problem with using noble (or other libraries that use BlueZ) in multiple adapters is the state issue. For example, HomeKit constantly scans in the background to watch for events from devices, but BlueZ (or at least noble) freaks out when you try to communicate with a device while a scan is happening. It's very annoying.

  1. I agree that we need a centralized resource for writing an adapter, rather than pointing people to several different articles. Would you be interested in starting one on our wiki?
  2. Yes, that's probably a good idea, as well. We just did something similar for serial ports, although that one is far less complicated than a Bluetooth one would need to be.
madb1lly commented 4 years ago

Hi @KaiRo-at and @mrstegeman, just a note to say that this sounds very similar to what I'd like to do with Happy Lighting / Triones bulbs: https://discourse.mozilla.org/t/wip-adapter-for-bluetooth-happylighting-triones-bulbs/49477 I've not progressed .... at all (!) yet, but it's still on my wish list. Cheers 🙂