--- tags: Home Assistant --- # Part 3 - Integrating zewelor/bt-mqtt-gateway and Home Assistant ##### `Internet of Things` `Raspberry Pi 4` `Home Assistant` `zewelor/bt-mqtt-gateway` > [name=Mr. Akashic] [time=Wed, Jun 7, 2021] ## Prerequisite * 請先完成Bluno Beetle LED的程式 ([範例程式 bleLED.ino](https://github.com/awlchang/Bluno-Beetle-BLE-LED)),並將程式寫入Bluno Beetle ([操作連結](https://www.youtube.com/watch?v=KzjWbRozItM))。請使用範例程式完成本篇實作。 * Visual Studio Code使用SSH連線到Raspberry Pi 4。 * 設定Home Assistant與MQTT Server連線,操作步驟如下簡報。 {%slideshare AkashaC1/connect-home-assistant-to-mqtt-server %} ## Implementation 1. 在Visual Studio Code開啟[Part 1](https://hackmd.io/@JGoK5hXkSQuAC32KMw8hHw/rJ_-X24cO)文章中建立的zewelor/bt-mqtt-gateway工作目錄<span class="dark_orange">/opt/bt-mqtt-gateway/</span>,會看到該目錄底下有一個資料夾叫<span class="dark_orange">workers</span>和一個.yaml檔叫<span class="dark_orange">config.yaml</span>。將([程式檔連結](https://github.com/awlchang/Integrating-zewelor-bt-mqtt-gateway-and-Home-Assistant)) blunoled.py放到<span class="dark_orange">workers</span>資料夾底下。(程式碼註解待補) 2. 加入blunoled的資訊到<span class="dark_orange">/opt/bt-mqtt-gateway/</span>路徑下的<span class="dark_orange">config.yaml</span>,內容如下: ```yaml= # 第一次設定包含mqtt的參數,之後再新增物聯網設備只需要新增workers節點下的參數。 mqtt: host: 127.0.0.1 # The IP address of the MQTT server port: 1883 #topic_prefix: hostname # All messages will have that prefix added, remove if you dont need this. client_id: bt-mqtt-gateway # client_id不可以與其他MQTT client重覆 availability_topic: lwt_topic manager: sensor_config: topic: homeassistant retain: true topic_subscription: update_all: topic: homeassistant/status payload: online command_timeout: 35 # Timeout for worker operations. Can be removed if the default of 35 seconds is sufficient. command_retries: 0 # Number of retries for worker commands. Default is 0. Might not be supported for all workers. update_retries: 0 # Number of retries for worker updates. Default is 0. Might not be supported for all workers. workers: blunoled: # 該名稱要跟/workers/目錄下的blunoled.py的檔名一樣。 args: devices: greenled: 50:65:83:6F:54:7F # The Mac address of the Bluno Beetle LED (換成自己的Bluno Beetle設備的mac address) topic_prefix: blunoled/bathroom state_topic_prefix: blunoled/bathroom # MQTT publish topic to Home Assistant topic_subscription: blunoled/+/+/set update_interval: 30 # Update LED state per 30 seconds ``` 完成編輯後儲存。 3. 重新啟動zewelor/bt-mqtt-gateway服務,指令為<span class="dark_orange">sudo docker-compose restart bt-mqtt-gateway</span>。執行結果如下。 ``` Restarting bt-mqtt-gateway ... done ``` 4. 新增blunoled裝置到Home Assistant服務。開啟Home Assistant服務網頁(http://192.168.0.112:8123 (IP位址改成自己樹莓派上的IP位置)),接著遵循下面簡報順序即可完成透過Home Assistant的網頁開關藍牙LED燈。 {%slideshareAkashaC1/part-3-integrating-zewelorbtmqttgateway-and-home-assistant %} 簡報第四頁的輸入內容如下。 ```yaml= switch: # https://www.home-assistant.io/docs/mqtt/discovery/ - platform: mqtt unique_id: green_led_switch name: "Green LED" state_topic: "blunoled/bathroom/greenled" command_topic: "blunoled/000/greenled/set" payload_on: "on" payload_off: "off" state_on: "on" state_off: "off" qos: 0 ``` ## Introduction 在[Part 1](https://hackmd.io/@JGoK5hXkSQuAC32KMw8hHw/rJ_-X24cO)的文章,已經成功在Raspberry Pi 4中將zewelor/bt-mqtt-gateway的服務啟動,接著在本篇實作完成下列功能。 * 將客製化的藍牙(Bluno Beetle BLE)LED燈加入zewelor/bt-mqtt-gateway * 整合zewelor/bt-mqtt-gateway和Home Assistant * 透過Home Assistant開關藍牙LED燈 ![](https://i.imgur.com/rbxxlFT.png) ### Components and Supplies * [Bluno Beetle](https://wiki.dfrobot.com/Bluno_Beetle_SKU_DFR0339) * 1 * Jump Wire * 2 * LED * 1 * Resistor 220 ohm * 1 ### Software Development Tool * Visual Studio Code ## Acknowledgements 1. [zewelor/bt-mqtt-gateway](https://github.com/zewelor/bt-mqtt-gateway) <style> .dark_orange { color: #FF8C00; background:#F6F6F6; border-radius:4px; padding-right:6px; padding-left:6px; } .sub_title { font-size: 25px; } .blockquote { background:#F6F6F6; } </style>