[英]Azure IoT hub cloud to device message using Stream Analytics
I am sending data through the path as below. 我通过以下路径发送数据。
Android -> IoT Hub -> Stream Analytics -> SQL
Android - > IoT Hub - >流分析 - > SQL
I am calling machine learning function at the query of Stream Analytics. 我在Stream Analytics的查询中调用机器学习功能。 Now, I want to return the result of machine learning to the Android device.
现在,我想将机器学习的结果返回给Android设备。 For receiving cloud-to-device message at Android device, I have done and tested with Device Explorer.
为了在Android设备上接收云到设备消息,我已经完成并使用Device Explorer进行了测试。 I am looking at official tutorials, 1 , 2 but still no clue on how to send cloud to device message using the Stream Analytics.
我在看官方教程, 1 , 2 ,但仍然对如何发送云使用流分析设备的消息毫无头绪。 He said using the service bus and function app but did not give the details.
他说使用服务总线和功能应用程序,但没有提供详细信息。 I am new to Azure.
我是Azure的新手。 Hoping someone will give me some guidance or any link so I can understand more on how to implement it.
希望有人会给我一些指导或任何链接,以便我可以更多地了解如何实现它。 Thanks in advance.
提前致谢。
You can use an Azure Function (Preview) to output an ASA job for sending a cloud-to-device message via the Azure IoT Hub service-facing endpoint. 您可以使用Azure功能(预览)输出ASA作业, 以通过Azure IoT Hub面向服务的端点发送云到设备消息 。
The following is an example of this function. 以下是此功能的示例。
run.csx: run.csx:
#r "Newtonsoft.Json"
using System.Configuration;
using System.Text;
using System.Net;
using Microsoft.Azure.Devices;
using Newtonsoft.Json;
// create proxy
static Microsoft.Azure.Devices.ServiceClient client = ServiceClient.CreateFromConnectionString(ConfigurationManager.AppSettings["myIoTHub"]);
public static async Task<HttpResponseMessage> Run(string input, HttpRequestMessage req, TraceWriter log)
{
log.Info($"ASA Job: {input}");
var data = JsonConvert.DeserializeAnonymousType(input, new[] { new { xyz = "", IoTHub = new { ConnectionDeviceId = ""}}});
if(!string.IsNullOrEmpty(data[0]?.IoTHub?.ConnectionDeviceId))
{
string deviceId = data[0].IoTHub.ConnectionDeviceId;
log.Info($"Device: {deviceId}");
// cloud-to-device message
var msg = JsonConvert.SerializeObject(new { temp = 20.5 });
var c2dmsg = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(msg));
// send AMQP message
await client.SendAsync(deviceId, c2dmsg);
}
return req.CreateResponse(HttpStatusCode.NoContent);
}
function.json: function.json:
{
"bindings": [
{
"authLevel": "function",
"name": "input",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
],
"disabled": false
}
project.json: project.json:
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.Devices": "1.3.2"
}
}
}
}
Appendix A 附录A.
For the test purpose, make the following steps: 出于测试目的,请执行以下步骤:
[ { "time": "2017-11-26T12:52:23.4292501Z", "counter": 57, "windSpeed": 8.7358, "temperature": 16.63, "humidity": 79.42, "EventProcessedUtcTime": "2017-11-26T12:52:21.3568252Z", "PartitionId": 2, "EventEnqueuedUtcTime": "2017-11-26T12:52:22.435Z", "IoTHub": { "MessageId": null, "CorrelationId": null, "ConnectionDeviceId": " Device1 ", "ConnectionDeviceGenerationId": "636189812948967054", "EnqueuedTime": "2017-11-26T12:52:21.562Z", "StreamId": null } } ] [{“time”:“2017-11-26T12:52:23.4292501Z”,“counter”:57,“windSpeed”:8.7358,“temperature”:16.63,“湿度”:79.42,“EventProcessedUtcTime”:“2017- 11-26T12:52:21.3568252Z“,”PartitionId“:2,”EventEnqueuedUtcTime“:”2017-11-26T12:52:22.435Z“,”IoTHub“:{”MessageId“:null,”CorrelationId“:null, “ConnectionDeviceId”:“ Device1 ”,“ConnectionDeviceGenerationId”:“636189812948967054”,“EnqueuedTime”:“2017-11-26T12:52:21.562Z”,“StreamId”:null}}]
Change the value Device1 for your actually deviceId. 更改您的实际设备ID值Device1的 。
You should see this function in the combobox when you selected your subscription in the Import option combobox. 当您在导入选项组合框中选择订阅时,您应该在组合框中看到此功能。 Once you done, press the Save button and watch the notification message on the screen.
完成后,按“ 保存”按钮,在屏幕上观看通知消息。 The ASA will send a validation message to your AF and its response status should be 20x code.
ASA将向您的AF发送验证消息,其响应状态应为20x代码。
Note, the inpsim is my iothub input. 注意, inpsim是我的iothub输入。
The ASA outputs payload for HttpTrigger Function in the following format, see my example: ASA以下列格式输出HttpTrigger函数的有效负载,请参阅我的示例:
[
{
"time": "2017-11-26T12:52:23.4292501Z",
"counter": 57,
"windSpeed": 8.7358,
"temperature": 16.63,
"humidity": 79.42,
"EventProcessedUtcTime": "2017-11-26T12:52:21.3568252Z",
"PartitionId": 2,
"EventEnqueuedUtcTime": "2017-11-26T12:52:22.435Z",
"IoTHub": {
"MessageId": null,
"CorrelationId": null,
"ConnectionDeviceId": "Device1",
"ConnectionDeviceGenerationId": "636189812948967054",
"EnqueuedTime": "2017-11-26T12:52:21.562Z",
"StreamId": null
}
}
]
Note, that my telemetry data (*) are counter , temperature , humidity and timestamp time , so the other properties are created implicitly by ASA job. 请注意,我的遥测数据(*)是计数器 , 温度 , 湿度和时间戳时间 ,因此其他属性由ASA作业隐式创建。 Based on the query, you can create any business properties for C2D message.
根据查询,您可以为C2D消息创建任何业务属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.