8
11

More than 3 years have passed since last update.

SwitchBotをWindows 10 から制御する

Last updated at Posted at 2021-04-23

swbo.gif

bluetoothから制御できる指ロボットです。

コマンドを送ると横の切れ込みがボタンを押すためのメカが飛び出しスイッチを押してくれます。

コーヒーメーカー改造するか?色々に悩んだ末指ロボットを採用することにした。
image.png

携帯から動作することを確認して次にPCから動作の確認をする。

Bleakのインストール

pip install bleak

メーカーの資料に詳細は、ありません。

この指ロボットをハッキングします。

1回スマートフォンで試験してください。

image.png
WoHANDの表示されているのがswitchBotです。
選択してStartボタンを押します。
image.png
赤枠の数値をメモしてください。

Source Code

switchBot.py
import asyncio
import argparse
from bleak import *
def scan():
    async def run():
        devices = await BleakScanner.discover()
        for d in devices:
            if "D3:F8:8D" in d.address : # filter switch bot divices
                print(f"python switchbot.py -d {d.address}")
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
def switchBot(address):
    UUID = "cba20002-224d-11e6-9fb8-0002a5d5c51b"
    async def run(address, loop):
        async with BleakClient(address, loop=loop) as client:
            y = await client.read_gatt_char(UUID)
            await client.write_gatt_char(UUID, bytearray(b'\x57\x01\x00'))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(address, loop))
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--scan', dest='scan', required=False, default=False, action='store_true',
                        help="Run Switchbot in scan mode - scan devices to control")
    parser.add_argument('-d', '--device', dest='device', required=False, default=None,
                        help="Specify the address of a device to control")
    opts, args = parser.parse_known_args(sys.argv[1:])
    if opts.scan:
        scan()
    elif opts.device:
        switchBot(opts.device)
if __name__ == '__main__':
    main()

実行

BLE0>python switchBot.py -h
usage: switchBot.py [-h] [-s] [-d DEVICE]

optional arguments:
  -h, --help            show this help message and exit
  -s, --scan            Run Switchbot in scan mode - scan devices to control
  -d DEVICE, --device DEVICE
                        Specify the address of a device to control

アドレスの調査

BLE0>python switchBot.py -s
python switchBot.py -d D3:F8:8D:xx:xx:xx

ゆびロボットの実行

python switchBot.py -d D3:F8:8D:xx:xx:xx

実際のアプリケーション化

image.png

8
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
11