Project:MQTT Broker: Difference between revisions

From CoMakingSpace Wiki

(MQTT Broker installed.)
(Removed help needed, Added comment about how paho and adafruit mqtt sucked)
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
|project title=MQTT Broker
|project title=MQTT Broker
|image=MusicStation.JPG
|image=MusicStation.JPG
|status=only an idea
|status=done
|date=
|date=July 2018
|initiator=[[User:NitramLegov|Martin]]
|initiator=[[User:NitramLegov|Martin]]
|team=[[User:NitramLegov|Martin]] {{HelpNeeded}}
|team=[[User:NitramLegov|Martin]]
|tools=[[Raspberry Pi]]
|tools=[[Raspberry Pi]]
|software=[https://github.com/comakingspace/MQTTBroker MQTTBroker]
|software=[https://github.com/comakingspace/MQTTBroker MQTTBroker]
}}
}}


== MQTT ==
= MQTT Overall =
MQTT (Message Queue Telemetry Transport) is a communication protocol with a strong focus on a small data footprint. <br>
MQTT (Message Queue Telemetry Transport) is a communication protocol with a strong focus on a small data footprint. <br>
Therefore, it is perfect for communication between lightweight devices (such as microcontrollers like the [[ESP8266]]). MQTT generally runs on TCP/IP, but there is also an extension (MQTT-SN), which can be used on non-TCP networks. Further information can of course be found on [https://en.wikipedia.org/wiki/MQTT Wikipedia], [http://mqtt.org/documentation mqtt.org] or - if you require German information - [https://www.informatik-aktuell.de/betrieb/netzwerke/mqtt-leitfaden-zum-protokoll-fuer-das-internet-der-dinge.html Informatik-Aktuell] published a nice introduction article.<br>
Therefore, it is perfect for communication between lightweight devices (such as microcontrollers like the [[ESP8266]]). MQTT generally runs on TCP/IP, but there is also an extension (MQTT-SN), which can be used on non-TCP networks. Further information can of course be found on [https://en.wikipedia.org/wiki/MQTT Wikipedia], [http://mqtt.org/documentation mqtt.org] or - if you require German information - [https://www.informatik-aktuell.de/betrieb/netzwerke/mqtt-leitfaden-zum-protokoll-fuer-das-internet-der-dinge.html Informatik-Aktuell] published a nice introduction article.<br>
Line 19: Line 19:
== Client usage ==
== Client usage ==
As mentioned, MQTT is designed to run on a broad variety of devices. As of this, implementations exist for microcontrollers, regular PCs as well as smartphones. <br>
As mentioned, MQTT is designed to run on a broad variety of devices. As of this, implementations exist for microcontrollers, regular PCs as well as smartphones. <br>
If you want to learn more about topics, messages, subscriptions and QoS, please read this part of the [https://mosquitto.org/man/mqtt-7.html mosquitto documentation]. <br>
===Libraries===
If you want to integrate MQTT in your project, please check the selection of client libraries on [https://github.com/mqtt/mqtt.github.io/wiki/libraries GitHub]. <br>
If you want to integrate MQTT in your project, please check the selection of client libraries on [https://github.com/mqtt/mqtt.github.io/wiki/libraries GitHub]. <br>
For anything using the Arduino IDE, please also see the library provided by adafruit [https://learn.adafruit.com/mqtt-adafruit-io-and-you/arduino-plus-library-setup AdafruitMQTT].<br>
===Microcontroller===
If you want to learn more about topics, messages, subscriptions and QoS, please read this part of the [https://mosquitto.org/man/mqtt-7.html mosquitto documentation]. <br>
For anything using the Arduino IDE, please use [https://github.com/knolleary/pubsubclient PubSubClient]. {{NL}} and {{P}} have tried the Adafruit MQTT library as well as the one from the paho project. Both sucked (in different ways, but basically critical things did not work).<br>
If you want to "just use" MQTT on your device and check the messages in our network, please see this [https://www.hivemq.com/blog/seven-best-mqtt-client-tools article].
===Clients with UI===
 
If you want to "just use" MQTT on your device and check the messages in our network, please see this [https://www.hivemq.com/blog/seven-best-mqtt-client-tools article]. <br />
On PCs, we have made good experiences with [http://www.mqttfx.org/ MQTT.fx], which is a Java based client.<br />
On Android, we have been making good experiences with [https://play.google.com/store/apps/details?id=at.tripwire.mqtt.client&rdid=at.tripwire.mqtt.client MyMQTT].


=== CoMakingSpace Implementation ===
= MQTT in the CoMakingSpace =


We added a MQTT Broker to our [[network]] in order to enable IoT devices to easily communicate with each other and send / receive messages (such as status updates). <br>
In order to be able to use MQTT in the CoMakingSpace, the [[Raspberry Pi]] in the [[Common Room|common room]] runs an instance of [https://mosquitto.org/ mosquitto], which is an open source MQTT broker.  


Our implementation runs on the same [[Raspberry Pi]] in the [[Common Room|common room]] that also hosts [[Project:Music Station]]. Therefore, the hostname is comakingcontroller (having a fixed IP: 192.168.1.2). It offers its services on the standard MQTT port: 1883.
Our implementation runs on the same [[Raspberry Pi]] in the [[Common Room|common room]] that also hosts [[Project:Music Station]]. Therefore, the hostname is comakingcontroller (having a fixed IP: 192.168.1.2). It offers its services on the standard MQTT port: 1883.


==Automated Publishing==
===Shell===
In addition to the broker, the pi in the [[Common Room|common room]] also got the mosquitto-clients package installed. This package mainly provides [https://mosquitto.org/man/mosquitto_pub-1.html mosquitto_pub] and [https://mosquitto.org/man/mosquitto_sub-1.html mosquitto_sub]. This means that you can publish messages via the shell. Of course, this can also be automated via a [[cron]] job. An example message could be published as follows: <br />
<code>mosquitto_pub -h comakingcontroller -t /CommonRoom/FDD/Text -m "Test"</code>
===Python===
For python users, the [https://www.eclipse.org/paho/clients/python/docs/ paho-mqtt] package was installed. So feel free to use python scripts to publish messages.An example can be found on [https://github.com/comakingspace/CommonRoomPiSettings/blob/master/OtherScripts/ActiveWikiUsers.py github].
This should work both with Python 2 and Python 3. Of course, it is recommended to use Python 3 for any new scripts!
==Users==
The following projects use - or will use - the MQTT Broker:
The following projects use - or will use - the MQTT Broker:
* [[Project: Enhanced Door Bell]]
* [[Project:Flip Dot Display]]
* [[Project:Enhanced Door Bell]]


== Topics ==
== Topics ==
If you create a new topic, it would be great if you document it here. <br>
If you create a new topic, it would be great if you document it here. <br>
Right now, the following topics exist:<br>
Right now, the following topics exist:<br>
---none---
{|class="wikitable"
!Topic !! Description
|-
|/CommonRoom/FDD/Text || Send text to the FlipDot Display. It will be displayed immediately.
|-
|/CommonRoom/FDD/Binary || not implemented yet. Will be used to send binary data directly to the FlipDot Displays. Please note that it will be the senders responsibility to send meaningful data. Anything else will lead to rubbish being displayed.
|-
|}
 
 


[[Category:Raspberry Pi]]
[[Category:Raspberry Pi]]

Latest revision as of 08:35, 11 January 2019

ProjectInfoBox

MQTT Broker

MusicStation.JPG
Status: done
Release Date: July 2018
Initiator: Martin
Team: Martin
Tools Used: Raspberry Pi
Software Used: MQTTBroker


MQTT Overall

MQTT (Message Queue Telemetry Transport) is a communication protocol with a strong focus on a small data footprint.
Therefore, it is perfect for communication between lightweight devices (such as microcontrollers like the ESP8266). MQTT generally runs on TCP/IP, but there is also an extension (MQTT-SN), which can be used on non-TCP networks. Further information can of course be found on Wikipedia, mqtt.org or - if you require German information - Informatik-Aktuell published a nice introduction article.

Broker

Since MQTT follows a client/server architecture, a central server is needed in order to publish messages and to subscribe to messages. This server is called broker. Since MQTT is an open protocol, multiple implementations exist, which have been compared on GitHub.

Client usage

As mentioned, MQTT is designed to run on a broad variety of devices. As of this, implementations exist for microcontrollers, regular PCs as well as smartphones.
If you want to learn more about topics, messages, subscriptions and QoS, please read this part of the mosquitto documentation.

Libraries

If you want to integrate MQTT in your project, please check the selection of client libraries on GitHub.

Microcontroller

For anything using the Arduino IDE, please use PubSubClient. Martin and Patrick have tried the Adafruit MQTT library as well as the one from the paho project. Both sucked (in different ways, but basically critical things did not work).

Clients with UI

If you want to "just use" MQTT on your device and check the messages in our network, please see this article.
On PCs, we have made good experiences with MQTT.fx, which is a Java based client.
On Android, we have been making good experiences with MyMQTT.

MQTT in the CoMakingSpace

In order to be able to use MQTT in the CoMakingSpace, the Raspberry Pi in the common room runs an instance of mosquitto, which is an open source MQTT broker.

Our implementation runs on the same Raspberry Pi in the common room that also hosts Project:Music Station. Therefore, the hostname is comakingcontroller (having a fixed IP: 192.168.1.2). It offers its services on the standard MQTT port: 1883.

Automated Publishing

Shell

In addition to the broker, the pi in the common room also got the mosquitto-clients package installed. This package mainly provides mosquitto_pub and mosquitto_sub. This means that you can publish messages via the shell. Of course, this can also be automated via a cron job. An example message could be published as follows:
mosquitto_pub -h comakingcontroller -t /CommonRoom/FDD/Text -m "Test"

Python

For python users, the paho-mqtt package was installed. So feel free to use python scripts to publish messages.An example can be found on github. This should work both with Python 2 and Python 3. Of course, it is recommended to use Python 3 for any new scripts!

Users

The following projects use - or will use - the MQTT Broker:

Topics

If you create a new topic, it would be great if you document it here.
Right now, the following topics exist:

Topic Description
/CommonRoom/FDD/Text Send text to the FlipDot Display. It will be displayed immediately.
/CommonRoom/FDD/Binary not implemented yet. Will be used to send binary data directly to the FlipDot Displays. Please note that it will be the senders responsibility to send meaningful data. Anything else will lead to rubbish being displayed.