高木のブログ

SwitchBotのボットをAPIで操作する

· 130 words · 1 minutes to read
Tags: SwitchBot API IoT

SwitchBotのボット を買ったので、APIから操作をサクッと試してみた記録

手順 🔗

ドキュメント: SwitchBotAPI/README.md at main · OpenWonderLabs/SwitchBotAPI

APIキーは公式アプリから取得できる。(「SwitchBotのスマート加湿器をAPIで操作する 」に書いた)

デバイスを探す(ボットのdevice IDを取得) 🔗

$ curl -s -X GET -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices | jq '.body.deviceList[] | select(.deviceType == "Bot")'
{
  "deviceId": "EX0DEVICE0ID",
  "deviceName": "ボット",
  "deviceType": "Bot",
  "enableCloudService": true,
  "hubDeviceId": "EX0HUB0DEVICE"
}

たくさんデバイスが出てくるので、deviceTypeで絞り込みをする

enableCloudServiceがfalseになっている場合、アプリからクラウドサービスを有効にしておく

ステータスを確認する 🔗

$ curl -s -X GET -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices/EX0DEVICE0ID/status | jq
{
  "statusCode": 100,
  "body": {
    "deviceId": "EX0DEVICE0ID",
    "deviceType": "Bot",
    "hubDeviceId": "EX0HUB0DEVICE",
    "power": "off"
  },
  "message": "success"
}

スイッチをオン、オフする 🔗

モードは「スイッチ」モードにしている(アプリから設定)

オン 🔗

$ curl -s -X POST -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices/EX0DEVICE0ID/commands -H 'Content-Type: application/json' -d '{"command": "turnOn","parameter": "default","commandType": "command"}' | jq
{
  "statusCode": 100,
  "body": {},
  "message": "success"
}

オフ 🔗

$ curl -s -X POST -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices/EX0DEVICE0ID/commands -H 'Content-Type: application/json' -d '{"command": "turnOff","parameter": "default","commandType": "command"}' | jq
{
  "statusCode": 100,
  "body": {},
  "message": "success"
}

Categories


Tags