【 IoT 】建立與部署 Python 版本的 Module 到 Azure IoT Edge 及 Module 與 Hub 間通訊
內容
- 學習目標
 - 前置準備作業
 - 示意圖
 - 使用工具與服務
 - 前置準備作業
 - 實作
 - 清除資料與設定
 
學習目標
- Azure CLI 使用
 - Docker 使用
 - Jupyter 使用
 - 實作範例 
- ﹝Case1﹞ - 部署 
Python Flask API Module到Azure IoT Edge- Module01 Version 0.1
 
 - ﹝Case2﹞ - 
Python Module間透過Message Route溝通- Module01 Version 0.2 與 Module02 Version 0.1
 
 - ﹝Case3﹞ - 
Azure IoT Edge透過Output Route傳送訊息到Azure IoT Hub- Module03 Version 0.1
 
 - ﹝Case4﹞ - 
Azure IoT Edge透過send_message方式傳送訊息到Azure IoT Hub- Module04 Version 0.1
 
 - ﹝Case5﹞ - 
Azure IoT Edge接收Azure IoT Hub傳送的訊息- Module05 Version 0.1
 
 
 - ﹝Case1﹞ - 部署 
 
示意圖

- 說明 
- 開發人員使用雲端的 Azure Ubuntu VM 上所安裝的 Jupyter 來開發 Python Module、使用 Azure CLI 建立 Azure 相關服務、建立 Docker Image、Push Docker Image 到 Azure Container Registry、設定 Azure IoT Hub 的 Edge 以更新地端 Raspberry Pi 的 Module,Azure IoT Edge 更新 Module 後會依 Module 的需求做相對應的動作,如 API 的 CRUD、Module 間的 Message Route、接收 IoT Hub 訊息、傳送訊息到 IoT Hub 等動作。
 
 
使用工具與服務
- Azure IoT Hub
 - Azure IoT Edge
 - Azure Container Registry
 - Azure Ubuntu VM
 - Azure CLI
 - Raspberry Pi 4
 - Python 3
 - Jupyter
 - Git
 - Docker
 
前置準備作業
- Azure 
- 已安裝 
Azure Ubuntu 16.04 VM- Ref 建立 Azure VM
 
 - 已安裝 
Python 3.6- Ref Python 版本管理與虛擬環境
 
 - 已安裝 Jupyter
 
 - 已安裝 
 - Raspberry Pi 4 
- 已安裝 Raspbian Buster
 - 已設定網路連線
 
 
專案初始化
Step 1. 開啟 Jupyter
 
Step 2. 開啟 Terminal
 -  
點選右上角的
New再點選Terminal
 
Step 3. 載入專案
-  
在
Terminal中輸入下方指令git clone https://github.com/ArcherHuang/Azure-IoT-Edge-Sample.git
 
Step 4. 開啟 Jupyter 程式
 -  
回到
Jupyter首頁,再點選Azure-IoT-Edge-Sample資料夾
 -  
點選
Create-Docker-Image-to-Rpi-IoT-Edge-Module-Use-Python.ipynb
 
建立與設定 Raspberry Pi Docker、Azure IoT Hub、Azure IoT Edge、Azure Container Registry
 Step 1. 執行 Jupyter 中的 1.1 進行套件更新
 - 點選此區塊後按上方的 
Run 

Step 2. 執行 Jupyter 中的 1.2 進行 Docker 安裝
 - 點選此區塊後按上方的 
Run 

Step 3. 執行 Jupyter 中的 1.3 進行 Azure CLI 安裝
 - 點選此區塊後按上方的 
Run 

Step 4. 登入 Azure
- 點選此區塊後按上方的 
Run 

- 點選所顯示的連結並輸入所顯示的 
Code進行登入認證 

- 輸入所顯示的 
Code 

- 選擇登入的帳號
 

- 認證完成
 


Step 5. 執行 Jupyter 中的 1.5 進行 azure-cli-iot-ext 安裝
 - 點選此區塊後按上方的 
Run 

Step 6. 執行 Jupyter 中的 1.6 進行相關資訊設定
 - 點選此區塊後按上方的 
Run 

Step 7. 執行 Jupyter 中的 1.7 進行 新建資源群組
 - 點選此區塊後按上方的 
Run 

Step 8. 執行 Jupyter 中的 1.8 進行 IoT Hub 建立
 - 點選此區塊後按上方的 
Run 

Step 9. 執行 Jupyter 中的 1.9 進行 IoT Edge 建立
 - 點選此區塊後按上方的 
Run 

