簡體   English   中英

無法在Azure IoT中心Java上創建模擬的設備應用程序

[英]Unable to Create a simulated device app on Azure IoT hub Java

我是Azure IoT中心的新手。 我正在使用Java進行開發,並且首先要學習教程。 我能夠創建設備身份,並且我從雲模塊接收的消息也可以正常執行。 但是,當我嘗試執行設備仿真模塊時,我得到:

錯誤:錯誤{condition = amqp:connection:framing-error,description ='org.apache.qpid.proton.engine.TransportException:連接已終止',info = null}

錯誤:org.apache.qpid.proton.engine.TransportException:連接異常終止

我認為這與AMQPS配置有關,但不確定在這里出什么問題。

有人遇到過這樣的問題嗎?

@ UmerF92,感謝您的耐心配合。 您能否在您的環境中嘗試以下代碼? 該示例遵循了教程( https://azure.microsoft.com/zh-cn/documentation/articles/iot-hub-java-java-getstarted/ ),並且在我這一方面工作正常。

private static String connString = "HostName=<your hub>.azure-devices.net;"
            + "DeviceId=< your device >;"
            + "SharedAccessKey=<your share key >";
    private static IotHubClientProtocol protocol = IotHubClientProtocol.AMQPS;//or HTTPS
    private static boolean stopThread = false;


private static String DEVICEID="<your device id>";

private static class TelemetryDataPoint {
    public String deviceId;
    public double windSpeed;

    public String serialize() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }
}

protected static class EventCallback implements IotHubEventCallback {
    public void execute(IotHubStatusCode statusCode, Object context) {
        System.out.println("IoT Hub responded to message with status " + statusCode.name());

        if (context != null) {
            synchronized (context) {
                context.notify();
            }
        }
    }
}

private static class MessageSender implements Runnable {
    public volatile boolean stopThread = false;

    public void run() {
        try {
            double avgWindSpeed = 10;
            Random rand = new Random();
            DeviceClient client;

            client = new DeviceClient(connString, protocol);
            client.open();

            while (!stopThread) {
                double currentWindSpeed = avgWindSpeed + rand.nextDouble() * 4 - 2;
                TelemetryDataPoint telemetryDataPoint = new TelemetryDataPoint();

                telemetryDataPoint.deviceId = DEVICEID;
                telemetryDataPoint.windSpeed = currentWindSpeed;

                String msgStr = telemetryDataPoint.serialize();
                Message msg = new Message(msgStr);

                System.out.println(msgStr);

                Object lockobj = new Object();
                EventCallback callback = new EventCallback();

                client.sendEventAsync(msg, callback, lockobj);

                synchronized (lockobj) {
                    lockobj.wait();
                }

                Thread.sleep(1000);
            }
            client.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
    MessageSender messageSender = new MessageSender();
    Thread t0 = new Thread(messageSender);

    t0.start();

    System.out.println("Press ENTER to exit...");
    System.in.read();
    messageSender.stopThread = true;
    t0.join();
} 

請嘗試一下。

任何更新或疑慮,請讓我知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM