commit 6f6f2a767b20582a339402b3ab32491a15fd24a1 Author: 3013050 <3013050@stud.hs-mannheim.de> Date: Thu Nov 13 16:37:47 2025 +0100 Finished MQTT diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af5ae10 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +paho* +/VAR-MQTT/.idea/ \ No newline at end of file diff --git a/VAR-MQTT/.classpath b/VAR-MQTT/.classpath new file mode 100644 index 0000000..a6291de --- /dev/null +++ b/VAR-MQTT/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/VAR-MQTT/.project b/VAR-MQTT/.project new file mode 100644 index 0000000..5342d72 --- /dev/null +++ b/VAR-MQTT/.project @@ -0,0 +1,17 @@ + + + VAR-MQTT + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/VAR-MQTT/.settings/org.eclipse.jdt.core.prefs b/VAR-MQTT/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..e40c773 --- /dev/null +++ b/VAR-MQTT/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,13 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/VAR-MQTT/VAR-MQTT.iml b/VAR-MQTT/VAR-MQTT.iml new file mode 100644 index 0000000..5de6864 --- /dev/null +++ b/VAR-MQTT/VAR-MQTT.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VAR-MQTT/bin/var/mom/mqtt/log/Conf.class b/VAR-MQTT/bin/var/mom/mqtt/log/Conf.class new file mode 100644 index 0000000..11dc094 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/log/Conf.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/log/Publisher.class b/VAR-MQTT/bin/var/mom/mqtt/log/Publisher.class new file mode 100644 index 0000000..2fbba20 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/log/Publisher.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/log/Subscriber$1.class b/VAR-MQTT/bin/var/mom/mqtt/log/Subscriber$1.class new file mode 100644 index 0000000..d4bf1d8 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/log/Subscriber$1.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/log/Subscriber.class b/VAR-MQTT/bin/var/mom/mqtt/log/Subscriber.class new file mode 100644 index 0000000..9fbdbb8 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/log/Subscriber.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/smarthome/AlarmSubscriber$1.class b/VAR-MQTT/bin/var/mom/mqtt/smarthome/AlarmSubscriber$1.class new file mode 100644 index 0000000..be37ff3 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/smarthome/AlarmSubscriber$1.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/smarthome/AlarmSubscriber.class b/VAR-MQTT/bin/var/mom/mqtt/smarthome/AlarmSubscriber.class new file mode 100644 index 0000000..efc336a Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/smarthome/AlarmSubscriber.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/smarthome/Conf.class b/VAR-MQTT/bin/var/mom/mqtt/smarthome/Conf.class new file mode 100644 index 0000000..d146584 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/smarthome/Conf.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/smarthome/LoggingSubscriber$1.class b/VAR-MQTT/bin/var/mom/mqtt/smarthome/LoggingSubscriber$1.class new file mode 100644 index 0000000..13c7704 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/smarthome/LoggingSubscriber$1.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/smarthome/LoggingSubscriber.class b/VAR-MQTT/bin/var/mom/mqtt/smarthome/LoggingSubscriber.class new file mode 100644 index 0000000..fd00d84 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/smarthome/LoggingSubscriber.class differ diff --git a/VAR-MQTT/bin/var/mom/mqtt/smarthome/Sensor.class b/VAR-MQTT/bin/var/mom/mqtt/smarthome/Sensor.class new file mode 100644 index 0000000..db8adf8 Binary files /dev/null and b/VAR-MQTT/bin/var/mom/mqtt/smarthome/Sensor.class differ diff --git a/VAR-MQTT/lib/org.eclipse.paho.client.mqttv3-1.1.0.jar b/VAR-MQTT/lib/org.eclipse.paho.client.mqttv3-1.1.0.jar new file mode 100644 index 0000000..76b0c9d Binary files /dev/null and b/VAR-MQTT/lib/org.eclipse.paho.client.mqttv3-1.1.0.jar differ diff --git a/VAR-MQTT/src/var/mom/mqtt/log/Conf.java b/VAR-MQTT/src/var/mom/mqtt/log/Conf.java new file mode 100644 index 0000000..6be044e --- /dev/null +++ b/VAR-MQTT/src/var/mom/mqtt/log/Conf.java @@ -0,0 +1,7 @@ +package var.mom.mqtt.log; + +public class Conf { + public static final String TOPIC = "var/mom/mqtt/4711/messages"; + // public static final String BROKER = "tcp://broker.mqttdashboard.com"; + public static final String BROKER = "tcp://localhost:1883"; +} diff --git a/VAR-MQTT/src/var/mom/mqtt/log/Publisher.java b/VAR-MQTT/src/var/mom/mqtt/log/Publisher.java new file mode 100644 index 0000000..d9fcd5f --- /dev/null +++ b/VAR-MQTT/src/var/mom/mqtt/log/Publisher.java @@ -0,0 +1,27 @@ +package var.mom.mqtt.log; + +import java.util.Date; + +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; + +class Publisher { + public static void main(String[] args) { + MqttClient client; + String clientId = MqttClient.generateClientId(); + try { + client = new MqttClient(Conf.BROKER, clientId); + client.connect(); + MqttMessage message = new MqttMessage(); + for (int i = 0; i < 30; i++) { + String m = "[" + clientId + "] message " + i + ": " + (new Date()).toString(); + message.setPayload(m.getBytes()); + client.publish(Conf.TOPIC, message); + } + client.disconnect(); + } catch (MqttException e) { + System.err.println(e.getMessage()); + } + } +} diff --git a/VAR-MQTT/src/var/mom/mqtt/log/Subscriber.java b/VAR-MQTT/src/var/mom/mqtt/log/Subscriber.java new file mode 100644 index 0000000..013fc03 --- /dev/null +++ b/VAR-MQTT/src/var/mom/mqtt/log/Subscriber.java @@ -0,0 +1,45 @@ +package var.mom.mqtt.log; + +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; + +class Subscriber { + public static void main(String[] args) { + MqttClient client = null; + String clientId = MqttClient.generateClientId(); + try { + client = new MqttClient(Conf.BROKER, clientId); + client.setCallback(new MqttCallback() { + + @Override + public void connectionLost(Throwable arg0) { + } + + @Override + public void deliveryComplete(IMqttDeliveryToken arg0) { + } + + @Override + public void messageArrived(String topic, MqttMessage m) throws Exception { + System.out.println("Topic: " + topic + ", Message: " + m.toString()); + } + }); + client.connect(); + client.subscribe(Conf.TOPIC); + while (true) { + Thread.sleep(1000); + } + } catch (MqttException | InterruptedException e) { + System.err.println(e.getMessage()); + } finally { + try { + client.disconnect(); + } catch (MqttException e) { + // unrecoverable + } + } + } +} diff --git a/VAR-MQTT/src/var/mom/mqtt/smarthome/AlarmSubscriber.java b/VAR-MQTT/src/var/mom/mqtt/smarthome/AlarmSubscriber.java new file mode 100644 index 0000000..9d9ee0a --- /dev/null +++ b/VAR-MQTT/src/var/mom/mqtt/smarthome/AlarmSubscriber.java @@ -0,0 +1,60 @@ +package var.mom.mqtt.smarthome; + +import org.eclipse.paho.client.mqttv3.*; +import var.mom.mqtt.log.Conf; + +public class AlarmSubscriber { + private static final String floor = "groundFloor"; + private static final String room = "kitchen"; + private static final String measuredValue = "temperature"; + private static final double threshold = 5; + + + + public static void main(String[] args) { + final String TOPIC = var.mom.mqtt.smarthome.Conf.ROOTTOPIC + "/" + floor + "/" + room + "/" + measuredValue; + MqttClient client = null; + String clientId = MqttClient.generateClientId(); + + try { + client = new MqttClient(var.mom.mqtt.log.Conf.BROKER, clientId); + client.setCallback(new MqttCallback() { + + @Override + public void connectionLost(Throwable arg0) { + } + + @Override + public void deliveryComplete(IMqttDeliveryToken arg0) { + } + + @Override + public void messageArrived(String topic, MqttMessage m) throws Exception { + try { + double observation = Double.valueOf(m.toString()); + if (observation > threshold) { + System.out.printf("ALARM - THRESHOLD ON " + floor + " IN " + room + " PASSED: " + observation + "\n"); + } + } + catch (NumberFormatException e) { + System.err.println("Invalid number format: " + m.toString()); + } + } + }); + + client.connect(); + client.subscribe(TOPIC); + while (true) { + Thread.sleep(1000); + } + } catch (MqttException | InterruptedException e) { + System.err.println(e.getMessage()); + } finally { + try { + client.disconnect(); + } catch (MqttException e) { + // unrecoverable + } + } + } +} diff --git a/VAR-MQTT/src/var/mom/mqtt/smarthome/Conf.java b/VAR-MQTT/src/var/mom/mqtt/smarthome/Conf.java new file mode 100644 index 0000000..9231ece --- /dev/null +++ b/VAR-MQTT/src/var/mom/mqtt/smarthome/Conf.java @@ -0,0 +1,6 @@ +package var.mom.mqtt.smarthome; + +public class Conf { + public static final String BROKER = "tcp://localhost:1883"; + public static final String ROOTTOPIC = "SmartHome4751"; +} diff --git a/VAR-MQTT/src/var/mom/mqtt/smarthome/LoggingSubscriber.java b/VAR-MQTT/src/var/mom/mqtt/smarthome/LoggingSubscriber.java new file mode 100644 index 0000000..101c0b7 --- /dev/null +++ b/VAR-MQTT/src/var/mom/mqtt/smarthome/LoggingSubscriber.java @@ -0,0 +1,43 @@ +package var.mom.mqtt.smarthome; + +import org.eclipse.paho.client.mqttv3.*; +import var.mom.mqtt.log.Conf; + +public class LoggingSubscriber { + public static void main(String[] args) { + final String TOPIC = var.mom.mqtt.smarthome.Conf.ROOTTOPIC + "/#"; + MqttClient client = null; + String clientId = MqttClient.generateClientId(); + try { + client = new MqttClient(var.mom.mqtt.log.Conf.BROKER, clientId); + client.setCallback(new MqttCallback() { + + @Override + public void connectionLost(Throwable arg0) { + } + + @Override + public void deliveryComplete(IMqttDeliveryToken arg0) { + } + + @Override + public void messageArrived(String topic, MqttMessage m) throws Exception { + System.out.println(m.toString()); + } + }); + client.connect(); + client.subscribe(Conf.TOPIC); + while (true) { + Thread.sleep(1000); + } + } catch (MqttException | InterruptedException e) { + System.err.println(e.getMessage()); + } finally { + try { + client.disconnect(); + } catch (MqttException e) { + // unrecoverable + } + } + } +} diff --git a/VAR-MQTT/src/var/mom/mqtt/smarthome/Sensor.java b/VAR-MQTT/src/var/mom/mqtt/smarthome/Sensor.java new file mode 100644 index 0000000..de90341 --- /dev/null +++ b/VAR-MQTT/src/var/mom/mqtt/smarthome/Sensor.java @@ -0,0 +1,49 @@ +package var.mom.mqtt.smarthome; + +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; + +import java.util.Date; + +public class Sensor { + private static final String floor = "groundFloor"; + private static final String room = "bathroom"; + private static final String measuredValue = "temperature"; + private static final double initialValue = 0; + + private static double value = initialValue; + + + public static void main(String[] args) { + final String TOPIC = Conf.ROOTTOPIC + "/" + floor + "/" + room + "/" + measuredValue; + MqttClient client; + String clientId = MqttClient.generateClientId(); + + try { + client = new MqttClient(Conf.BROKER, clientId); + client.connect(); + MqttMessage message = new MqttMessage(); + + while (true) { + if (Math.random() > 0.5) { + value += 0.1; + } + else { + value -= 0.1; + } + + String payload = String.valueOf(value); + message.setPayload(payload.getBytes()); + + System.out.println("Measuring: " + value); + + client.publish(TOPIC, message); + } + +// client.disconnect(); + } catch (MqttException e) { + System.err.println(e.getMessage()); + } + } +}