godot_client | ||
resources | ||
rp2040_client | ||
README.md |
In this workshop, we will learn how to use MQTT for connected devices.
- Install Mosquitto Broker and Client
- Adjust ports/firewalls
On the client side, we will learn to install and use an MQTT client on diverse devices.
- Install MicroPython client on RP2040
- Install GDScript client on Godot Game Engine
Software Required
- Git (for downloading project repo)
- Thonny (python code editor with RP2040 support)
Optional
- Raspberry Pi running Raspbian
- Godot Game Engine
Intro
What is MQTT? MQTT is a very lightweight protocol for sending messages around a network. It is simple and easy to work with, involving the use of a "broker" that centralizes the messages, and "clients" that can receive and send the messages.
Because MQTT is so lightweight, it works well on devices with limited resources, such as microcontrollers. It has become one of the most popular protocols for internet-of-things and edge devices.
The MQTT Broker
While there are many software packages for installing an MQTT broker, probably the most popular is Mosquitto. It is available for most operating systems, and comes with both broker and client software.
Installation on Linux (debian):
sudo apt install mosquitto-dev
Once you have installed the broker, it will automatically start as a service that will run in the background, and clients can connect to it.
In Mosquitto version 2+ extra security was implemented requiring clients to connect with authentication. This is overkill for many purposes, so we will allow clients to connect anonymously by editing the configuration file.
Locate /etc/mosquitto/mosquitto.conf
Open the file for editing:
sudo nano mosquitto.conf
Add the following line:
allow_anonymous true
However, there may be other factors in your OS setup that can affect the clients from connecting - most importantly your firewall settings.
In Ubuntu Linux, the most common firewall software is UFW. This comes by default with that distribution. The firewall can be configured to allow or disallow traffic on specific ports.
The port used by default for the Mosquitto broker is 1883. We must ensure that this is available for clients to connect to.
The command to see if UFW is running and what ports it is monitoring looks like this:
sudo ufw status
To allow connections through the firewall on port 1883 (the MQTT port) you can write a rule for UFW
sudo ufw allow 1883/tcp
Note that you can get a lot more specific with UFW rules, including limiting access to certain applications or IP addresses.
Resources
- Mosquitto man pages https://mosquitto.org/man/
- Godot MQTT client https://github.com/goatchurchprime/godot-mqtt
- Install broker/client on Linux http://www.steves-internet-guide.com/install-mosquitto-linux/
- Core Electronics RP2040 guide https://core-electronics.com.au/guides/getting-started-with-mqtt-on-raspberry-pi-pico-w-connect-to-the-internet-of-things/
- RP2040 client https://github.com/micropython/micropython-lib/tree/master/micropython/umqtt.simple