commit 2d1fdda152494db174f5415286ff1d1b85050880 Author: James Pattinson Date: Wed Feb 19 19:09:25 2025 +0000 Initial Commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..671de81 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:latest +WORKDIR /usr/local/bin +COPY mqtt-log.py . +RUN pip install --no-cache-dir paho-mqtt +CMD ["python","-u","mqtt-log.py"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..7fc264b --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +version: '3.8' + +services: + meshmqtt: + build: + context: . + ports: + - "777:777" # Adjust to your needs + environment: + - ENV_VAR=value # Optional environment variables + volumes: + - ./app:/app # Optional volume mount diff --git a/mqtt-log.py b/mqtt-log.py new file mode 100755 index 0000000..f9fcdb1 --- /dev/null +++ b/mqtt-log.py @@ -0,0 +1,40 @@ +import paho.mqtt.client as mqtt +import logging + +# Configure logging +logging.basicConfig(filename="mqtt_messages.log", level=logging.INFO, format="%(asctime)s - %(message)s") + +# MQTT Broker details +BROKER = "bonnie.pattinson.org" # Change to your broker address +PORT = 1883 # Default MQTT port +TOPIC = "msh/EU_868/2/json/#" # Change to your topic +USERNAME = "mesh" # Change to your MQTT username +PASSWORD = "Welcome123" # Change to your MQTT password + +print("Script Loaded!") + +def on_connect(client, userdata, flags, reason_code, properties): + if reason_code == 0: + print("Connected to MQTT Broker!") + client.subscribe(TOPIC) + else: + print(f"Failed to connect, return code {reason_code}") + +# Callback when a message is received +def on_message(client, userdata, msg): + message = msg.payload.decode("utf-8", errors="replace") + log_entry = f"Topic: {msg.topic}, Message: {message}" + print(log_entry) # Print to console + logging.info(log_entry) # Log to file + +# Setup MQTT client +client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2) +client.username_pw_set(USERNAME, PASSWORD) +client.on_connect = on_connect +client.on_message = on_message + +# Connect to the broker +client.connect(BROKER, PORT, 60) + +# Keep listening for messages +client.loop_forever()