Javadoc link

An Mqtt Broker is a service that can be used to easily distribute messages from different devices.  Mqtt is a very common protocol in IoT (Internet of Things).  You'll find many tutorials on the web covering Mqtt and sensors, lights or switches.  Mqtt tyically requires a network connection in order to deliver or recieve messages. Most microcontrollers some form of wifi or ether connectivity will have libraries for Mqtt.  This includes the popular Esp8266, Esp32 and even Arduino with an ethernet shield. 

Mqtt supports "topics".  Topics are named location where sensors can send their messages, and any device that has access can subscribe to recieve messages.  In the above diagram a temperature sensor is publishing the current temperature to a topic called "temp".  Two devices interested in recieving current temperature messages from the sensor subscribe to that same topic.  Any message sent from the device gets relayed to all subscribers.

Because its a simple and lightweight protocol its become popular with home automation systems.
An Mqtt broker is core to the most popular FOSS home automation systems

MyRobotLab now has the abilty to run an embedded Mqtt Broker service.

"Any" mqtt capable microcontroller or sensor has the possibility of interfacing with MRL now.

Any sensor or device which uses mqtt broker config can immediately be connected to the MRL MqttBroker service.  Fill in the the appropriate config, and your done !

But I'm going to do something a little more challenging.  I'm going to buy an expensive mqtt enabled device that uses a propriotary mqtt cloud broker and re-write the firmware to use MLR's MqttBroker.

Lets take an example of how we can use this mqtt broker. 
 

I found a very inexpensive ESP8266 based relay with these Sonoff basic switches. They are under $5 a unit. Sweet ! ..  If you use the firmware which they come with you can play with them using eWeLink mobile software


https://tasmota.github.io/docs/Getting-Started/ 

mqttbroker

Example code (from branch develop):
#########################################
# MqttBroker.py
# more info @: http://myrobotlab.org/service/MqttBroker
#########################################
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):
!!org.myrobotlab.service.config.MqttBrokerConfig
address: 0.0.0.0
listeners: null
mqttPort: 1883
password: null
peers: null
type: MqttBroker
username: null
wsPort: 8080