0
1

More than 1 year has passed since last update.

[備忘録]IoTEdgeを導入し、IoThubからコンテナ配信できるようにする

Posted at

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\"}"
    }
}

参考

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
image.png

参考

VScodeとの連携し、ACRからイメージを引っ張ってコンテナ配信をする

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1