From 6271aaff667e081df5f9162861748346e5d4c7cd Mon Sep 17 00:00:00 2001 From: Franco Carbognani Date: Sat, 24 Aug 2024 14:47:45 +0200 Subject: [PATCH] Create mqttproxy.py --- src/simoc_sam/mqttproxy.py | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/simoc_sam/mqttproxy.py diff --git a/src/simoc_sam/mqttproxy.py b/src/simoc_sam/mqttproxy.py new file mode 100644 index 00000000..f20cb483 --- /dev/null +++ b/src/simoc_sam/mqttproxy.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +import paho.mqtt.client as mqtt + +KEEPALIVE = 10 # in seconds +TOPIC = "sam/#" + +LOCAL_BROKER ='localhost' +LOCAL_PORT = 1883 + +REMOTE_BROKER ='mqtt.simoc.space' # this must match the CNAME in your server-cert! +REMOTE_PORT = 8883 + +# Callback when the client connects to the broker +def on_local_connect(local_client, userdata, flags, rc, properties=None): + print(f'Locally connected with result code {rc}') + # Subscribe to the MQTT topic + local_client.subscribe(TOPIC) + +def on_message(client, userdata, msg): + payload = msg.payload.decode("utf-8") + topic = msg.topic + print(f"Received message: {payload}") + print(f"from topic: {topic}") + forward_message(topic, payload) + +# Function to forward the message to the remote broker +def forward_message(topic, payload): + remote_client.publish(topic, payload) + print(f"Message forwarded to remote broker: {topic} -> {payload}") + +# Create an MQTT client +local_client = mqtt.Client() +remote_client = mqtt.Client() + +# Set callback functions +local_client.on_connect = on_local_connect +local_client.on_message = on_message + +remote_client.tls_set(ca_certs="/etc/mosquitto/certs/ca.crt", certfile="/etc/mosquitto/certs/client.crt", keyfile="/etc/mosquitto/certs/client.key") +remote_client.tls_insecure_set(True) + +# Connect to the local MQTT broker +local_client.connect(LOCAL_BROKER, LOCAL_PORT, KEEPALIVE) +# Connect to the remote MQTT broker +remote_client.connect(REMOTE_BROKER, REMOTE_PORT, KEEPALIVE) + +# Start the loop to process received messages and maintain connections +local_client.loop_start() +remote_client.loop_start() + +try: + while True: + pass # Keep the script running +except KeyboardInterrupt: + print("Exiting bridge server...") + local_client.loop_stop() + remote_client.loop_stop() + local_client.disconnect() + remote_client.disconnect() +