Java 发送云到设备消息Azure IoT

Java 发送云到设备消息Azure IoT,java,azure,esp8266,azure-iot-hub,azure-iot-sdk,Java,Azure,Esp8266,Azure Iot Hub,Azure Iot Sdk,我目前有一个esp8266使用找到的示例代码向Azure发送消息。下面的代码是我尝试使用java库调用arduino上的direct方法。我创建了一个对象,该对象具有与simplesample\u mqtt.c中定义的模型相同的属性。然后我调用MethodResult.invoke(deviceId、methodName、responseTimeout、connectTimeout、device)并传入设备对象、deviceId和我想调用的方法,但我得到了超时异常 BackEndApplicat

我目前有一个esp8266使用找到的示例代码向Azure发送消息。下面的代码是我尝试使用java库调用arduino上的direct方法。我创建了一个对象,该对象具有与
simplesample\u mqtt.c
中定义的模型相同的属性。然后我调用
MethodResult.invoke(deviceId、methodName、responseTimeout、connectTimeout、device)
并传入设备对象、deviceId和我想调用的方法,但我得到了超时异常

BackEndApplication.java

package com.microsoft.docs.iothub.samples;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod;
import com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;

public class BackEndApplication {

    // Connection string for your IoT Hub
    // az iot hub show-connection-string --hub-name {your iot hub name}
    public static final String iotHubConnectionString = "HostName=Something.azure-devices.net;SharedAccessKeyName=Owner;SharedAccessKey=jdjdjdjdjdjdjdjdjdjd";

    // Device to call direct method on.
    public static final String deviceId = "DeviceIdUsedByArduino";

    // Name of direct method and payload.
    public static final String methodName = "TurnFanOn";

    public static final Long responseTimeout = TimeUnit.SECONDS.toSeconds(30);
    public static final Long connectTimeout = TimeUnit.SECONDS.toSeconds(5);

    public static void main(String[] args) {
        try {
            System.out.println("Calling direct method...");
            ContosoAnemometer device = new ContosoAnemometer();
            device.DeviceId = deviceId;
            device.WindSpeed = 1;
            device.Temperature = 1;
            device.Humidity = 1;

            // Create a DeviceMethod instance to call a direct method.
            DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);

            // Call the direct method.
            MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, device);

            if (result == null) {
                throw new IOException("Direct method invoke returns null");
            }

            // Show the acknowledgement from the device.
            System.out.println("Status: " + result.getStatus());
            System.out.println("Response: " + result.getPayload());
        } catch (IotHubException e) {
            System.out.println("IotHubException calling direct method:");
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println("IOException calling direct method:");
            System.out.println(e.getMessage());
        }
        System.out.println("Done!");
    }
}
iot\U配置S.h

#define IOT_CONFIG_CONNECTION_STRING    "HostName=Something.azure-devices.net;DeviceId=DeviceIdUsedByArduino;SharedAccessKey=somthing="
对Arduino的回应

Connected to wifi
Fetching NTP epoch time failed! Waiting 2 seconds to retry.
Fetched NTP epoch time is: 1527899856
IoT Hub SDK for C, version 1.1.29
IoTHubClient accepted the message for delivery
Message Id: 0 Received.
Result Call Back Called! Result is: IOTHUB_CLIENT_CONFIRMATION_OK 
引发异常

Calling direct method...
IotHubException calling direct method:
IoT Hub not found! {"errorCode":404103,"trackingId":"09128374091283749028h-G:5-TimeStamp:06/02/2018 00:39:09","message":"Timed out waiting for device to subscribe.","timestampUtc":"2018-06-02T00:39:09.7655165Z"} 
Done!
关于如何运行c简单设备方法,请参阅本节。 在此示例中,它使用IoTubClient\u LL\u SetDeviceMethodCallback设置回拨到后端的直接方法。你提到的样品只是为了行动

[更新]:

实际上,Azure-iot-sdk-c序列化程序中的WITH_方法和WITH_操作之间存在差异。操作可以通过后端的云到设备消息调用,而方法需要在DeviceMethodCallback中处理

直接方法是同步的,并且在超时后成功或失败(默认值:30秒,可设置为3600秒)。作为方法的结果,设备可能会返回一些消息体,但方法不需要这样做。对于方法调用的顺序或任何并发语义没有保证。直接方法遵循请求-响应模式,用于需要立即确认其结果的通信

如果要使用方法而不是操作,则需要在模型中使用WITH_METHOD宏声明函数,并需要通过IoTubClient_LL_SetDeviceMethodCallback实现设备方法回调处理