- .Net Core 2.1のインストール SDK 2.1.803 64bit版
- Powershellの起動と作業フォルダの作成 スタートボタンを右クリックし、[Windows Powershell(管理者)]をクリックします
- Powershellが起動したら、適当な作業フォルダへ移動(cd C:\work) → 適当な作業フォルダが無ければ作成(md C:\work)してから移動します
- 作業フォルダ内にアプリ用のフォルダを作成(md SimTemp)
- 作成したフォルダに移動(cd .\SimTemp)
- トレーニング用に編集済のソースコードをダウンロードし、作成したアプリ用フォルダに解凍しててください
- 正常にダウンロードできているか確認
- ファイルを確認 ls または dir
- ダウンロードしたパッケージを復元 dotnet restore
- ソースコードをエディタで編集 notepad .\SimulatedDevice.cs
- SimulatedDevice.csの接続文字列を修正する {your device connection string here}を接続したいDeviceのプライマリ接続文字列と置き換えます
- サンプルプログラムの修正:送信データ項目の追加 複数のデバイスを接続する際にどのデバイスから送信されたデータ判別できるようにデバイス固有の名前(id)を追加します
- サンプルプログラムの実行 実際にサンプルプログラムを実行してみます
SDK 2.1.803 32bit版
上記のリンク、または.NET Coreのダウンロードサイトから、
[All .NET Core downloads] → [.NET Core 2.1]を開き、.Net Core 2.1のSDKをダウンロードし、
開発で利用するPCにインストールしてください
※ 必ずRuntimeではなく、開発に必要なSDKを入手してください

実行結果
PS C:\WINDOWS\system32> md \work … 適当なフォルダがない場合は作成する ディレクトリ: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2019/07/03 13:27 work PS C:\WINDOWS\system32> cd \work PS C:\work> md SimTemp ディレクトリ: C:\work Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2019/07/03 13:27 SimTemp PS C:\work> cd .\SimTemp\
編集前のオリジナルのソースコードが必要な場合はGitHubから入手できます
上記のリンク、または下記のサイトを開いてサンプルプログラムをダウンロードします
https://github.com/Azure-Samples/azure-iot-samples-csharp/tree/master/iot-hub/Quickstarts/simulated-device
実行結果
PS C:\work\SimTemp> ls ディレクトリ: C:\work\SimTemp Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2019/07/03 13:29 280 simulated-device.csproj -a---- 2019/07/03 13:29 2752 SimulatedDevice.cs PS C:\work\SimTemp> dotnet restore C:\work\SimTemp\simulated-device.csproj のパッケージを復元しています... Microsoft.Azure.Devices.Client 1.20.3 をインストールしています。 MSBuild ファイル C:\work\SimTemp\obj\simulated-device.csproj.nuget.g.props を生成しています。 MSBuild ファイル C:\work\SimTemp\obj\simulated-device.csproj.nuget.g.targets を生成しています。 C:\work\SimTemp\simulated-device.csproj の復元が 36.86 sec で完了しました。 PS C:\work\SimTemp> notepad .\SimulatedDevice.cs
IoT Hubの共有アクセスポリシーの接続文字列-プライマリキーではないので注してください
※接続文字列内にDeviceID=が含まれていることを確認し、{}は含めずに文字列を""で囲みます
// Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // This application uses the Azure IoT Hub device SDK for .NET // For samples see: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/iothub/device/samples using System; using Microsoft.Azure.Devices.Client; using Newtonsoft.Json; using System.Text; using System.Threading.Tasks; namespace simulated_device { class SimulatedDevice { private static DeviceClient s_deviceClient; // The device connection string to authenticate the device with your IoT hub. // Using the Azure CLI: // az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyDotnetDevice --output table private readonly static string s_connectionString = "{Your device connection string here}"; {Your device connection string here}↑を作成したDeviceの接続文字列と置き換える↓ private readonly static string s_connectionString = "HostName=OEC-Test.azure-devices.net;DeviceId=OecDev01;SharedAccessKey=*******************************************="; // Async method to send simulated telemetry private static async void SendDeviceToCloudMessagesAsync()
"{your name}"を任意の文字列へ変更してください→"Sensor01"など
また、通信エラーなどでリトライを行った場合、送信内容が一意のデータであることを判別するために、
測定時、取得時などの時間情報を付加しておくと後で役立ちます
long unixtime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; // Linux時刻を取得
sendid =name+"-"+unixtime.ToString("000000000000"); // Device名とLinux時刻を連結
サンプルプログラムの修正が終わったら、ファイルを保存して下さい
// Get Unix Time long unixtime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; // Linux 時刻を取得 ←追加する // Create JSON message var telemetryDataPoint = new { name = "{Your Name}", sendid = "{Your Name}"+unixtime.ToString("-000000000000"), ←追加する temperature = currentTemperature, humidity = currentHumidity };
Deviceとの接続文字列が正しく設定されていれば、以下のような画面が表示されると思います
また、デバイスの固有情報(赤字部分)が追加されているか出力画面で確認してください
PS C:\work\SimTemp> dotnet run IoT Hub Quickstarts #1 - Simulated device. Ctrl-C to exit. 2019/07/30 16:26:36 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471595829","temperature":30.712722610082814,"humidity":68.48270042263097} 2019/07/30 16:26:37 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471597451","temperature":21.779690965441844,"humidity":73.946073760253412} 2019/07/30 16:26:38 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471598491","temperature":31.575226118590322,"humidity":67.70453897663603} 2019/07/30 16:26:39 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471599530","temperature":27.051496839640429,"humidity":73.840918230750091} 2019/07/30 16:26:40 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471600570","temperature":34.024024821829059,"humidity":74.0475238831935} 2019/07/30 16:26:41 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471601601","temperature":22.899094365024517,"humidity":69.199914778210186} 2019/07/30 16:26:42 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471602630","temperature":28.789402432176004,"humidity":68.492886325573963} 2019/07/30 16:26:43 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471603661","temperature":26.866686792050807,"humidity":65.214441123052708} 2019/07/30 16:26:44 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471604690","temperature":21.874282922071536,"humidity":63.123421791532742} 2019/07/30 16:26:45 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471605720","temperature":33.91832797504884,"humidity":77.207286459024658} 2019/07/30 16:26:46 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471606761","temperature":30.053892664170775,"humidity":78.883632877321745} 2019/07/30 16:26:47 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471607790","temperature":28.788778343605241,"humidity":69.689796236199228} 2019/07/30 16:26:48 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471608821","temperature":27.845380416999284,"humidity":65.620333936819961} 2019/07/30 16:26:49 > Sending message: {"name":"Sensor01","sendid":"Sensor01-1564471609860","temperature":25.912457840010738,"humidity":72.227780414851281} PS C:\WORK\SimTemp>