//
you're reading...
How-to's

Publishing data to MQTT

nodered

The MQTT message broker is very popular for publishing sensor data for Internet of Things (IoT) devices.

This post shows you how to publish EV3 sensor data to it.

The image above shows a flow that subscribes to the sensor data topic and prints it out using the very popular node-red visual web tool.

To publish the data, I use a filter called MQTTFilter:

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import lejos.robotics.SampleProvider;
import lejos.robotics.filter.AbstractFilter;

public class MQTTFilter extends AbstractFilter {
    private String topic;
    private int qos;
    private MemoryPersistence persistence = new MemoryPersistence();
    MqttClient client;

	public MQTTFilter(SampleProvider source, String topic, String server, String clientId, int qos) throws MqttException {
		super(source);
		this.topic = topic;
		this.qos = qos;
		client = new MqttClient(server, clientId, persistence);
		MqttConnectOptions connOpts = new MqttConnectOptions();
		connOpts.setCleanSession(true);
        connOpts.setCleanSession(true);
        System.out.println("Connecting to server: " + server);
        client.connect(connOpts);
        System.out.println("Connected");
	}
	
	public void fetchSample(float sample[], int off) {
		super.fetchSample(sample, off);
		StringBuffer b = new StringBuffer();
		
		for(int i=0;i<sampleSize();i++) {
			b.append(sample[i]);
			if (i != sampleSize()-1) b.append(" ");
		}
		
	    try {
	        MqttMessage message = new MqttMessage(b.toString().getBytes());
	        message.setQos(qos);
	        client.publish(topic, message);
	        System.out.println("Message published: " + message.toString());
	    } catch(MqttException e) {
	        System.err.println("MQTT exception: " + e);
	        System.exit(1);
	    }
	}
}

You can then publish any data that you receive fromm a sensor, e.g. by:

import lejos.hardware.Button;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3IRSensor;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class EV3MQTTTest {
	public static void main(String[] args) throws Exception {
		EV3IRSensor ir = new EV3IRSensor(SensorPort.S2);
		SampleProvider sp = ir.getDistanceMode();
		MQTTFilter mqtt = new MQTTFilter(sp, "/ev3/test", "tcp://192.168.0.101:1883", "ev3", 0); 
		float[] sample = new float[sp.sampleSize()];
		while (Button.ESCAPE.isUp()) {
			mqtt.fetchSample(sample, 0);
			Delay.msDelay(10000);
		}
		ir.close();
		System.exit(0);
	}
}

I am using an MQTT broker called mosquitto running on a Raspberry Pi. Node-RED is running on the same Raspberry Pi.

For the MQTT filter you need the jar file for the Eclipse Paho MQTT client.

With the new 0.9.1 release of leJOS, you can just add the jar file to your Eclipse Java Build path and the new Eclipse 0.9.1 version of the Eclipse plugin will automatically upload it to the EV3.

Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: