☁️《我把 BLE 裝置接上雲!HomeKit、MQTT、iOS 三方整合實作》
資訊/科技
奧提斯
更新日:2025年4月22日
在這個萬物聯網的時代,感測器不能只藏在家裡,它應該「上雲」。
這篇我來分享怎麼讓:
- BLE 裝置 → MQTT
- MQTT → Homebridge Plugin → HomeKit
- MQTT → 其他平台(可選)
不但 Siri 能開風扇,連 Grafana 都能畫出你家的濕度趨勢圖,一次滿足自嗨、實用與炫技三種需求!
🎯 為什麼要導入 MQTT?
因為 BLE 本身連線距離短,資料只能傳給手機或單一裝置,但:
- MQTT 可以跨平台同步(手機、平板、網頁都能訂閱)
- 資料可以儲存分析(用 InfluxDB + Grafana 畫圖)
- MQTT 支援 QoS、保留訊息、自動重送,非常適合感測器資料
- Homebridge 有超多 MQTT Plugin 可以無縫串接 HomeKit!
一句話:BLE 感測器 + MQTT = 全平台智慧家庭基底。
🧱 架構圖(簡化版)
[BLE Sensor]
↓
[iOS App / Raspberry Pi Script]
↓ 解析後送 MQTT 訊息
[MQTT Broker (如 Mosquitto)]
↓ ↓
[Homebridge Plugin] [Grafana / Home Assistant / Cloud DB]
↓
[HomeKit]
🔧 我怎麼實作的?
1️⃣ 建 BLE → MQTT 中繼程式
可以用:
- iOS App(使用 CoreBluetooth 掃描並轉發)
- Raspberry Pi + Node.js + noble 套件
- 或 Python 的
bleak套件
我選 Node.js 為例子:
const noble = require("@abandonware/noble");
const mqtt = require("mqtt");
const client = mqtt.connect("mqtt://localhost");
noble.on("discover", (peripheral) => {
const temperature = parseSensor(peripheral.advertisement.manufacturerData);
client.publish("home/temperature", temperature.toString());
});
2️⃣ Homebridge 使用 homebridge-mqttthing 插件接資料
安裝插件後,config 加入:
{
"accessory": "mqttthing",
"type": "temperatureSensor",
"name": "BLE 溫度感測器",
"url": "mqtt://localhost",
"topics": {
"getCurrentTemperature": "home/temperature"
}
}
這樣 Apple 家庭 App 就能顯示從 BLE 裝置廣播來的溫度了 🎉
3️⃣ 想做歷史圖表?加上 fakegato-history 或導資料進 InfluxDB → Grafana!
🧠 Bonus 整合玩法
- 與 Home Assistant 串接:MQTT 是原生支援
- 跨區域同步:遠端監控家中溫濕度
- Web dashboard:自己架前端 UI,感覺像在控制 NASA 控制台 🛰️
⚠️ 實作提醒
- MQTT Broker 建議選擇 Mosquitto(穩又輕)
- BLE 廣播資料要定時解析並確認 checksum,避免誤傳
- 如果 BLE Sensor 不穩,可以用 Raspberry Pi 當固定中繼機
- MQTT topic 記得保持一致格式,例如
home/room1/temperature,方便維護
🚀 結語
BLE 本來就是一種「局部」通訊技術,但透過 MQTT,你把它接到了整個雲端世界,也讓 HomeKit 不只是「本地智慧」,而是跨平台協作的智慧家庭中樞。
iOS App 客製化 上架。
iOS 軟體開發、修改、維護既有App
IT/程式 > iPhone/iPad/Mac應用程式開發
$ 10000
$ 10000
0 / 1000