4. 打開main.py程序,在目前的Azure IOT Edge demo程序庫裏面沒有用python開發的模擬程序模塊,只有一個filter模塊,常常用來作測試的tempSensor是用.net來寫的,我在這裏寫了一個python版本的設備模擬程序,源代碼能夠從個人Github庫裏下載: html
https://github.com/kingliantop/AzureIOTEdgeSample/tree/master/PythonDeviceSolution python
代碼部分,定義溫度和溼度做爲基本信息: git
# Define the JSON message to send to IoT Hub. github
TEMPERATURE = 20.0 docker
HUMIDITY = 60 json
MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f}" ubuntu
增長證書部分,避免安全認證報錯: windows
def set_certificates(self): 安全
isWindows = sys.platform.lower() in ['windows', 'win32'] 服務器
if not isWindows:
CERT_FILE = os.environ['EdgeModuleCACertificateFile']
print("Adding TrustedCerts from: {0}".format(CERT_FILE))
# this brings in x509 privateKey and certificate
file = open(CERT_FILE)
try:
self.client.set_option("TrustedCerts", file.read())
print ( "set_option TrustedCerts successful" )
except IoTHubClientError as iothub_client_error:
print ( "set_option TrustedCerts failed (%s)" % iothub_client_error )
file.close()
在消息發送層面,你能夠看到消息實際上是發送到了上行隊列,而不是直接發送到IOT Hub,而後消息會由Edge runtime負責發送:
# Send the message.
print( "Steven demo sending message: %s" % message.get_string() )
hub_manager.forward_event_to_output("temperatureOutput", message, 0)
5. 使用VSCode插件構建Edge solution,其實最主要作的就是作docker build和docker push,把打包的鏡像推送到ACR,方便IOT Edge設備部署,選擇deployment.template.json單擊右鍵,選擇Build IOT Edge Solution,該插件會完成相應操做,打包鏡像並推送到容器註冊表:
以此類推,你能夠構建PythonFilter程序,代碼能夠從以下地址下載,或則會本身生成:
https://github.com/kingliantop/AzureIOTEdgeSample/tree/master/PythonFilterSolution
http://www.javashuo.com/article/p-rzuwzoxr-hb.html
sudo apt-get install python-pip
sudo pip install --upgrade pip
sudo pip install -U azure-iot-edge-runtime-ctl
sudo iotedgectl setup --connection-string " HostName=steveniothub.azure-devices.cn;DeviceId=myubuntudevice;SharedAccessKey=Kr5Y7gSGKT7GWQF3tkvdm/3QyhKHrfTmTz81EYzv4bc=" --nopass
sudo iotedgectl start
# 首先獲取你的容器註冊表的用戶名密碼
az acr list -o table
az acr credential show –name stevenacrdemo
或者也能夠在容器註冊表的管理界面,訪問祕鑰部分,啓用管理員用戶,得到用戶名,密碼和登陸服務器:
# 登陸容器註冊表
sudo iotedgectl login --address stevendemoacr.azurecr.cn --username stevendemoacr --password vFjKAE2G2PoDsQWPLfu+M9xwQfnK9l4u
稍等一段時間,能夠看到module部署狀態變成了running: