Hi,
I'm trying to talk to mosquitto broker on my raspberry pi via java client written on my PC (windows 8.1), I'have already installed libmosquitto1, mosquitto, mosquitto-clients, python-mosquitto & python3-mosquitto.
When I use terminal window for subscribing and publishing using:
and
Code: Select all
mosquitto_pub -d -t hello/world -m "Greetings from Terminal window 2"
everything works fine, but when I use java client on my PC I' can't get it to work, java client is:
Code: Select all
import java.util.*;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.*;
public class mqtt_mosquitto_proba {
public static void main(String[] args) {
System.out.println("pocetak"); // proba jeli radi
// String topic = "marko";
// String content = "Message from MqttPublishSample";
int qos = 0;
String broker = "tcp:192.168.1.47:1883";
String clientId = "JavaSample";
MemoryPersistence persistence = new MemoryPersistence();
Scanner unos = new Scanner(System.in);
while(true){
System.out.println("unesi temu");
String topic = unos.nextLine();
System.out.println("Tema je: " + topic);
System.out.println("Unesi poruku");
String content = unos.nextLine();
System.out.println("Poruka je: " + content);
// unos.close();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
System.out.println("Connecting to broker: "+broker);
sampleClient.connect(connOpts);
System.out.println("Connected");
System.out.println("Publishing message: "+content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
sampleClient.publish(topic, message);
System.out.println("Message published");
sampleClient.disconnect();
System.out.println("Disconnected");
// System.exit(0);
} catch(MqttException me) {
System.out.println("reason "+me.getReasonCode());
System.out.println("msg "+me.getMessage());
System.out.println("loc "+me.getLocalizedMessage());
System.out.println("cause "+me.getCause());
System.out.println("excep "+me);
me.printStackTrace();
}
}
}
}
when I try
Code: Select all
ps -ef | grep mosq && netstat -tln | grep 1883
I get
Code: Select all
107 2216 1 0 12:52 ? 00:05:55 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
pi 2679 2622 0 13:30 pts/1 00:00:00 grep --color=auto mosq
tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN
In my /etc/network/interfaces I have:
Code: Select all
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
when i try to send some message on topic I subscribed on my terminal via putty i get this error in eclipse:
Code: Select all
Exception in thread "main" java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:457)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:273)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:224)
at mqtt_mosquitto_proba.main(mqtt_mosquitto_proba.java:30)