Step 10. 執行 Jupyter 中的 1.10 進行取得 Azure IoT Edge 的 Connection String
 - 點選此區塊後按上方的 
Run 

- 請再依照 安裝 Azure IoT Edge 於 Raspberry Pi 4 進行 
Azure IoT Edge的安裝與Connection String設定 
Step 11. 執行 Jupyter 中的 1.11 進行 Azure Container Registry 建立
 - 點選此區塊後按上方的 
Run 

Step 12. 執行 Jupyter 中的 1.12 進行啟用 Azure Container Registry 的 Admin user
 - 點選此區塊後按上方的 
Run 

Step 13. 執行 Jupyter 中的 1.13 進行取得 Azure Container Registry 的 name 與 password
 - 點選此區塊後按上方的 
Run 

Case1 部署 Python Flask API Module 到 Azure IoT Edge
 Step 1. 執行 Jupyter 中的 2.1 產生 deployment-1.json
 - 點選此區塊後按上方的 
Run 

Step 2. 執行 Jupyter 中的 2.2 進行安裝在 x86 系統中能 Build ARM 的 Images 套件
 - 點選此區塊後按上方的 
Run 

Step 3. 執行 Jupyter 中的 2.3 產生 Module01 的 Docker Image
 - 點選此區塊後按上方的 
Run 

Step 4. 執行 Jupyter 中的 2.4 產生 acr_password.txt
 - 點選此區塊後按上方的 
Run 

Step 5. 執行 Jupyter 中的 2.5 進行 Azure Container Registry Login
 - 點選此區塊後按上方的 
Run 

Step 6. 執行 Jupyter 中的 2.6 進行將 Module01 的 Docker Image 上傳到 Azure Container Registry
 - 點選此區塊後按上方的 
Run 

Step 7. 執行 Jupyter 中的 2.7 設定 Azure IoT Hub 中 Edge 的 module 以進行部署到 Azure IoT Edge 中
 - 點選此區塊後按上方的 
Run 

Step 8. 在 Raspberry Pi 中查看部署狀況
 -  
請在
Raspberry Pi的終端機輸入下方指令 ( Module 部署到 Raspberry Pi 需幾分鐘時間 )sudo iotedge list
 
Step 9. 查看 Raspberry Pi 的 IP
 -  
請在
Raspberry Pi的終端機輸入下方指令hostname -I
 
Step 10. 開啟瀏覽器進行測試
-  
網址輸入
- ㊟ 
Raspberry-Pi-IP請輸入Step 9所取得的IP 
http://Raspberry-Pi-IP:8080 - ㊟ 
 -  
測試結果

 
Case2 Python Module 間透過 Message Route 溝通
 Step 1. 執行 Jupyter 中的 3.1 建立 Module01 的 Docker Image
 - 點選此區塊後按上方的 
Run 

Step 2. 執行 Jupyter 中的 3.2 進行將 Module01 的 Docker Image 上傳到 Azure Container Registry
 - 點選此區塊後按上方的 
Run 

Step 3. 執行 Jupyter 中的 3.3 建立 Module02的 Docker Image
 - 點選此區塊後按上方的 
Run 

Step 4. 執行 Jupyter 中的 3.4 進行將 Module02 的 Docker Image 上傳到 Azure Container Registry
 - 點選此區塊後按上方的 
Run 

Step 5. 執行 Jupyter 中的 3.5 產生 deployment-2.json
 - 點選此區塊後按上方的 
Run 

Step 6. 執行 Jupyter 中的 3.6 進行設定 Azure IoT Hub 中 Edge 的 module 以進行部署到 Azure IoT Edge 中
 - 點選此區塊後按上方的 
Run 

Step 7. 在 Raspberry Pi 中查看部署狀況
 -  
請在
Raspberry Pi的終端機輸入下方指令 ( Module 部署到 Raspberry Pi 需幾分鐘時間 )sudo iotedge list
 
Step 8. 查看 Raspberry Pi 的 IP
 -  
請在
Raspberry Pi的終端機輸入下方指令hostname -I
 
Step 9. 測試部署狀況
-  
瀏覽器端
-  
開啟瀏覽器輸入下方網址
- ㊟ 
Raspberry-Pi-IP請輸入 Step 8 所取得的 IP - http://Raspberry-Pi-IP:8080/message/hello_world
 
 - ㊟ 
 -  
瀏覽器顯示結果
Message - hello_world!
 
 -  
 -  
Raspberry Pi 端
-  
請輸入下方指令
sudo iotedge logs module02 -  
顯示結果

 
 -  
 
