Publishing commands to device in IBM IoT using MQTT in Java

0 votes

I am currently trying to publish a command to a specific topic in the IBM IoT Foundation MQTT Broker using a Java web application. My application is already able to listen to device events and act on them, however publishing commands to the device is a problem. I know for sure that my device is listening to the proper topic for commands, so what could be the problem? More specifically, here is the command I call to publish to the topic (from my Java app):

publish("iot-2/cmd/" + MQTTUtil.getDefaultCmdId() + "/fmt/json", rawJSONCommand, false, 0); 
System.out.println("Finished sending command!"); 

Where the "publish" method is defined as follows:

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); 
} }

All that happens is that I enter the method, I get "About to send!" printed on my console as the code specifies, and then the actual 'client.publish(topic, mqttMsg)' call blocks my program indefinitely.. Eventually, after blocking for a while, I get the following error:

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
Jul 27, 2018 in IoT (Internet of Things) by Matt
• 2,270 points
1,519 views

1 answer to this question.

0 votes

If you are publishing from an application, are you specifying the device type and device id?

myAppClient.publishCommand(deviceType, deviceId, "stop", data);

Refer to section in documentation about publishing commands to connected devices.https://docs.internetofthings.ibmcloud.com/java/java_cli_app.html

answered Jul 27, 2018 by anonymous2
• 4,240 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Using FIWARE to start Cygnus as a Service in IoT Solution

Cygnus is supposed to create /var/run/cygnus/ when started. You ...READ MORE

answered Aug 13, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
948 views
0 votes
1 answer

Control LED Blinking Using Java in Azure IoT Hub

Your description of what you did is ...READ MORE

answered Sep 5, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
993 views
0 votes
1 answer
0 votes
0 answers
0 votes
1 answer

Connect IBM Watson IoT using Paho MQTT

You should make these changes to the ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,771 views
0 votes
1 answer

MQTT protocol connection error

Ok so you need two libraries to ...READ MORE

answered Jul 6, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points

reshown Jul 6, 2018 by Vardhan 3,088 views
0 votes
1 answer

Mosquitto 1.4.2 Websocket support

down vote In the dir mosquitto-1.4.X edit the ...READ MORE

answered Jul 9, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,444 views
0 votes
1 answer

IoT request response protocol

Based on your requirement of a light ...READ MORE

answered Jul 13, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,859 views
0 votes
1 answer

Integrate IoT device to Azure using MQTT

Today there isn't an official support for ...READ MORE

answered Sep 24, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
2,432 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP