本文是Power Platform 低代碼開發物聯網App 系列文章的第三講:
《 概覽 -Power Platform低代碼開發物聯網App》
《(2)-Power Apps 連接Azure SQL Database 讀取物聯網設備清單》
《(3)-Power Apps 通過Power BI 磁貼顯示物聯網設備實時數據曲線》
《(4.1)-將Azure IoT Service SDK 集成到 Azure Function併發布 》
《(4.2)-Power Apps 通過Power Automate 發起Http請求調用Azure functions 進行設備遠程控制》
《(5.1)-利用Azure Stream Analytics 將物聯網遙測歷史消息寫入Azure SQL Database 》
《(5.2)-Power Apps 查詢物聯網設備歷史遙測消息》
本文介紹:
在Power Apps中通過Power BI 磁貼展示實時數據(物聯網設備遙測數據);
示意圖如下:
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]
歡迎關注公衆號“雲計算實戰”,接收最新文章推送。
本作品由Sean Yu 採用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。
歡迎轉載、使用、重新發布,但務必保留文章鏈接:https://www.51azure.cloud,且不得用於商業目的。