by OpenWonderLabs

OpenWonderLabs / python-host

The python code running on Raspberry Pi or other Linux based boards to control SwitchBot.

163 Stars 45 Forks Last release: Not found Apache License 2.0 17 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


What is the SwitchBot opensource project?

SwitchBot is a smart IoT robot to mechanically control all your switches and buttons. You can control the bot by your smartphone app (iOS or Android, SwitchLink, or other platform based on our open APIs.

This project aims to provide a super light-weight solution to control your SwitchBot on Raspberry Pi/OpenWRT or any other Linux based board.

The python-host distribution is supported and tested on Python 2.7.

How to Install?

On Raspberry Pi. You will need: - A Raspberry Pi 3 or A Raspberry Pi 2 plugged with a Bluetooth dongle. - A SwitchBot. - An SD Card with a fresh install of Raspbian (tested against the latest build 2017-01-11 Jessie with Pixel).

Installation: - Boot your fresh Pi and open a command prompt. - Install the require library.

sudo apt-get update
sudo apt-get install python-pexpect
sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev 
sudo apt-get install libudev-dev libical-dev libreadline-dev
sudo pip install bluepy
- Clone this repo to the Pi.
git clone
- run the python code.
cd python-host
You can use in two ways:
  1. Scan and control by device name.

    sudo python
    • Follow the instruction, input the device number for SwitchBot you want to control.
  2. Control SwitchBot by MAC address. (MAC address should be retrived in advanced)

    sudo python [mac_addr action_cmd]
    action_cmd :Press, Turn On, Turn Off.
    eg: sudo python  xx:xx:xx:xx:xx:xx Press
  3. Python 3 and new bluetooth stack support

The original
script will work only on Python 2 and it relies on the old Bluez utils (like
) that have been deprecated in the latest Bluez releases.

If you want to use the script on Python 3 or on a Linux distro that no longer ships Bluez with the old tools, use the script instead.

To install the required dependencies on Ubuntu/Debian/Raspbian:

apt-get install python3-pip
pip3 install pybluez
apt-get install libboost-python-dev
apt-get install libboost-thread-dev
pip3 install gattlib

If for some reason the gattlib installation fails:

pip3 download gattlib
tar xvzf ./gattlib-0.20150805.tar.gz
cd gattlib-0.20150805/
sed -ie 's/boost_python-py34/boost_python-py35/'
pip3 install .


python3 --help
for usage tips.

Enjoy :)

Thanks to contributors:


Share your words in our community.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.