From 5309619d658cf186235041a5f98dbca3e3df41fa Mon Sep 17 00:00:00 2001 From: Riccardo Presotto Date: Mon, 26 Apr 2021 15:44:23 +0200 Subject: [PATCH] work in progress mqtt --- src/main/java/MQTT/PubExample.java | 54 --------------------- src/main/java/MQTT/SubExample.java | 75 ------------------------------ 2 files changed, 129 deletions(-) delete mode 100644 src/main/java/MQTT/PubExample.java delete mode 100644 src/main/java/MQTT/SubExample.java diff --git a/src/main/java/MQTT/PubExample.java b/src/main/java/MQTT/PubExample.java deleted file mode 100644 index c798fa6..0000000 --- a/src/main/java/MQTT/PubExample.java +++ /dev/null @@ -1,54 +0,0 @@ -package MQTT; - -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; -import org.eclipse.paho.client.mqttv3.MqttException; -import org.eclipse.paho.client.mqttv3.MqttMessage; - -public class PubExample { - - public static void main(String[] args) { - // Create an Mqtt client - String broker = "tcp://localhost:1883"; - String clientId = "tem_sens_01"; - String topic = "home/sensors/bedroom/temperature"; - int qos = 1; - - { - try { - MqttClient mqttClient = new MqttClient(broker, clientId); - MqttConnectOptions connOpts = new MqttConnectOptions(); - connOpts.setCleanSession(true); // false = the broker stores all subscriptions for the client and all missed messages for the client that subscribed with a Qos level 1 or 2 - // connOpts.setKeepAliveInterval(1000); //longest period of time that the broker and client can endure without sending a message. - // connOpts.setWill(); //If the client disconnects ungracefully, the broker sends the LWT message on behalf of the client. - // Connect the client - System.out.println(clientId + " Connecting Broker" + broker); - mqttClient.connect(connOpts); - System.out.println(clientId + " Connected"); - - // Create a Mqtt message - String randTemp = String.valueOf(18 + (Math.random() * 4)); // create a random temperature between 18 and 22 degrees - MqttMessage message = new MqttMessage(randTemp.getBytes()); - - // Set the QoS on the Message - message.setQos(qos); - System.out.println(clientId + " Publishing message: " + randTemp + " ..."); - mqttClient.publish(topic, message); - System.out.println(clientId + " Message published"); - - // Disconnect the client - mqttClient.disconnect(); - System.out.println("Publisher " + clientId + " disconnected"); - - } 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(); - } - } - - } -} diff --git a/src/main/java/MQTT/SubExample.java b/src/main/java/MQTT/SubExample.java deleted file mode 100644 index 1de2812..0000000 --- a/src/main/java/MQTT/SubExample.java +++ /dev/null @@ -1,75 +0,0 @@ -package MQTT; - -import org.eclipse.paho.client.mqttv3.*; - -import java.sql.Timestamp; -import java.util.Scanner; - -public class SubExample { - public static void main(String[] args) { - // Create an Mqtt client - - - System.out.println("Subscriber initializing..."); - String broker = "tcp://localhost:1883"; - String clientId = "sub_01"; - String topic = "home/sensors/bedroom/temperature"; - int qos = 1; - - try { - // Create an Mqtt client - MqttClient mqttClient = new MqttClient(broker, clientId); - MqttConnectOptions connOpts = new MqttConnectOptions(); - connOpts.setCleanSession(true); - - // Connect the client - System.out.println(clientId + " Connecting to " + broker); - mqttClient.connect(connOpts); - System.out.println(clientId + " Connected"); - - - // Callback - mqttClient.setCallback(new MqttCallback() { - - public void messageArrived(String topic, MqttMessage message) { - // Called when a message arrives from the server that matches any subscription made by the client - String time = new Timestamp(System.currentTimeMillis()).toString(); - String receivedMessage = new String(message.getPayload()); - System.out.println(clientId + " Received a Message!" + - "\n\tTime: " + time + - "\n\tTopic: " + topic + - "\n\tMessage: " + receivedMessage + - "\n\tQoS: " + message.getQos() + "\n"); - System.out.println("\n *** Press return to exit *** \n"); - } - - public void connectionLost(Throwable cause) { - System.out.println(clientId + " Connectionlost! " + cause.getMessage()); - } - - public void deliveryComplete(IMqttDeliveryToken token) { - } - - }); - - // Subscribe client to the topic filter and a QoS level of 1 - System.out.println("Subscribing client "+clientId+" to topic: " + topic); - mqttClient.subscribe(topic, qos); - System.out.println(clientId + " Subscribed"); - - System.out.println("\n *** Press a random key to exit *** \n"); - Scanner command = new Scanner(System.in); - command.nextLine(); - if (mqttClient.isConnected()) - mqttClient.disconnect(); - - } 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(); - } - } -} -- 2.18.1