Skip to content

Latest commit

 

History

History

ssl-mqtt-subscriber

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Overview

This example shows how to subscribe to a MQTT broker over TLS.

The default broker used is: test.mosquitto.org.

Requirements

  • MICROEJ SDK 6.
  • A VEE Port that contains:
    • EDC-1.3 or higher
    • BON-1.4 or higher
    • NET-1.1 or higher
    • SSL-2.2 or higher

This example has been tested on:

Usage

Follow MICROEJ SDK 6 Installation Guide to setup the SDK.

By default, the sample will use the NXP i.MX RT1170 VEE Port 3.0.0. The sample retrieves the VEE Port as a module.

Refer to the Select a VEE Port documentation to use another VEE Port in your project.

Run on Simulator

Run the following command in your IDE (or click the Play button next to the line below when opening this README in IntelliJ IDEA):

./gradlew :ssl-mqtt-subscriber:runOnSimulator

Alternative ways to run in simulation are described in the Run on Simulator documentation.

Run on Device

Complete the Getting Started for NXP i.MX RT1170 Evaluation Kit to make sure your environment is fully setup.

If you are using another VEE Port, make sure to properly setup the VEE Port environment before going further. Refer to the dedicated VEE Port README or Getting Started for more information.

Run the following command in your IDE (or click the Play button next to the line below when opening this README in IntelliJ IDEA):

./gradlew :ssl-mqtt-subscriber:runOnDevice

Alternative ways to run on device are described in the Run on Device documentation.

Expected Behavior

Once the application is connected to the internet, the following traces should be observed in the console:

[subscriber] INFO: Network Lost
[subscriber] INFO: Time updated.
[subscriber] INFO: Network available
[subscriber] INFO: Try to connect to ssl://test.mosquitto.org:8883
[subscriber] INFO: Client connected using SSL
[subscriber] INFO: Client subscribed to microej

Multi-Sandbox Considerations

In a Multi-Sandbox context, the network callback registered at the application startup should be unregistered when the application stops.

Find below an implementation example:

	public static final Logger LOGGER = Logger.getLogger("[Subscriber]");
	private MqttClient client;
	private Thread thread;
	private boolean subscribe;
	private SimpleNetworkCallbackAdapter simpleNetworkCallbackAdapter;

	/**
	 * Stops the publishing and unregister the network state listener.
	 */
	@Override
	public synchronized void stop() {
		subscribe = false;
		disconnect();
		ConnectivityManager connectivityManager = ServiceFactory.getService(ConnectivityManager.class);
		if (connectivityManager != null && simpleNetworkCallbackAdapter != null) {
			connectivityManager.unregisterNetworkCallback(simpleNetworkCallbackAdapter);
		}
		simpleNetworkCallbackAdapter = null;
	}

	private synchronized void disconnect() {
		MqttClient client = this.client;
		this.client = null;
		if (client != null) {
			try {
				client.disconnect();
				LOGGER.info("Client disconnected");
			} catch (MqttException e) {
				LOGGER.fine(e.getMessage());
			}
		}
	}

Dependencies

The dependencies defined in build.gradle.kts are configured in libs.versions.toml.

All dependencies are retrieved transitively by Gradle.

Source

N/A

Restrictions

None.


Markdown
Copyright 2019-2025 MicroEJ Corp. All rights reserved.
Use of this source code is governed by a BSD-style license that can be found with this software.