Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Activate switchbot by python with bluepy on Raspberry Pi
# Activate switchbot by python with bluepy on Raspberry Pi
# https://github.com/IanHarvey/bluepy
#
# Switchbot's API is taken from this
# https://github.com/OpenWonderLabs/python-host
import binascii
from bluepy.btle import Peripheral
# find your switchbot address by
# pi@raspberry:~ $ sudo hcitool lescan
# replace "ff:..:ff"
p = Peripheral("ff:ff:ff:ff:ff:ff", "random")
hand_service = p.getServiceByUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b")
hand = hand_service.getCharacteristics("cba20002-224d-11e6-9fb8-0002a5d5c51b")[0]
hand.write(binascii.a2b_hex("570100"))
p.disconnect()
@rasstef
Copy link

rasstef commented Jan 18, 2021

it's not working for me, but why?

  • MagicSwitchbot
  • Raspberry 3 Model B -> has Bluetooth Low Energy (BLE) on board
  • Raspbian Buster -> bluez 5.50
# hciconfig hci0 down
# btmgmt le on
# hciconfig hci0 up
# hcitool lescan
  ...
  34:14:B5:4A:8E:19 MagicSwitchBot

#./switchbot-cmd.py  34:14:B5:4A:8E:19 on
Connecting... 34:14:B5:4A:8E:19
Traceback (most recent call last):
  File "./switchbot-cmd.py", line 20, in <module>
    p = Peripheral(mac, 'random')
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 391, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral 34:14:B5:4A:8E:19, addr type: random

cannot connect to the device no matter what I do. I also tried gatttool, nothing works...
Have you got an idea? Is an older version of the bluetooth stack needed (stretch?)?

@vladeha
Copy link

vladeha commented Mar 7, 2021

same thing...no luck. does anyone have a solution to this problem?

@remko2000
Copy link

remko2000 commented Mar 15, 2021

I follow

@EngineeredEntropy
Copy link

EngineeredEntropy commented Jul 20, 2021

Try entering your mac address with all lowercase letters, fixed this issue for me.

@scargill
Copy link

scargill commented Apr 8, 2022

I can't even get that far on my RPI4 - I just entered the code as above but chaning my MAC number - no mistakes - yet...

pi@mainpi:~:11:46[0]> ./switchbot_bluepi.py
./switchbot_bluepi.py: line 7: import: command not found
from: too many arguments
./switchbot_bluepi.py: line 13: syntax error near unexpected token `('
./switchbot_bluepi.py: line 13: `p = Peripheral("fb:ff:1f:00:4d:aa", "random")'
pi@mainpi:~:11:46[2]>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment