Skip to content

Instantly share code, notes, and snippets.

@mugifly

mugifly/switchbot-cmd.md

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

switchbot-cmd.py

Simplest Switchbot Command

Installation

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

Usage

$ wget https://gist.githubusercontent.com/mugifly/a29f34df7de8960d72245fcb124513c7/raw/switchbot-cmd.py
$ chmod u+x switchbot-cmd.py

$ sudo hcitool lescan

$ ./switchbot-cmd.py ff:ff:ff:ff:ff:ff on
$ ./switchbot-cmd.py ff:ff:ff:ff:ff:ff off
$ ./switchbot-cmd.py ff:ff:ff:ff:ff:ff press
#!/usr/bin/env python
# Simplest Switchbot Command
# https://gist.github.com/mugifly/77e5ba2a2f1186bb7d494a90d4317695
# Thanks to https://gist.github.com/aerialist/163a5794e95ccd28dc023161324009ed
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'
quit(1)
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'
hand.write(binascii.a2b_hex('570101'))
elif mode == 'off':
print 'Off'
hand.write(binascii.a2b_hex('570102'))
elif mode == 'press':
print 'Press'
hand.write(binascii.a2b_hex('570100'))
p.disconnect()
@bachoo786

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.