Skip to content

Instantly share code, notes, and snippets.

@jtuttas
Created April 6, 2018 09:37
Show Gist options
  • Save jtuttas/b31f07f679dde22ad90650f14cf87214 to your computer and use it in GitHub Desktop.
Save jtuttas/b31f07f679dde22ad90650f14cf87214 to your computer and use it in GitHub Desktop.
MQTT mit Java
package mmbbs;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class MQTT {
public static void main(String[] args) {
MqttClient client;
try {
client = new MqttClient("tcp://service.joerg-tuttas.de", "clientId");
client.connect();
System.out.println("Connected");
client.setCallback(new MqttCallback() {
public void connectionLost(Throwable cause) {
System.out.println("Connection Lost");
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("Message arived topic:"+topic+" message:"+message.toString());
}
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("deliveryCompleted");
}
});
client.subscribe("esp32/temp");
while(true) {
}
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@jtuttas
Copy link
Author

jtuttas commented Apr 6, 2018

Zu installieren via Maven im pom.xml

    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>1.0.2</version>
    </dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment