I had some IoT devices that I wanted to integrate in my monitoring. For this I set up a MQTT broker as the MQTT protocol is a simple solution to send data from IoT devices to a server. This tutorial is focusing on setting up the server, but I also introduce a Python based MQTT client to test our installation.
On your server, first install mosquitto, our MQTT server/broker.
```bash
sudo apt-get install mosquitto
```
Allow standard mqtt port in firewall (if you have ufw installed)
```bash
sudo ufw allow 1883
```
Now on the client side connect to the server and publish some fake sensor values.
First install the mqtt client
```bash
sudo pip install phao-mqtt
```
and then use the following python code on your client side to send fake values to your server. You only need to change `mqtt.example.com` to your servers IP/domain.
and configure mosquitto to use it in `/etc/mosquitto/conf.d/default.conf`:
```
allow_anonymous false
password_file /etc/mosquitto/passwd
```
Now restart mosquitto to enable the protection
```bash
sudo systemctl restart mosquitto
```
Test the installation by uncommenting `client.username_pw_set(username="username",password="my_super_secret_pw")` and filling in your credentials.
The result code `0` indicates a valid connection. `5` indicates a authentication error.
I hope this helps setting up a MQTT broker. Hopefully I will have the time to write how to connect such a broker to Grafana via Telegraf and Influx DB.