4
4

More than 1 year has passed since last update.

Raspberry Pi Zero WHでSwitchBotカーテン(BLE)を動かした

Posted at

はじめに

Raspberry Piを使ってBLEでSwitchbotカーテンを動かす手順です。よく見るPythonではなく、Node.jsです。
Pythonの環境はZero WH単体では作れなかったので、Node.jsです。

機器

構築

以下の公式の手順を参照しながら構築しました。ほとんどそのまんまです。

OSの準備

OSはRaspiOS(buster)のliteをインストールしました。

「2021-12-02-raspios-buster-armhf-lite.zip」を解凍してddでmicroSDに書き込み、Zero WHのスロットに差し込んで起動しました。wifi設定、パスワード設定、sshd有効化やlocale諸々の設定を済ませておきます。
OSやパッケージのアップデートはしませんでした。

$ node -v
v10.24.0

と、古いままです。

パッケージインストール

piユーザでの作業です。githubの手順と比べてnpmのインストールが必要です。

$ sudo apt install bluetooth bluez libbluetooth-dev libudev-dev
$ sudo apt install npm
$ npm install @abandonware/noble
$ npm install node-switchbot

スクリプト作成

switchbotカーテンが1台の場合は、以下のスクリプトで問題ないと思います。
複数台ある人はdiscoverの条件を変えてください。

open.js
// Load the node-switchbot and get a `Switchbot` constructor object
const Switchbot = require('node-switchbot');
// Create an `Switchbot` object
const switchbot = new Switchbot();

switchbot.discover({ model: 'c', quick: true }).then((device_list) => {
  return device_list[0].open();
}).then(() => {
  console.log('Done.');
  process.exit(0)
}).catch((error) => {
  console.error(error);
  process.exit(0)
});
close.js
// Load the node-switchbot and get a `Switchbot` constructor object
const Switchbot = require('node-switchbot');
// Create an `Switchbot` object
const switchbot = new Switchbot();

switchbot.discover({ model: 'c', quick: true }).then((device_list) => {
  return device_list[0].close();
}).then(() => {
  console.log('Done.');
  process.exit(0)
}).catch((error) => {
  console.error(error);
  process.exit(0)
});

実行

sudo付けて実行するだけです。やや時間がかかります。

$ sudo node open.js
Done.
$ sudo node close.js
Done.

おわりに

Node-REDを使って室内と室外の照度センサーの値から、室内照明の点灯/消灯とカーテンの開閉を連動させて、完全自動管理しています。

4
4
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
4
4