Skip to content

Instantly share code, notes, and snippets.

@mugifly
Last active February 10, 2022 23:58
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mugifly/a29f34df7de8960d72245fcb124513c7 to your computer and use it in GitHub Desktop.
Save mugifly/a29f34df7de8960d72245fcb124513c7 to your computer and use it in GitHub Desktop.
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()
@vogler
Copy link

vogler commented Nov 2, 2021

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?

Send 5701037804, where 0x78 = 120s.
https://github.com/OpenWonderLabs/python-host/wiki/Bot-BLE-open-API#0x01-execute-an-action

@vogler
Copy link

vogler commented Nov 2, 2021

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