--- tags: Home Assistant --- # Part 4 - Building your own QR code scanner for Home Assistant ##### `Home Assistant` `zewelor/bt-mqtt-gateway` `QR code scanner` `MQTT Tag Scanner` > [name=Mr. Akashic] [time=Sat, Jun 19, 2021] ## Prerequisites * Pulling the zewelor/bt-mqtt-gateway image and running the container ## Implementation 本篇所有完整檔案[下載](https://github.com/awlchang/HackMD-Part-4-Building-your-own-QR-code-scanner-for-Home-Assistant.git)。正文開始。 1. 下載[Dockerfile](https://github.com/awlchang/HackMD-Part-4-Building-your-own-QR-code-scanner-for-Home-Assistant/blob/main/Dockerfile),來準備建立新的image。 2. 在Dockerfile的同一層目錄下執行<span class="dark_orange">docker build -t [NEW_IMAGE_NAME] .</span>指令,會看到開始執行Dockerfile內的指令,如下畫面: ``` Sending build context to Docker daemon 12.04MB Step 1/4 : FROM zewelor/bt-mqtt-gateway:latest ---> 4d03df950fcb Step 2/4 : RUN /usr/local/bin/python -m pip install --upgrade pip ---> Running in decaa9ac73e6 Requirement already satisfied: pip in /usr/local/lib/python3.8/site-packages (21.0.1) Collecting pip Downloading pip-21.1.3-py3-none-any.whl (1.5 MB) Installing collected packages: pip ......more ``` 3. 執行<span class="dark_orange">docker images</span>指令,會看到剛建立image。 ``` REPOSITORY TAG IMAGE ID CREATED SIZE zewelor2/bt-mqtt-gateway latest c7130bee2890 13 seconds ago 321MB ``` zewelor2/bt-mqtt-gateway就是此篇範例的<span class="dark_orange">NEW_IMAGE_NAME</span>。 4. 新增zewelor2/bt-mqtt-gateway的service到docker-compose.yaml。 :::info :warning: **注意:** 接下來在5~7的步驟中,所設定的檔案要放在跟docker-compose.yaml內設定的路徑一樣,如下圖。 </div> :::  5. 在bt-mqtt-gateway的workers資料夾新增QR code掃描器的python檔。直接下載使用[nteumm.py](https://github.com/awlchang/HackMD-Part-4-Building-your-own-QR-code-scanner-for-Home-Assistant/blob/main/nteumm.py)。 6. 新增以下程式碼到bt-mqtt-gateway的gateway.py檔案 ```Python= import signal from workers.nteumm import NteummWorker # Call the device_ungrab function of the NteummWorker for releasing device if it has been already grabbed def signal_handler(signal, frame): print('Stopping') NteummWorker.device_ungrab(NteummWorker) # dev.ungrab() sys.exit(0) # catch Ctrl + C signal.signal(signal.SIGINT, signal_handler) ``` 7. 新增以下文字到bt-mqtt-gateway的config.yaml檔案。[完整檔案](https://github.com/awlchang/HackMD-Part-4-Building-your-own-QR-code-scanner-for-Home-Assistant/blob/main/config.yaml)。 ```yaml= nteumm: args: autodiscovery_prefix: homeassistant # topic_prefix: nteumm devices: nteumm: name: Nteumm discovery_topic: nteumm/tag_scanned discovery_value_template: "{{ value_json.Nteumm.UID }}" #如果有兩支QR code掃描器,就新增以下文字。 #nteumm2: # name: Nteumm2 # discovery_topic: nteumm2/tag_scanned # discovery_value_template: "{{ value_json.Nteumm2.UID }}" ``` 8. 執行<span class="dark_orange">docker restart bt-mqtt-gateway</span>指令,重新執行bt-mqtt-gateway服務。 9. 開啟Home Assistant網頁,切換頁面Configuration > Tags,掃描QR code後就會自動顯示在Tags的頁面。以上就完成本篇的紀錄實作。  ## Introduction 本篇記錄實作Home Assistant新增來自多個QR code掃描器讀取的QR code。實現的流程順序如下: 1. Raspberry Pi 4上的USB Dongle Bluetooth Receiver收到QR code掃描器讀取的QR code。 2. zewelor/bt-mqtt-gateway服務讀取USB Dongle Bluetooth Receiver收到的QR Code。 3. zewelor/bt-mqtt-gateway服務透過MQTT將第n支掃描器讀取的QR code傳給Home Assistant。 4. Home Assistant透過MQTT收到來自zewelor/bt-mqtt-gateway傳送的QR code並自動新增在網頁上。 ### Component and Supplies * F18W 無線 CMOS 影像式 二維 USB 條碼掃描器 * QR code ([QR Code Generator](https://www.the-qrcode-generator.com/)) ### Software Development Tool * Visual Studio Code ### Runtime Environment * Raspbian * Docker container ## Acknowledgements 1. [zewelor/bt-mqtt-gateway](https://github.com/zewelor/bt-mqtt-gateway) 2. [MQTT Tag Scanner](https://www.home-assistant.io/integrations/tag.mqtt/) <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>
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up