This project is a study and implementation of an IoT network communication layer using MQTT and ZeroMQ protocols. An ADR document detailing the design decisions can be found in IOT_NETWORK_ADR.md.
This project includes a Dockerized MQTT broker using Eclipse Mosquitto. The broker supports both MQTT and WebSocket connections.
- Ensure you have Docker and Docker Compose installed on your machine.
- Clone this repository to your local machine.
- Navigate to the project directory.
- Run
make upto start the MQTT broker. - Use
make logsto view the broker logs.
The MQTT broker is configured via the mosquitto.conf file located in the mosquitto/config directory. You can modify this file to change settings such as authentication, logging, and listener ports.
You can test the MQTT broker using any MQTT client. Connect to the broker at localhost:1883 for MQTT and localhost:9001 for WebSocket connections.
The default credentials are set in the passwd file located in the mosquitto/config directory. Username: test, Password: test.
sudo apt install mosquitto-clients
mosquitto_sub -h localhost -t test/topicA -u test -P test
mosquitto_pub -h localhost -t test/topicA -m "Hello MQTT" -u test -P testDefining the subscribed topic as test/# will subscribe to all topics starting with test/ Another wildcard is + which matches a single level in the topic hierarchy. For example, test/+/data will match test/node1/data and test/node2/data but not test/node1/sensor/data.
You can also use MQTT client applications like MQTT.fx or MQTT Explorer to connect to the broker.
You can use the Paho MQTT client library for Python to connect to the broker. A simple example is provided.
make demo-mqtt-nodeTo add new users, use the following command:
docker exec -it mqtt_broker mosquitto_passwd -c /mosquitto/config/passwd <username>To enable TLS/SSL, you need to provide the necessary certificate files and update the mosquitto.conf file accordingly.
# Secure MQTT listener
listener 8883
cafile /mosquitto/config/ca.crt
certfile /mosquitto/config/server.crt
keyfile /mosquitto/config/server.key
# Secure WebSocket listener
listener 9002
protocol websockets
cafile /mosquitto/config/ca.crt
certfile /mosquitto/config/server.crt
keyfile /mosquitto/config/server.keyYou'll need to obtain SSL certificates for your domain. Let's Encrypt provides free certificates, or you can use reverse proxies like Caddy Server that handle certificate generation automatically. Place your certificate files in the mosquitto/config directory so they're available inside the container. example
This project also includes a simple demo of ZeroMQ for peer-to-peer communication.
Run the the following command to start a ZeroMQ node:
make demo-zmq-node