MQTT Client
GreenLightning fully supports the MQTT protocol. On this page, learn how to build a simple MQTT client. We recommend using a broker such as mosquitto to do your testing with.
Please refer to the GreenLightning-API repo for more examples like this one.

Example

We want to build a very simple app that can do the following:
  • Subscribe to a topic that informs us about the status of an LED somewhere
  • Publish LED status every 5 seconds
  • Listen to the toggle message
Let's get started with our client. First, we need to update GreenApp to look like this:
LEDClient.java
1
package com.ociweb;
2
3
import com.ociweb.gl.api.*;
4
5
public class LEDClient implements GreenApp {
6
private MQTTBridge mqttConfig;
7
8
// Define the topics.
9
private final String toggleLightTopic = "light/toggle";
10
private final String lightStatusTopic = "light/status";
11
12
@Override
13
public void declareConfiguration(Builder config) {
14
// Configure MQTT.
15
mqttConfig = config.useMQTT("127.0.0.1", 1883, "LedController", 200)
16
.cleanSession(true)
17
.transmissionQoS(MQTTQoS.atLeastOnce)
18
.keepAliveSeconds(10);
19
20
// This is not needed, but if you visit the URL given to you in the console when you run this app,
21
// you can see a detailed overview of your entire application in your browser.
22
config.enableTelemetry(8099);
23
24
// Set the pulse for the timer in seconds.
25
config.setTimerPulseRate(5000);
26
}
27
28
@Override
29
public void declareBehavior(GreenRuntime runtime) {
30
// We subscribe to the topic that is sent when someone wants to toggle the light.
31
runtime.bridgeSubscription(toggleLightTopic, toggleLightTopic, mqttConfig).setQoS(MQTTQoS.atLeastOnce);
32
33
// We want to publish the status of the LED every 5 secs.
34
runtime.bridgeTransmission(lightStatusTopic, lightStatusTopic, mqttConfig).setQoS(MQTTQoS.atLeastOnce);
35
36
// Create the light behavior.
37
LightBehavior light = new LightBehavior(runtime, lightStatusTopic);
38
runtime.registerListener("LightBehavior", light)
39
.addSubscription(toggleLightTopic, light::toggleLight)
40
.addSubscription(lightStatusTopic, light::lightStatusUpdate);
41
}
42
43
}
Copied!
Finally, create another file named LightBehavior.java to look like this:
LightBehavior.java
1
package com.ociweb;
2
3
import com.ociweb.gl.api.*;
4
import com.ociweb.pronghorn.pipe.ChannelReader;
5
6
public class LightBehavior implements PubSubMethodListener, TimeListener {
7
// This will be bridged to MQTT inside LEDClient.java
8
private PubSubFixedTopicService cmd;
9
10
private boolean status = false; // current state of LED, false = off, true = on
11
12
public LightBehavior(GreenRuntime runtime, String statusTopic) {
13
// Need PubSub command channel
14
cmd = runtime.newCommandChannel().newPubSubService(statusTopic);
15
}
16
17
public boolean lightStatusUpdate(CharSequence topic, ChannelReader payload) {
18
// We received an update about the status of the light
19
status = payload.readBoolean();
20
System.out.println("Status update! Light on: " + status);
21
22
return true;
23
}
24
25
@Override
26
public void timeEvent(long time, int iteration) {
27
// Every 5 seconds, publish the status of the LED.
28
cmd.publishTopic(writer -> writer.writeBoolean(status));
29
}
30
31
public boolean toggleLight(CharSequence topic, ChannelReader payload) {
32
// Read if light should be on or off from the payload
33
status = payload.readBoolean();
34
35
// TODO: do something here with an LED
36
System.out.println("Toggle request! Status: " + status);
37
38
return true;
39
}
40
}
Copied!
Last modified 3yr ago
Copy link
Contents
Example