GreenLightning
  • GreenLightning
  • Chapter 0: What is Green Lightning?
    • Downloads
    • FAQs
    • Benchmarks
  • Chapter 1: Getting Started with GreenLightning
    • What you need
    • Simple REST Server
      • Getting Started
      • Structures and Fields
      • Saying Hello
      • Posting and Responding with JSON
    • Comprehensive Start Guide
  • Chapter 2: Configuration
    • HTTP Configuration
    • MQTT Configuration
    • TLS Certificates
  • Chapter 3: Listeners and Behaviors
    • Default Listeners
    • Blocking Behaviors
  • Chapter 4: Routes
    • Basic Routing
  • Chapter 5: PubSub
    • Publish & Subscribe
  • Chapter 6: Clients
    • HTTP Client
    • MQTT Client
Powered by GitBook
On this page
  • Configurations
  • useMQTT(String host, int port, String clientName, int maxInFlight)
  • cleanSession(boolean b)
  • keepAliveSeconds(int sec)
  • lastWill(String topic, boolean retain, MQTTQoS qos, Writable payload)
  • useTLS(optional: TLSCertificates tlsCertificates)
  • authentication(String user, String pass, optional: TLSCertificates certificates)
  • transmissionQoS(MQTTQoS qos)
  • subscriptionQoS(MQTTQoS qos)
  • transmissionRetain(boolean b)
  • connectionFeedbackTopic(String topic)
  1. Chapter 2: Configuration

MQTT Configuration

PreviousHTTP ConfigurationNextTLS Certificates

Last updated 6 years ago

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 .

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)

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)

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

useTLS(optional: TLSCertificates tlsCertificates)

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");

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

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 level for MQTT, and payload is the payload sent in the last will message.

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

here
MQTT Essentials Part 7
quality of service
here