Skip to content

kafkaphoenix/iot-network

Repository files navigation

IoT Network Study with MQTT and ZeroMQ

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.

MQTT Broker with Docker

This project includes a Dockerized MQTT broker using Eclipse Mosquitto. The broker supports both MQTT and WebSocket connections.

Installation

  1. Ensure you have Docker and Docker Compose installed on your machine.
  2. Clone this repository to your local machine.
  3. Navigate to the project directory.
  4. Run make up to start the MQTT broker.
  5. Use make logs to view the broker logs.

Configuration

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.

Testing

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.

Using mosquitto-client tools

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 test

Defining 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.

MQTT client applications

You can also use MQTT client applications like MQTT.fx or MQTT Explorer to connect to the broker.

Using Python Paho MQTT Client

You can use the Paho MQTT client library for Python to connect to the broker. A simple example is provided.

make demo-mqtt-node

Adding new users

To add new users, use the following command:

docker exec -it mqtt_broker  mosquitto_passwd -c /mosquitto/config/passwd <username>

Setting up TLS/SSL

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.key

You'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

ZeroMQ Demo

This project also includes a simple demo of ZeroMQ for peer-to-peer communication.

Running the Demo

Run the the following command to start a ZeroMQ node:

make demo-zmq-node

About

IoT Network Communication Layer Study

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published