lora_sync/main.py

90 lines
2.8 KiB
Python

import time
import os
import meshtastic
import meshtastic.tcp_interface
from pubsub import pub
from send_to_irc import SendMsgBot
MESSAGE_FILE = "message.txt"
def send_message_from_mesh():
if os.path.exists(MESSAGE_FILE):
with open(MESSAGE_FILE, 'r') as file:
message = file.read().strip()
if message:
# If you dont have irc...then you can just print/hook into your service here.
xmpp = SendMsgBot('mesh_bot@website.com', 'totallyrealpassword', 'accounttosend@website.com', message)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
xmpp.process(forever=False)
# Clear the file after sending the message
open(MESSAGE_FILE, 'w').close()
def onReceive(packet, interface):
# called when a packet arrives
if 'decoded' in packet:
print('decoded')
decoded = packet['decoded']
if 'TEXT_MESSAGE_APP' in packet['decoded']:
print('TEXT_MESSAGE_APP')
if 'portnum' in decoded:
app_name = decoded['portnum']
if app_name == 'TELEMETRY_APP':
with open(MESSAGE_FILE, 'w') as file:
file.write(f"TELEMETRY_APP: ")
file.write(str(packet))
print('Telemetry found')
elif app_name == 'POSITION_APP':
with open(MESSAGE_FILE, 'w') as file:
file.write(f"POSITION_APP: ")
file.write(str(packet))
print('Position found')
else:
print('Not a recodnized portnum message')
print(packet)
if 'decoded' in packet and 'text' in packet['decoded']:
print(packet)
message = packet['decoded']['text']
# Write message to file
with open(MESSAGE_FILE, 'w') as file:
file.write(message)
# Notify send_message function
# pub.sendMessage('send_to_irc')
print(f"text message here: {message}")
else:
print('Not the text message')
print(packet)
# Subscribe send_message function to the send_to_irc topic
# pub.subscribe(send_message, 'send_to_irc')
# This does not work ATM, so doing a quick workaround.
# Initialize the meshtastic interface
interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.1.102') # repeater
# send message from client ip to repeater.
# Subscribe onReceive function to the meshtastic.receive topic
pub.subscribe(onReceive, "meshtastic.receive")
# Keep the script running to process incoming messages
try:
while True:
time.sleep(1)
send_message_from_mesh()
except KeyboardInterrupt:
pass
finally:
interface.close()