MQTT Configuration

GreenLightning has built-in MQTT support for Internet-of-Things projects or other types of applications. See MQTT Client for more information on how to create a MQTT client with GreenLightning. Learn how to create a MQTT Client here.

To enable MQTT support and to create a custom MQTT configuration, use the following code:

MQTTClient.java
public class MQTTClient implements GreenApp {
    private MQTTBridge mqttConfig;
    @Override
	public void declareConfiguration(Builder builder) {
	      mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200);
	}
}

You can add more configurations by appending them to the last configuration:

MQTTClient.java
mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .cleanSession(true); //example of an extra MQTT configuration

Configurations

useMQTT(String host, int port, String clientName, int maxInFlight)

Enables MQTT usage. maxInFlight is an optional argument that lets you specify the maximum number of supported in-flight messages.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200);

cleanSession(boolean b)

Lets broker know if a clean session is required on connection. See MQTT Essentials Part 7 for more information about this flag.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .cleanSession(true);

keepAliveSeconds(int sec)

If no data is sent or received over an open connection for a certain time period then the client will generate a PINGREQ and expect to receive a PINGRESP from the broker. This specifies that interval in seconds.

This message exchange confirms that the connection is open and working.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .keepAliveSeconds(10);

lastWill(String topic, boolean retain, MQTTQoS qos, Writable payload)

When the client disconnects, this will be the last message sent before disconnect is finished. topic is the topic the message should be broadcast to, retain determines if a client that tries to subscribe to the last will topic will receive the last will message or not, qos specifies quality of service level for MQTT, and payload is the payload sent in the last will message.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .lastWill("last/will", false, MQTTQoS.atLeastOnce, w -> {w.writeBoolean(true);});

useTLS(optional: TLSCertificates tlsCertificates)

Enable TLS for MQTT connection. Specify custom certificates if required (otherwise uses default, see here).

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .useTLS();

authentication(String user, String pass, optional: TLSCertificates certificates)

If your broker requires username and password authentication, set it here. You can optionally pass in TLS certificates if broker is using TLS with authentication.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .authentication("jperalta", "password");

transmissionQoS(MQTTQoS qos)

Sets the default quality-of-service for message transmission.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .transmissionQoS(MQTTQoS.atLeastOnce);

subscriptionQoS(MQTTQoS qos)

Sets the default quality-of-service for subscriptions.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .subscriptionQoS(MQTTQoS.atLeastOnce);

transmissionRetain(boolean b)

Lets broker know if transmissions should be retained or not by default.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .transmissionRetain(true);

connectionFeedbackTopic(String topic)

The topic for broadcasting if connection is established or not.

mqttConfig = builder.useMQTT("127.0.0.1", 1883, "MQTTClientTest", 200)
                .connectionFeedbackTopic("lifecycle");

Last updated