87 lines
2.4 KiB
Python
87 lines
2.4 KiB
Python
import meshtastic
|
|
import meshtastic.serial_interface
|
|
from meshtastic import mqtt_pb2, mesh_pb2
|
|
from pubsub import pub
|
|
import paho.mqtt.client as mqtt
|
|
import time
|
|
|
|
# --- CONFIGURATION ---
|
|
MQTT_BROKER = "mqtt.meshtastic.org"
|
|
MQTT_USER = "meshdev"
|
|
MQTT_PW = "large4cats"
|
|
REGION = "US"
|
|
CHANNEL = "LongFast"
|
|
SERIAL_PORT = "/dev/ttyACM0"
|
|
|
|
# --- MQTT SETUP ---
|
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
|
client.username_pw_set(MQTT_USER, MQTT_PW)
|
|
|
|
def on_connect(client, userdata, flags, rc, properties):
|
|
if rc == 0:
|
|
print("Connected to Meshtastic MQTT Broker!")
|
|
else:
|
|
print(f"Failed to connect, return code {rc}")
|
|
|
|
client.on_connect = on_connect
|
|
|
|
try:
|
|
client.connect(MQTT_BROKER, 1883, 60)
|
|
client.loop_start()
|
|
except Exception as e:
|
|
print(f"MQTT Connection Error: {e}")
|
|
|
|
# --- BRIDGE LOGIC ---
|
|
def onReceive(packet, interface):
|
|
try:
|
|
from_id = packet.get('fromId')
|
|
decoded = packet.get('decoded', {})
|
|
portnum = decoded.get('portnum')
|
|
|
|
if not from_id or from_id == 'None':
|
|
return
|
|
|
|
print(f"\n[MESH] From: {from_id} | Type: {portnum}")
|
|
|
|
raw_packet = packet.get('raw')
|
|
|
|
if raw_packet:
|
|
envelope = mqtt_pb2.ServiceEnvelope()
|
|
envelope.packet.CopyFrom(raw_packet)
|
|
envelope.channel_id = CHANNEL
|
|
|
|
# FIXED ATTRIBUTE HERE
|
|
envelope.gateway_id = f"!{interface.myInfo.my_node_num:08x}"
|
|
|
|
topic = f"msh/{REGION}/2/e/{CHANNEL}/{from_id}"
|
|
|
|
client.publish(topic, envelope.SerializeToString())
|
|
print(f" --> Uplinked to {topic}")
|
|
else:
|
|
print(" (Skipping: No raw packet data found)")
|
|
|
|
except Exception as e:
|
|
print(f" Error in bridge: {e}")
|
|
|
|
# --- MAIN RUNNER ---
|
|
print("--- Starting RAK4630 to Global Map Bridge ---")
|
|
print(f"Initializing Serial on {SERIAL_PORT}...")
|
|
|
|
try:
|
|
interface = meshtastic.serial_interface.SerialInterface(devPath=SERIAL_PORT)
|
|
pub.subscribe(onReceive, "meshtastic.receive")
|
|
|
|
# FIXED ATTRIBUTE HERE
|
|
print(f"My Node ID: !{interface.myInfo.my_node_num:08x}")
|
|
print("Bridge Active. Waiting for Mesh traffic... (Ctrl+C to stop)")
|
|
|
|
while True:
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
print("\nStopping bridge...")
|
|
client.loop_stop()
|
|
interface.close()
|
|
except Exception as e:
|
|
print(f"Connection Error: {e}")
|