Skip to content

Instantly share code, notes, and snippets.



Last active Apr 17, 2020
What would you like to do?
Simplest Switchbot Command

Simplest Switchbot Command


It tested on Raspberry Pi Zero W with Raspbian Stretch.

$ sudo apt-get install python-pip libglib2.0-dev bluez-tools
$ sudo pip install bluepy
$ sudo hciconfig hci0 down
$ sudo btmgmt le on
$ sudo hciconfig hci0 up


$ wget
$ chmod u+x

$ sudo hcitool lescan

$ ./ ff:ff:ff:ff:ff:ff on
$ ./ ff:ff:ff:ff:ff:ff off
$ ./ ff:ff:ff:ff:ff:ff press
#!/usr/bin/env python
# Simplest Switchbot Command
# Thanks to
import sys
import binascii
from bluepy.btle import Peripheral
if len(sys.argv) != 3:
print '[Usage] python %s ff:ff:ff:ff:ff:ff MODE' % sys.argv[0]
print '\nMODE: on, off, press'
print '\nYou can get a MAC address of Switchbot, with using $ sudo hcitool lescan'
mac = sys.argv[1]
mode = sys.argv[2]
print 'Connecting... ' + mac
p = Peripheral(mac, 'random')
hand_service = p.getServiceByUUID('cba20d00-224d-11e6-9fb8-0002a5d5c51b')
hand = hand_service.getCharacteristics('cba20002-224d-11e6-9fb8-0002a5d5c51b')[0]
if mode == 'on':
print 'On'
elif mode == 'off':
print 'Off'
elif mode == 'press':
print 'Press'

This comment has been minimized.

Copy link

@bachoo786 bachoo786 commented Apr 17, 2020

Hey thanks for this.

I want to ask you is it possible to add the press function for some duration? So with the press command but hold it for 2 minutes before it returns to it normal position?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.