IoThubの導入
IoThubの導入は以下を参考
PrimaryIDを用いてIoThubにデータを送信
primary_idをiothubからコピーして、client情報に入力する
from scd30_i2c import SCD30
import time
import pytz
from datetime import datetime
import os
from azure.iot.device import IoTHubDeviceClient, Message
#set scd30 sensor
try:
scd30 = SCD30()
scd30.set_measurement_interval(10)
scd30.start_periodic_measurement()
time.sleep(2)
except TimeoutError as e:
print(e)
os.system('exit')
pass
#iothub_primary_key
CONNECTION_STRING ="<primary_key>"
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING, websockets=True)
while True:
# if scd30 isn't ready, wait 0.2sec
try:
if scd30.get_data_ready():
m = scd30.read_measurement()
if m is not None:
tim = '"timestamp":"'+datetime.now(pytz.timezone('Asia/Tokyo')).strftime('%Y-%m-%d %H:%M:%S.%f')+'"'
temp = '"' + "temp(degree)" + '"' + ":" + '"' + str(round(m[1]-3.5,3)) + '"'
hum = '"' + "humid(%)" + '"' + ":" + '"' + str(round(m[2],3)) + '"'
co2 = '"' + "co2(ppm)" + '"' + ":" + '"' + str(round(m[0],3)) + '"'
mylist = [tim,temp,hum,co2]
mystr = '{' + ','.join(map(str,mylist))+'}'
if str(round(m[1],3))!="nan":
print(mystr)
message = Message(mystr)
client.send_message(message)
time.sleep(1)
else:
print("value is nan")
time.sleep(1)
else:
time.sleep(0.2)
except OSError as e:
print(e)
pass
Azure Cloud Shell で以下を入力し、データがエッジ側からクラウド側に配信されているかどうか確認する
az iot hub monitor-events --hub-name aot-ai
出力
Starting event monitor, use ctrl-c to stop...
{
"event": {
"origin": "Aot-test01",
"module": "",
"interface": "",
"component": "",
"payload": "{\"timestamp\":\"2021-07-06 14:12:16.809562\",\"temp(degree)\":\"28.237\",\"humid(%)\":\"61.229\",\"co2(ppm)\":\"670.836\"}"
}
}
参考
- azure-iot-deviceモジュールの使い方
https://www.tama-negi.com/2020/11/11/azure-iot-raspberry4/
IoTEdgeの導入
IoTEdge(デバイス側)とIoThub(クラウド側)の通信については以下の動画が参考になる
https://www.youtube.com/watch?v=vGgm2pTQVlM
Azureのドキュメントを参考にIoTEdgeをインストール
https://docs.microsoft.com/ja-jp/azure/iot-edge/how-to-install-iot-edge?view=iotedge-2020-11
IoThubの対象のIoTEdgeデバイスのランタイムの状態を確認し、runningであればok
参考
Azure IoTEdgeのインストール方法
※IoTEdge導入したらラズパイをrebootする
https://docs.microsoft.com/ja-jp/azure/iot-edge/how-to-install-iot-edge?view=iotedge-2020-11"sudo apt-get install moby-engine"の際のエラー
E: Sub-process /usr/bin/dpkg returned an error code (1)
https://askubuntu.com/questions/1062171/dpkg-deb-error-paste-subprocess-was-killed-by-signal-broken-pipe
VScodeとの連携し、ACRからイメージを引っ張ってコンテナ配信をする
参考
Azure IoT toolをVScodeに導入
https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-tools
https://docs.microsoft.com/ja-jp/azure/iot-edge/how-to-vs-code-develop-module?view=iotedge-2020-11gitignoreの空ファイル作成
https://stackoverflow.com/questions/68162849/error-enoent-no-such-file-or-directory-in-visual-studio-codeラズパイのカーネル情報等を確認
https://qiita.com/niwasawa/items/a13a5fcefe9fef34c855ACRにログインするには
https://github.com/MicrosoftDocs/azure-docs.ja-jp/blob/master/articles/container-registry/container-registry-authentication.md
https://www.simpletraveler.jp/2020/09/02/microsoftazure-deploy-dockerimage-to-appservice-via-acr/#Azure_Container_Registry_ACR_DockerAzure-cli install
https://docs.microsoft.com/ja-jp/cli/azure/install-azure-cli-macosIoTEdge用環境変数設定
https://qiita.com/Yokogawa_Mita/items/e8a29f9c6682b3156ef2証明章作成
https://docs.microsoft.com/ja-jp/azure/iot-edge/how-to-create-test-certificates?view=iotedge-2020-11dockerがエッジ側でrunningになっているか確かめる&ホストPCでも確認
https://tarovlog.com/2021/01/14/not-work-docker-command/deployment.template.jsonに/dev/i2c-1を記載し、コンテナとデバイス間で疎通する
https://github.com/Azure-Samples/Custom-vision-service-iot-edge-raspberry-pi/blob/master/deployment.template.json