天天看點

Power Apps中展示實時資料案例(物聯網遙測資料)

本文介紹:

在Power Apps中通過Power BI 磁貼展示實時資料(物聯網裝置遙測資料);

基本的示意圖如下:

Power Apps中展示實時資料案例(物聯網遙測資料)

1.物聯網裝置通過SDK将遙測的溫濕度值發送到IoT Hub;

2.使用Azure Functions 的IoT Hub觸發讀取遙測消息,并将遙測消息post到Power BI的流資料集中;

3.在Power BI中建立dashboard,并将流資料集制作成磁貼;

4.在Power Apps中添加Power BI 磁貼并儲存;

5.在手機版Power Apps中檢視實時資料;

本例是一個綜合的案例,關于Power BI 流資料集和IoT的相關資訊,不再進行文字描述,可以看視訊了解詳細步驟。

視訊位址:https://www.51azure.cloud/post/2021/1/6/power-apps-power-bi-iot-real-time-data

本文中Azure Functions的示例代碼:

using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Net;
using System.IO;

namespace Company.Function
{
    public static class IotHubTriggerCSharp1
    {
        private static HttpClient client = new HttpClient();

        [FunctionName("IotHubTriggerCSharp1")]
        public static void Run([IoTHubTrigger("messages/events", Connection = "iotconnstring")]EventData message, ILogger log)
        {

            string msgBody=Encoding.UTF8.GetString(message.Body.Array);

            log.LogInformation($"C# IoT Hub trigger function processed a message: {msgBody}");
             string url="your pbi stream dataset endpoint";

             IoTDeviceMsg msg = JsonConvert.DeserializeObject<IoTDeviceMsg>(msgBody);

             var telemetryDataPoint=new {
                temperature = msg.temperature,
                humidity = msg.humidity,
                deviceid = "device001",
                telemetrydt=DateTime.Now
                };

            var messageString=JsonConvert.SerializeObject(telemetryDataPoint) ;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.Timeout = 8000;//設定請求逾時時間,機關為毫秒
            req.ContentType = "application/json";
            byte[] data = Encoding.UTF8.GetBytes("[" + messageString + "]");
                req.ContentLength = data.Length;

                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                 if (resp.StatusCode == HttpStatusCode.OK)
                {
                     log.LogInformation($"OK: {messageString}");
                }
        }

    }

    public class IoTDeviceMsg
    {
        public decimal temperature{get;set;}
        public decimal humidity{get;set;}

    }
}
           

如果不想使用IoT Hub,可以直接參考之前的文章,通過程式模拟的方式,生成實時資料:

《使用Power BI API 向流資料集推送實時資料并在儀表闆可視化》

聲明:

點選可查閱本站文章目錄 《文章分類目錄》

本站所有内容僅代表個人觀點,如與官文檔沖突,請以官方文檔為準。

可在本頁面下方留言或通過下方聯系方式聯系我:

微信:wxyusz;郵箱:[email protected]

歡迎關注公衆号“雲計算實戰”,接收最新文章推送。

Power Apps中展示實時資料案例(物聯網遙測資料)
Power Apps中展示實時資料案例(物聯網遙測資料)

本作品由Sean Yu 采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協定進行許可。

歡迎轉載、使用、重新釋出,但務必保留文章連結:https://www.51azure.cloud,且不得用于商業目的。

繼續閱讀