Case3 Azure IoT Edge 透過 Output Route 傳送訊息到 Azure IoT Hub
 Step 1. 執行 Jupyter 中的 4.1 進行建立 Module03 的 Docker Image
 - 點選此區塊後按上方的 
Run 

Step 2. 執行 Jupyter 中的 4.2 進行將 Module03 的 Docker Image 上傳到 Azure Container Registry
 - 點選此區塊後按上方的 
Run 

Step 3. 執行 Jupyter 中的 4.3 產生 deployment-3.json
 - 點選此區塊後按上方的 
Run 

Step 4. 執行 Jupyter 中的 4.4 進行設定 Azure IoT Hub 中 Edge 的 module 以進行部署到 Azure IoT Edge 中
 - 點選此區塊後按上方的 
Run 

Step 5. 在 Raspberry Pi 中查看部署狀況
 -  
請在
Raspberry Pi的終端機輸入下方指令 ( Module 部署到 Raspberry Pi 需幾分鐘時間 )sudo iotedge list
 
Step 6. 執行 Jupyter 中的 4.5 從 Azure IoT Hub 查看 Device to Cloud (D2C) 的 訊息
 - 點選此區塊後按上方的 
Run 

Case4 Azure IoT Edge 透過 send_message 方式傳送訊息到  Azure IoT Hub
 Step 1. 執行 Jupyter 中的 5.1 進行取得 Azure IoT Edge 的 Connection String 並存在 .env
 - 點選此區塊後按上方的 
Run 

Step 2. 執行 Jupyter 中的 5.2 進行建立 Module04 的 Docker Image
 - 點選此區塊後按上方的 
Run 

Step 3. 執行 Jupyter 中的 5.3 進行將 Module04 的 Docker Image 上傳到 Azure Container Registry
 - 點選此區塊後按上方的 
Run 

Step 4. 執行 Jupyter 中的 5.4 產生 deployment-4.json
 - 點選此區塊後按上方的 
Run 

Step 5. 執行 Jupyter 中的 5.5 進行設定 Azure IoT Hub 中 Edge 的 module 以進行部署到 Azure IoT Edge 中
 - 點選此區塊後按上方的 
Run 

Step 6. 在 Raspberry Pi 中查看部署狀況
 -  
請在
Raspberry Pi的終端機輸入下方指令 ( Module 部署到 Raspberry Pi 需幾分鐘時間 )sudo iotedge list
 
Step 7. 執行 Jupyter 中的 5.6 進行從 Azure IoT Hub 查看 Device to Cloud (D2C) 的 訊息
 - 點選此區塊後按上方的 
Run 

Case5 Azure IoT Edge 接收 Azure IoT Hub 傳送的訊息
 Step 1. 執行 Jupyter 中的 6.1 進行取得 Azure IoT Edge 的 Connection String 並存檔成 .env
 - 點選此區塊後按上方的 
Run 

Step 2. 執行 Jupyter 中的 6.2 建立 Module05的 Docker Image
 - 點選此區塊後按上方的 
Run 

Step 3. 執行 Jupyter 中的 6.3 進行將 Module05 的 Docker Image 上傳到 Azure Container Registry
 - 點選此區塊後按上方的 
Run 

Step 4. 執行 Jupyter 中的 6.4 產生 deployment-5.json
 - 點選此區塊後按上方的 
Run 

Step 5. 執行 Jupyter 中的 6.5 進行設定 Azure IoT Hub 中 Edge 的 module 以進行部署到 Azure IoT Edge 中
 - 點選此區塊後按上方的 
Run 

Step 6. 在 Raspberry Pi 中查看部署狀況
 -  
請在
Raspberry Pi的終端機輸入下方指令 ( Module 部署到 Raspberry Pi 需幾分鐘時間 )sudo iotedge list
 
Step 7. 執行 Jupyter 中的 6.6 從 Azure IoT Hub 傳送訊息到 Device - Cloud to Device (C2D)
 - 點選此區塊後按上方的 
Run 

Step 8. 測試部署狀況
- Raspberry Pi 端 
-  
請輸入下方指令
sudo iotedge logs module05 -  
顯示結果

 
 -  
 
清除資料與設定
Step 1. 執行 Jupyter 中的 7.1 進行移除 deployment-*.json 與 acr_password.txt
 - 點選此區塊後按上方的 
Run 

Step 2. 執行 Jupyter 中的 7.2 進行移除 Azure IoT Hub & Azure Container Registry & Resource Group
 - 點選此區塊後按上方的 
Run 

Reference
- https://docs.microsoft.com/zh-tw/python/api/azure-iot-device/?view=azure-python
 - https://docs.microsoft.com/zh-tw/azure/iot-edge/module-composition