Wio Nodeの測定データをAzure IoT Centralで表示する

Azure IoT Centralって、グラフ表示やメール通知がサクッとできて便利ですよね。
このAzure IoT Centralに、Wio Nodeの測定データを送ることができるか試してみました。

コンセプト

いたってシンプル。
タイマートリガーで10分間隔にAzure Functionsを起動して、Wio Nodeから温度・湿度を取得してAzure IoT Centralへメッセージ送信するというもの。

f:id:matsujirushix:20190721214747p:plain

Wio Node

Wio NodeにGrove-BME280を接続、専用アプリでセットアップします。
すると、SeeedのWio LinkサーバーへREST APIで温度・湿度を取得することができます。

www.seeedstudio.com

www.seeedstudio.com

f:id:matsujirushix:20190721215645p:plain

Azure IoT Central

アプリケーションを用意して、デバイステンプレートを追加、温度・湿度に対応する測定を追加します。

項目 フィールド名
温度 temperature
湿度 humidity

f:id:matsujirushix:20190721220019p:plain

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);
        }
    }
}

そして、WioNodeTokenWioNodeTempURIWioNodeHumiURIDeviceOfIoTHubにWio NodeやAzure IoT Central(の中のAzure IoT Hub)の情報を、デバッグ時はlocal.settings.json、実行時はapplication settingsに設定します。

local.settings.jsonの設定例
f:id:matsujirushix:20190721221032p:plain

application settingsの設定例
f:id:matsujirushix:20190721221205p:plain

DeviceOfIoTHubに設定する、Azure IoT Centralの中のAzure IoT Hubのデバイス接続文字列は、dps_cstrコマンドで調べてください。
参考:デバイスからAzure IoT Centralに接続する方法 - matsujirushi’s blog

結果

でた。

f:id:matsujirushix:20190721221644p:plain

費用

1日あたり、0.31円でした。

f:id:matsujirushix:20190721221924p:plain