GithubHelp home page GithubHelp logo

rubenhoenle / matrix-mqtt-bridge Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 571 KB

Bridge for forwarding messages between the MQTT protocol and the Matrix messenger.

Home Page: https://rubenhoenle.github.io

Python 81.86% Nix 18.14%
mqtt matrix hivemq matrixbot docker

matrix-mqtt-bridge's Introduction

CI/CD

Matrix-MQTT-Bridge

This project was created to create bridge between the Matrix Messenger and the MQTT protocol. I'm using this to control a microcontroller / Raspberry Pi Pico over a Matrix chat. Using this project the microcontroller is able to recieve the messages posted into the Matrix chat (like in the graphic). MQTT Messages which get published by the microcontroller get also forwarded into the Matrix chat by the Matrix-MQTT-Bridge.

Matrix-MQTT-Bridge


Running the bridge

Setup

You can run the Matrix-MQTT-Bridge via Docker, e.g. by using this docker-compose.yaml file. You will have to create a config.ini file to configure the connection to the MQTT broker and to the Matrix server. I'm using this project in combination with a free private HiveMQ cloud instance, which acts as my MQTT broker.

version: "3.9"

services:
  matrix-mqtt-bridge:
    image: ghcr.io/rubenhoenle/matrix-mqtt-bridge:latest
    container_name: matrix-mqtt-bridge
    restart: always
    volumes:
      - ./config.ini:/config.ini

Config file

The config.ini is split into two parts: The Matrix configuration and the configuration for the MQTT connection.

Matrix configuration

You will need two Matrix accounts: The one you are using on e.g. your phone (I'm using my regular, personal Matrix Account for this) and this Matrix-MQTT-Bridge will require it's own account. After you have created a seperate Matrix account for the Matrix-MQTT-Bridge, create a new chatroom and with one of your accounts and add the other account to this room. Now, check if your able to write / recieve messages in this chatroom using your two different accounts.

Important: Matrix room has to be unencrypted. Do not enable encryption when creating the Matrix chatroom!

MQTT configuration

Is actually self-explanatory. Enter your Host (for me it's my HiveMQ cloud instance), the port and the credentials to connect to the MQTT message broker (username / password).

  • The topic_sub is the MQTT topic the Matrix-MQTT-Bridge will subscribe to. All MQTT messages recieved on this topic will be sent into the Matrix chatroom by the bridge.
  • When the Matrix-MQTT-Bridge recieves a message via Matrix, it will publish this message to the MQTT broker using the topic specified in topic_pub.
[MATRIX]
homeserver = https://matrix.org
user = @YOUR_USERNAME:matrix.org
password = YOUR_PASSWORD_USED_TO_LOGIN_INTO_MATRIX
room_id = !YOUR_ROOM_ID:matrix.org

[MQTT]
host = YOUR_HOST.hivemq.cloud
user = YOUR_MQTT_USER
password = YOUR_MQTT_PASSWORD
port = 8883
tls = true
topic_sub = mqttbridge/sub
topic_pub = mqttbridge/pub

Development and contributing

Development is based on the nix package manager.

# launch dev shell with all dependencies installed
nix develop

# format code
nix fmt

# run the matrix-mqtt-bridge
nix run

Building and running the docker image

# build the container image
nix build .#containerImage

# load the container image into docker
docker load < result

# start the matrix-mqtt-bridge container
docker compose up

matrix-mqtt-bridge's People

Contributors

dependabot[bot] avatar rubenhoenle avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

lukelr

matrix-mqtt-bridge's Issues

Error when trying to bring the container up

Hi, I'm very interested in this project. After creating a new user for the bot and a new room, creating a config.ini, and then using your docker-compose.yaml, I ran sudo docker compose up I received this error:

[+] Running 2/2
 ✔ Network matrix-mqtt-bridge-docker_default  Created                                                                                                                                                           0.1s 
 ✔ Container matrix-mqtt-bridge               Created                                                                                                                                                           0.2s 
Attaching to matrix-mqtt-bridge
matrix-mqtt-bridge  | Traceback (most recent call last):
matrix-mqtt-bridge  |   File "//./matrix_mqtt_bridge.py", line 102, in <module>
matrix-mqtt-bridge  |     mqtt_client.connect(mqtt_host, int(mqtt_port))
matrix-mqtt-bridge  |   File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 914, in connect
matrix-mqtt-bridge  |     return self.reconnect()
matrix-mqtt-bridge  |   File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1073, in reconnect
matrix-mqtt-bridge  |     sock.do_handshake()
matrix-mqtt-bridge  |   File "/usr/local/lib/python3.9/ssl.py", line 1310, in do_handshake
matrix-mqtt-bridge  |     self._sslobj.do_handshake()
matrix-mqtt-bridge  | ConnectionResetError: [Errno 104] Connection reset by peer
matrix-mqtt-bridge exited with code 0

I checked and the mqtt credentials, address, and port are correct. I can connect with mosquitto_sub using them. Any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.