Share this content:
Phase.2完了!と言った翌日に早速エラー(汗)
今朝3時ごろにこんな感じで止まってました。
SwitchBot 防水温湿度計
(PlantFactry_P2) uribo@raspberrypi:~ $ python3 /home/uribo/RaspberryPi/PlantFactry_P2/getSwichBotData.py
[WARNING] API接続エラー: 429 Client Error: for url: https://api.switch-bot.com/v1.1/devices/CA5F45864706/status
[INFO] ステータスコード異常または取得失敗
SwitchBot Plug Mini & Curtain
(PlantFactry_P2) uribo@raspberrypi:~ $ python3 /home/uribo/RaspberryPi/PlantFactry_P2/sw_devicedata.py
SwitchBot Plug Mini & Curtainのデータ取得でエラーが発生しました: Failed to get device id from SwitchBot Cloud API.
Traceback (most recent call last):
File “/home/uribo/RaspberryPi/PlantFactry_P2/sw_devicedata.py”, line 139, in main
getPlugAndCurtain()
File “/home/uribo/RaspberryPi/PlantFactry_P2/sw_devicedata.py”, line 67, in getPlugAndCurtain
device_data = get_device_id(token)
^^^^^^^^^^^^^^^^^^^^
File “/home/uribo/RaspberryPi/PlantFactry_P2/sw_devicedata.py”, line 28, in get_device_id
raise Exception(“Failed to get device id from SwitchBot Cloud API.”)
Exception: Failed to get device id from SwitchBot Cloud API.
^CTraceback (most recent call last):
File “/home/uribo/RaspberryPi/PlantFactry_P2/sw_devicedata.py”, line 149, in
main()
File “/home/uribo/RaspberryPi/PlantFactry_P2/sw_devicedata.py”, line 146, in main
time.sleep(sampling_interval)
KeyboardInterrupt
いろいろ出てますが、ステータスコード”429”は”Too Many Requests”。
APIへのリクエスト回数が制限(レートリミット)を超えたためブロックされている状態。
SwitchBot APIのドキュメントを確認すると以下の通り。

どうやら、SwitchBot APIには1日1万回の制限があるようです。
現状、防水温湿度計とカーテン、Plug Mini×9にデバイスリスト取得コマンドを合わせて12個のコマンドを1分おきに叩いているので1日に17280回。
概ね15時間程度で発生しているので納得です。
APIの仕様的に1コマンドで複数デバイスの情報を取得することはできないので、当面はサンプリング時間を落として(104秒に1回)取得する方針。
インターネット経由でなくBLEによるローカル通信であればこのエラーは避けられますが、植物工場でデバイスを10m以内に設置する条件をつけるのは少しハードルが高いので、当面は前述の運用で様子を見ます。
高尚な制御をしているわけではないので特に問題はありませんが、デバイスが増える毎にサンプリング時間を落とす必要があることには注意が必要ですね。