[英]Unable to Create a simulated device app on Azure IoT hub Java
I am pretty new to Azure IoT Hub. 我是Azure IoT中心的新手。 I am using Java for its development and as a first step I am following this tutorial.
我正在使用Java进行开发,并且首先要学习本教程。 I was able to create a device identity and my receive messages from cloud module also executes fine.
我能够创建设备身份,并且我从云模块接收的消息也可以正常执行。 But when I try to execute device simulation module I get:
但是,当我尝试执行设备仿真模块时,我得到:
Error: Error{condition=amqp:connection:framing-error, description='org.apache.qpid.proton.engine.TransportException: connection aborted', info=null} 错误:错误{condition = amqp:connection:framing-error,description ='org.apache.qpid.proton.engine.TransportException:连接已终止',info = null}
Error: org.apache.qpid.proton.engine.TransportException: connection aborted 错误:org.apache.qpid.proton.engine.TransportException:连接异常终止
I think it is something related to AMQPS configurations but not really sure what might be going wrong here. 我认为这与AMQPS配置有关,但不确定在这里出什么问题。
Anyone ever faced such a problem ? 有人遇到过这样的问题吗?
@UmerF92, Thanks for your patience. @ UmerF92,感谢您的耐心配合。 Could you please try this code below on your environment?
您能否在您的环境中尝试以下代码? The sample was followed the tutorial( https://azure.microsoft.com/en-us/documentation/articles/iot-hub-java-java-getstarted/ ) and it worked fine on my side.
该示例遵循了教程( 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();
}
Please try it. 请尝试一下。
Any updates or concerns, please let me know. 任何更新或疑虑,请让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.