Javadoc link

MQTT is one of the protocols lumped in with the "Internet of Things" (IoT) and it can be used for lots of things. An MQTT client has been borged into MRL.

mqtt

Here's a screenshot from my Android phone running MyMQTT connected to the same broker.

 

And below is some text output from my MQTT broker (mosquitto running on a Beagle Bone Black). You can see the MRL client ("MRL MQTT python") and the Android app (".1416095691833").

950325066: New connection from 10.0.0.8 on port 1883.

950325066: Invalid protocol "MQTT" in CONNECT from 10.0.0.8.

950325066: Socket read error on client (null), disconnecting.

950325066: New connection from 10.0.0.8 on port 1883.

950325066: New client connected from 10.0.0.8 as MRL MQTT python (c1, k60).

950325066: Sending CONNACK to MRL MQTT python (0)

950325067: Received PUBLISH from MRL MQTT python (d0, q2, r0, m1, 'mrl', ... (25 bytes))

950325067: Sending PUBREC to MRL MQTT python (Mid: 1)

950325067: Received PUBREL from MRL MQTT python (Mid: 1)

950325067: Sending PUBCOMP to MRL MQTT python (Mid: 1)

950325067: Sending PUBLISH to .1416095691833 (d0, q1, r0, m1, 'mrl', ... (25 bytes))

950325067: Received PUBACK from .1416095691833 (Mid: 1)

950325127: Received PINGREQ from MRL MQTT python

950325127: Sending PINGRESP to MRL MQTT python

950325127: Received PINGREQ from .1416095691833

950325127: Sending PINGRESP to .1416095691833

950325187: Received PINGREQ from MRL MQTT python

950325187: Sending PINGRESP to MRL MQTT python

950325198: Received PINGREQ from .1416095691833

950325198: Sending PINGRESP to .1416095691833

950325221: Socket read error on client MRL MQTT python, disconnecting.

950325279: Received PINGREQ from .1416095691833

950325279: Sending PINGRESP to .1416095691833

950325339: Received PINGREQ from .1416095691833

950325339: Sending PINGRESP to .1416095691833

950325429: Client .1416095691833 has exceeded timeout, disconnecting.

 

Below is a screenshot from my phone about to publish a message to the channel (mrl/) that MRL is subscribed to.

 

And now below you can see how the message is displayed in the MQTT GUI

mqtt

Have worky (N) Mqtt Instances...

It may look like duplicates, but its not. There are 4 MRL Instances connected together, and all (so far) appear to be fully functional in the webgui.

There are 3 which have mqtt connectivity, and another that is connected through the webgui service.
WOOHOO ! - will continue looking for bugs and doing more refactoring ...

Mqtt Client UI

Mqtt Broker UI

 

So what can you do with Mqtt ?
Clients can send and recieve messages.
Brokers can allow any mqtt clients (not only MRL ones, but any device, or sensor, smartphone, computer or microcontroller that can speak mqtt) to publish and recieve messages.

Above is what I have currently hooked up at my house.

This is currently a draft pull request for review - https://github.com/MyRobotLab/myrobotlab/pull/795

I have also tested the mqtt client against Amazon IoT Core Mqtt Broker - its worky

The following items are in that PR
 

1. Created fully function embedded MqttBroker

2. WebGui for Mqtt and MqttBroker

3. Fixed getting message id

4. When SLF4J has multiple back ends - set log level fails - Fixed

5. Created Pojo MqttMsg

6. Created SslUtil class for tls

7. Complete refactor of Mqtt service

8. Made serviceData in Runtime transient - so that Runtime would be under the 128K msg size limit in Amazon

9. Changed Message.dataEncoding to field name to Message.encoding

10. Lowered Locales logging level to cut down on chatter

 

mqtt

References:

  • MQTT Wikipedia
  • OwnTracks (MQTT based GPS location sharing app for smart phones. Good guide for setting up a broker)
Example code (from branch develop):
#file : Mqtt.py (github)
#########################################
# Mqtt.py
# more info @: http://myrobotlab.org/service/Mqtt
#########################################
from time import sleep
 
# start service
mqtt = runtime.start("mqtt", "Mqtt")
broker = runtime.start("broker", "MqttBroker")
python = runtime.start("python", "Mqtt")
 
# start the local mqtt broker on standard port
broker.listen()
 
topic = "echoTopic"
 
mqtt.connect("tcp://localhost:1883")
# authentification mqtt.connect(broker,"guest","guest")
 
mqtt.subscribe(topic)
 
# qos = 1 # At most once (0), At least once (1), Exactly once (2).
mqtt.publish("echoTopic", "hello myrobotlab world")
python.subscribe("mqtt", "publishMqttMsg")
# or mqtt.addListener("publishMqttMsgString", "python")
 
# publishMqttMsg --> onMqttMsg(msg)
def onMqttMsg(msg):
  print ("message : ", msg)
 
 
for i in range(30):
    mqtt.publish(topic, "hello myrobotlab ! " + str(i))
    sleep(0.5)
Example configuration (from branch develop):
#file : Mqtt.py (github)
!!org.myrobotlab.service.config.MqttConfig
address: 0.0.0.0
listeners: null
mqttPort: 1883
password: null
peers: null
type: Mqtt
username: null
wsPort: 8080