Finished MQTT

main
Victor Hans-Georg Waitz 2025-11-13 16:37:47 +01:00
commit 6f6f2a767b
23 changed files with 303 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
paho*
/VAR-MQTT/.idea/

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/org.eclipse.paho.client.mqttv3-1.1.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
VAR-MQTT/.project 100644
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>VAR-MQTT</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -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

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<libelement value="jar://$MODULE_DIR$/lib/org.eclipse.paho.client.mqttv3-1.1.0.jar!/" />
<src_description expected_position="0">
<src_folder value="file://$MODULE_DIR$/src" expected_position="0" />
</src_description>
</component>
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="JavaSE-1.7" jdkType="JavaSDK" />
<orderEntry type="module-library">
<library name="org.eclipse.paho.client.mqttv3-1.1.0.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/org.eclipse.paho.client.mqttv3-1.1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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";
}

View File

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

View File

@ -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
}
}
}
}

View File

@ -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
}
}
}
}

View File

@ -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";
}

View File

@ -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
}
}
}
}

View File

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