Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9156

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

$
0
0
はじめに Raspberry Piを使ってBLEでSwitchbotカーテンを動かす手順です。よく見るPythonではなく、Node.jsです。 Pythonの環境はZero WH単体では作れなかったので、Node.jsです。 機器 Raspberry Pi Zero WH SwitchBot Curtain (https://www.switchbot.jp/products/switchbot-curtain) 構築 以下の公式の手順を参照しながら構築しました。ほとんどそのまんまです。 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を使って室内と室外の照度センサーの値から、室内照明の点灯/消灯とカーテンの開閉を連動させて、完全自動管理しています。

Viewing all articles
Browse latest Browse all 9156

Trending Articles