Betree / magicblue

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

magicblueshell crashes after firing the second command on a raspberry pi #4

Closed don41382 closed 8 years ago

don41382 commented 8 years ago

After starting magicblueshell, connecting to my Magic Blue Bulb and firing the second command, magiblueshell hangs for a while and stops without any further output.

Hardware / Software Raspberry PI Model B Revision 2.0 256 MB

pi@raspberrypi:~ $ python --version
Python 3.4.2

pi@raspberrypi:~ $ lsusb
Bus 001 Device 004: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

Execution

pi@raspberrypi:~ $ sudo magicblueshell
Magic Blue interactive shell v0.1
Type "help" to see what you can do
> connect DE:0C:59:91:F3:81
INFO:magicblue.magicblueshell:Connected : True
> set_warm_light 0.1
> set_warm_light 0.5
*** WAITING FOR 5 SECONDS **
pi@raspberrypi:~ $

Do you have any idea why this happens?

Betree commented 8 years ago

You can try to connect using GattTool (How to use manually with Gatttool) to see if you get disconnected after the second command.

If this is the case then there's probably a compatibility problem with your bluetooth device It may also be a problem with bluetooth library (libbluetooth-dev).

I can't investigate further, I'm moving out of the country in a few months and right my bulb is thousand miles away from me... unfortunately bluetooth 4.0 isn't that powerful!

Good luck, keep me in touch if you find anything

don41382 commented 8 years ago

Hey Betree,

Thanks for your fast response. I tried using the GattTool and it works like charm!

gatttool -i hci0 -b 'DE:0C:59:91:F3:81' -t random -I
[DE:0C:59:91:F3:81][LE]> connect
Attempting to connect to DE:0C:59:91:F3:81
Connection successful
[DE:0C:59:91:F3:81][LE]> char-write-req 0x000c 56FFFFFF00f0aa
Characteristic value was written successfully
[DE:0C:59:91:F3:81][LE]> char-write-req 0x000c 56FF000000f0aa 
Characteristic value was written successfully

Do you think it might be the pygattlib? I would appreciate any further help with this! By the way, where you going? Just moved to Australia and exchanged my HUE lights to Magic Blue :-) So, thanks for you project!

Betree commented 8 years ago

Not so far away from you actually, I'm moving to New Caledonia :)

Pygattlib hasn't been updated since I last tested it so I doubt it came from this lib. Maybe a difference in libbluetooth-dev version ? I can't tell which one I use to have installed, it would be nice if someone who has a working installation can provide the info.

Another possibility is that they changed some stuff in the bulb API in the newest versions (i bought mine around September 2015). It can also be a bug in my code but again I can't really test it right now.

So, what can you do ?

b0tting commented 8 years ago

I had the exact same problem on the same platform (Raspbian, RPI-B 256mb). Adding more pauzes between commands seemed to lower the freezing frequency, but my scripts remained unstable. The issue was with the pygattlib and was beyond my technical capabilities. I could not use Benjamin's library and had to fall back to running shell commands, which have not failed even once.

don41382 commented 8 years ago

Thanks @b0tting for the reply! I found a solution. I switched to bluepy python interface (https://github.com/IanHarvey/bluepy). Using this library solved the problem. I could change the current code to use this library. @Betree: Would that be okay for you?

import bluepy.btle bulb_id = 'DE:0C:59:91:F3:81' p = bluepy.btle.Peripheral(bulb_id, bluepy.btle.ADDR_TYPE_RANDOM) p.writeCharacteristic(0x000c,b'\xCC\x23\x33',True) #TURN ON p.writeCharacteristic(0x000c,b'\xCC\x24\x33',True) #TURN OFF

Betree commented 8 years ago

Bluepy looks a lot more maintained than pygattlib, that seems great !

don41382 commented 8 years ago

@Betree alright, I am going to update your code to the bluepy project on the weekend :-)

And I guess, I am going to implement some of your new functionality @b0tting