Wio Nodeの測定データをAzure IoT Centralで表示する
Azure IoT Centralって、グラフ表示やメール通知がサクッとできて便利ですよね。
このAzure IoT Centralに、Wio Nodeの測定データを送ることができるか試してみました。
コンセプト
いたってシンプル。
タイマートリガーで10分間隔にAzure Functionsを起動して、Wio Nodeから温度・湿度を取得してAzure IoT Centralへメッセージ送信するというもの。
Wio Node
Wio NodeにGrove-BME280を接続、専用アプリでセットアップします。
すると、SeeedのWio LinkサーバーへREST APIで温度・湿度を取得することができます。
Azure IoT Central
アプリケーションを用意して、デバイステンプレートを追加、温度・湿度に対応する測定を追加します。
項目 | フィールド名 |
---|---|
温度 | temperature |
湿度 | humidity |
Azure Functions
これを放り込みます。
using Microsoft.Azure.Devices.Client; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using System; using System.Configuration; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace mjcradlefunc { public static class Function1 { [FunctionName("WioNodeToIoTCentral")] public static async Task Run([TimerTrigger("0 */10 * * * *")]TimerInfo myTimer, ILogger log) { var temperatureObject = await GetJObjectFromWioNode(GetEnvValue("WioNodeTempURI"), GetEnvValue("WioNodeToken"), log); double temperature = (double)temperatureObject["temperature"]; log.LogInformation($"temperature is {temperature}"); var humidityObject = await GetJObjectFromWioNode(GetEnvValue("WioNodeHumiURI"), GetEnvValue("WioNodeToken"), log); double humidity = (double)humidityObject["humidity"]; log.LogInformation($"humidity is {humidity}"); var msgObject = new JObject(); msgObject["temperature"] = temperature; msgObject["humidity"] = humidity; var deviceClient = DeviceClient.CreateFromConnectionString(GetEnvValue("DeviceOfIoTHub"), TransportType.Amqp); var eventMessage = new Message(Encoding.UTF8.GetBytes(msgObject.ToString())); await deviceClient.SendEventAsync(eventMessage); } private static async Task<JObject> GetJObjectFromWioNode(string uri, string token, ILogger log) { var httpClient = new HttpClient(); var jsonString = await httpClient.GetStringAsync($"{uri}?access_token={token}"); log.LogInformation($"Got json string is {jsonString}"); return JObject.Parse(jsonString); } private static string GetEnvValue(string name) { return Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); } } }
そして、WioNodeToken
、WioNodeTempURI
、WioNodeHumiURI
、DeviceOfIoTHub
にWio NodeやAzure IoT Central(の中のAzure IoT Hub)の情報を、デバッグ時はlocal.settings.json
、実行時はapplication settingsに設定します。
local.settings.jsonの設定例
application settingsの設定例
DeviceOfIoTHub
に設定する、Azure IoT Centralの中のAzure IoT Hubのデバイス接続文字列は、dps_cstrコマンドで調べてください。
参考:デバイスからAzure IoT Centralに接続する方法 - matsujirushi’s blog
結果
でた。
費用
1日あたり、0.31円でした。