-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_mqtt_proxy.py
73 lines (56 loc) · 2.37 KB
/
dev_mqtt_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#
# Quick Bridge & Dirty to bring test data into a test node via MQTT
#
# This file is part of the meshtastic monitoring bridge distribution
# (https://github.com/jaredquinn/meshtastic-bridge).
# Copyright (c) 2024 Jared Quinn.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################
# This program is designed to perform a one-way proxy of MQTT traffic
# to a local MQTT server; primarily to provide test data during
# development. It can however also be used to monitor a mesh via MQTT
# without having a node appear in the nodelist.
#
# Requires a self-hosted MQTT broker.
######################################################################
import paho.mqtt.client as mqtt
import logging
import json
import os
import time
MQTT_HOST = os.environ.get('MQTT_HOST', "10.10.1.100")
PROXY_TOPIC = os.environ.get('MQTT_PROXY_TOPIC', 'msh/ANZ/#')
_w = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="jqdev-0x10", protocol=mqtt.MQTTv5)
_l = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="jqdev-0x10")
# We use the default Meshtastic credentials for the global server as
# the broker to listen on
_l.username_pw_set(username="meshdev", password="large4cats")
def on_listen_message(client, userdata, msg):
_w.publish(topic=msg.topic, payload=msg.payload)
def on_listen_connect(client, userdata, flags, reason_code, properties):
print('Connected to Listener')
client.subscribe(PROXY_TOPIC)
def on_write_connect(client, userdata, flags, reason_code, properties):
print('Connected to Writer')
_l.on_connect = on_listen_connect
_l.on_message = on_listen_message
_w.on_connect = on_write_connect
_l.connect("mqtt.meshtastic.org", 1883, 60)
_w.connect(MQTT_HOST, 1883, 60)
print('Starting Loops')
_l.loop_start()
_w.loop_start()
print('Waiting for data')
while True:
time.sleep(1)