在 Java 中使用 MQTT 向 IBM IoT 中的设备发布命令
Publishing commands to device in IBM IoT using MQTT in Java
我目前正在尝试使用 Java Web 应用程序向 IBM IoT Foundation MQTT Broker 中的特定主题发布命令。我的应用程序已经能够侦听设备事件并对其进行操作,但是向设备发布命令是一个问题。我确定我的设备正在监听正确的命令主题,那么可能是什么问题?更具体地说,这是我调用以发布到主题的命令(来自我的 Java 应用程序):
publish("iot-2/cmd/" + MQTTUtil.getDefaultCmdId() + "/fmt/json", rawJSONCommand, false, 0);
System.out.println("Finished sending command!");
其中"publish"方法定义如下:
public void publish(String topic, String message, boolean retained, int qos) { // check if client is connected
if (isMqttConnected())
{
// create a new MqttMessage from the message string
MqttMessage mqttMsg = new MqttMessage(message.getBytes());
// set retained flag
mqttMsg.setRetained(retained);
// set quality of service
mqttMsg.setQos(qos);
try {
System.out.println("About to send!");
client.publish(topic, mqttMsg);
System.out.println("Finished sending!"); }
catch (MqttPersistenceException e)
{ e.printStackTrace(); }
catch (MqttException e)
{ e.printStackTrace(); } }
else {
System.out.println("Connection lost!"); connectionLost(null);
} }
所有发生的事情就是我输入了方法,我在控制台上打印了代码指定的 "About to send!",然后实际的 'client.publish(topic, mqttMsg)' 调用无限期地阻塞了我的程序。最终,在阻塞了一会儿,我收到以下错误:
org.eclipse.paho.client.mqttv3.internal.ClientState checkForActivity SEVERE: a:2uwqwc:<MY_APP_NAME>: Timed out as no write activity, keepAlive=60,000 lastOutboundActivity=1,452,646,209,624 lastInboundActivity=1,452,646,149,303 time=1,452,646,329,628 lastPing=0
感谢您的帮助!
如果您从应用程序发布,是否指定了设备类型和设备 ID?
myAppClient.publishCommand(deviceType, deviceId, "stop", data);
请参阅文档中有关向连接的设备发布命令的部分。
https://docs.internetofthings.ibmcloud.com/java/java_cli_app.html
我目前正在尝试使用 Java Web 应用程序向 IBM IoT Foundation MQTT Broker 中的特定主题发布命令。我的应用程序已经能够侦听设备事件并对其进行操作,但是向设备发布命令是一个问题。我确定我的设备正在监听正确的命令主题,那么可能是什么问题?更具体地说,这是我调用以发布到主题的命令(来自我的 Java 应用程序):
publish("iot-2/cmd/" + MQTTUtil.getDefaultCmdId() + "/fmt/json", rawJSONCommand, false, 0);
System.out.println("Finished sending command!");
其中"publish"方法定义如下:
public void publish(String topic, String message, boolean retained, int qos) { // check if client is connected
if (isMqttConnected())
{
// create a new MqttMessage from the message string
MqttMessage mqttMsg = new MqttMessage(message.getBytes());
// set retained flag
mqttMsg.setRetained(retained);
// set quality of service
mqttMsg.setQos(qos);
try {
System.out.println("About to send!");
client.publish(topic, mqttMsg);
System.out.println("Finished sending!"); }
catch (MqttPersistenceException e)
{ e.printStackTrace(); }
catch (MqttException e)
{ e.printStackTrace(); } }
else {
System.out.println("Connection lost!"); connectionLost(null);
} }
所有发生的事情就是我输入了方法,我在控制台上打印了代码指定的 "About to send!",然后实际的 'client.publish(topic, mqttMsg)' 调用无限期地阻塞了我的程序。最终,在阻塞了一会儿,我收到以下错误:
org.eclipse.paho.client.mqttv3.internal.ClientState checkForActivity SEVERE: a:2uwqwc:<MY_APP_NAME>: Timed out as no write activity, keepAlive=60,000 lastOutboundActivity=1,452,646,209,624 lastInboundActivity=1,452,646,149,303 time=1,452,646,329,628 lastPing=0
感谢您的帮助!
如果您从应用程序发布,是否指定了设备类型和设备 ID?
myAppClient.publishCommand(deviceType, deviceId, "stop", data);
请参阅文档中有关向连接的设备发布命令的部分。 https://docs.internetofthings.ibmcloud.com/java/java_cli_app.html