Betree / magicblue

💡 Unofficial Python API to control Magic Blue bulbs over Bluetooth
MIT License
100 stars 23 forks source link

Cannot connect from shell #47

Closed FranciscoGomez90 closed 5 years ago

FranciscoGomez90 commented 5 years ago

Hello, I can use the shell app to connect to my lightbulb by listing the BLE devices and using connect 1 (or whatever idx) and the issueing set_color and the rest of command successfully. However, I cannot use the one liner to connect directly to a device and run a command. The error it get is (I replaced the actual MAC addres for MAC_ADD):

pi@raspberrypi:~ $ sudo magicblueshell -m [MAC_ADD] -c 'set_color red'

Traceback (most recent call last): File "/usr/local/bin/magicblueshell", line 11, in sys.exit(main()) File "/usr/local/lib/python3.5/dist-packages/magicblue/magicblueshell.py", line 335, in main shell.cmd_connect([params.mac_address]) File "/usr/local/lib/python3.5/dist-packages/magicblue/magicblueshell.py", line 173, in cmd_connect magic_blue.connect(self.bluetooth_adapter) File "/usr/local/lib/python3.5/dist-packages/magicblue/magicbluelib.py", line 139, in connect bluetooth_adapter_nr) File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 353, in init self._connect(deviceAddr, addrType, iface) File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 402, in _connect "Failed to connect to peripheral %s, addr type: %s" % (addr, addrType)) bluepy.btle.BTLEException: Failed to connect to peripheral [MAC_ADD], addr type: random

Any clue to make this work properly?? Thank you!

Betree commented 5 years ago

Hello @FranciscoGomez90, this is most certainly linked to your bulb version (some of them use public address type, other use random address type). When listing from the shell, we're able to do some stuff to automatically detect bulb's version.

Try to run with the -b param and try different versions:

pi@raspberrypi:~ $ sudo magicblueshell -m [MAC_ADD] -c 'set_color red' -b 10
FranciscoGomez90 commented 5 years ago

Hey! Thank you for your fast response. I'll try as soon as possible and I will let you know if i succeeded. Thak you again!

FranciscoGomez90 commented 5 years ago

It's me again. If you run the command with the -b param works like a charm (mine is a V9) I also made a simple ambilight project in python. I used pyscreenshot to capture the screen and get the mean of the R,G and B channels which I use in the bulb.set_color(). It works great! A little laggy but great! Thank you!

Betree commented 5 years ago

Nice @FranciscoGomez90! Glad to hear that :slightly_smiling_face: Actually running magicblueshell as a command is always slow cause it has to re-connect to the bulb every time. The best way to fasten it is to keep the connection open by using magicbluelib from python.