Skip to content

Instantly share code, notes, and snippets.

@takeru
Last active July 25, 2022 05:40
Show Gist options
  • Save takeru/3e14c9119091b2cc2ca88924f143c54c to your computer and use it in GitHub Desktop.
Save takeru/3e14c9119091b2cc2ca88924f143c54c to your computer and use it in GitHub Desktop.
SwitchBotPlugをcurlでオンオフ操作
# 0,10,20,30,40,50 * * * * /home/pi/switchbot_plug_onoff.sh on
# 2,12,22,32,42,52 * * * * /home/pi/switchbot_plug_onoff.sh off
#!/bin/bash
arg1="$1"
export SWITCHBOTDEVICEID=xxxxxxxxxxxx SWITCHBOTTOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if [[ $arg1 = "on" ]]; then
echo "on"
curl -X POST "https://api.switch-bot.com/v1.0/devices/$SWITCHBOTDEVICEID/commands" -H "Authorization: $SWITCHBOTTOKEN" -H "Content-Type: application/json" \
-d "{\"command\": \"turnOn\",\"parameter\": \"default\",\"commandType\": \"command\"}" | jq .
fi
if [[ $arg1 = "off" ]]; then
echo "off"
curl -X POST "https://api.switch-bot.com/v1.0/devices/$SWITCHBOTDEVICEID/commands" -H "Authorization: $SWITCHBOTTOKEN" -H "Content-Type: application/json" \
-d "{\"command\": \"turnOff\",\"parameter\": \"default\",\"commandType\": \"command\"}" | jq .
fi
curl -X GET "https://api.switch-bot.com/v1.0/devices/$SWITCHBOTDEVICEID/status" -H "Authorization: $SWITCHBOTTOKEN" -H "Content-Type: application/json" | jq .
SwitchBotアプリからトークン取得して、
export SWITCHBOTTOKEN=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
デバイスIDを取得
curl -X GET "https://api.switch-bot.com/v1.0/devices" -H "Authorization: $SWITCHBOTTOKEN" -H "Content-Type: application/json" | jq .
export SWITCHBOTDEVICEID=1234567890AB
ステータス on/off
curl -X GET "https://api.switch-bot.com/v1.0/devices/$SWITCHBOTDEVICEID/status" -H "Authorization: $SWITCHBOTTOKEN" -H "Content-Type: application/json" | jq .
オン
curl -X POST "https://api.switch-bot.com/v1.0/devices/$SWITCHBOTDEVICEID/commands" -H "Authorization: $SWITCHBOTTOKEN" -H "Content-Type: application/json" -d "{\"command\": \"turnOn\",\"parameter\": \"default\",\"commandType\": \"command\"}"
オフ
curl -X POST "https://api.switch-bot.com/v1.0/devices/$SWITCHBOTDEVICEID/commands" -H "Authorization: $SWITCHBOTTOKEN" -H "Content-Type: application/json" -d "{\"command\": \"turnOff\",\"parameter\": \"default\",\"commandType\": \"command\"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment