import time import os import meshtastic import meshtastic.tcp_interface from pubsub import pub from send_to_irc import SendMsgBot import json from signal import signal, SIGPIPE, SIG_DFL signal(SIGPIPE,SIG_DFL) MESSAGE_FILE = "message.txt" def send_message_from_mesh(): print('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=True) # Clear the file after sending the message open(MESSAGE_FILE, 'w').close() print('done sending') def onReceive(packet, interface): # called when a packet arrives print('******************************************') nodes = interface.nodesByNum # fromNode = nodes[packet['from']] # toNode = nodes[packet['to']] # TODO: REFACTOR, THIS IS UGLY AS F message = '' if packet['from'] in nodes: fromNode = nodes[packet['from']] message += f"From SN: {fromNode['user']['shortName']} LN: {fromNode['user']['longName']} \n" # print(f"From SN: {fromNode['user']['shortName']} LN: {fromNode['user']['longName']}") if packet['to'] in nodes: toNode = nodes[packet['to']] message += f"To SN: {toNode['user']['shortName']} LN: {toNode['user']['longName']} \n" # print(f"To SN: {toNode['user']['shortName']} LN: {toNode['user']['longName']}") else: message += f"To SN: Unknown LN: Unknown \n" if message: print(message) # print(f"To SN: {toNode['user']['shortName']} LN: {toNode['user']['longName']}") # fromNode = interface.getNode(nodeId=packet['from']) # print(fromNode.getLongName()) # toNode = interface.getNode(nodeId=packet['to']) 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(message) file.write(f"TELEMETRY_APP: ") # file.write(str(packet)) print('TELEMETRY_APP found') elif app_name == 'POSITION_APP': with open(MESSAGE_FILE, 'w') as file: file.write(message) file.write(f"POSITION_APP: ") # file.write(str(packet)) print('POSITION_APP found') elif app_name == 'NODEINFO_APP': with open(MESSAGE_FILE, 'w') as file: file.write(message) file.write(f"NODEINFO_APP: ") # file.write(str(packet)) print('NODEINFO_APP found') else: print('Not a recognized portnum message') print(packet) if 'decoded' in packet and 'text' in packet['decoded']: # print(packet) txt_message = packet['decoded']['text'] # Write message to file with open(MESSAGE_FILE, 'w') as file: file.write(message) file.write(txt_message) # Notify send_message function # pub.sendMessage('send_to_irc') print(f"text message here: {txt_message}") else: print('Not the text message') # print(packet) # def onReceive2(packet, interface2): # onReceive(packet, interface2) def onConnection(interface, topic=pub.AUTO_TOPIC): pass pub.subscribe(onConnection, "meshtastic.connection.established") # 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.104') # mobile # interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.1.102') # repeater interface.showInfo() # # interface.getNode() # tables = interface.showNodes() # nodes = interface.nodesByNum # print(nodes) # interface.getNode() # interface = meshtastic.tcp_interface.TCPInterface(hostname='192.168.1.104') # mobile # send message from client ip to repeater. # Subscribe onReceive function to the meshtastic.receive topic pub.subscribe(onReceive, "meshtastic.receive") # pub.subscribe(onReceive2, "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()