forked from brad28b/meshtastic-cli-receive-text
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_messages_tcp.py
75 lines (63 loc) · 2.27 KB
/
read_messages_tcp.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
72
73
74
75
import time
import sys
from pubsub import pub
from meshtastic.tcp_interface import TCPInterface
from meshtastic import portnums_pb2
node_ip = '192.168.1.125' # Replace with your Meshtastic node's IP address
def get_node_info(node_ip):
print("Initializing TcpInterface to get node info...")
local = TCPInterface(hostname=node_ip)
node_info = local.nodes
local.close()
print("Node info retrieved.")
return node_info
def parse_node_info(node_info):
print("Parsing node info...")
nodes = []
for node_id, node in node_info.items():
nodes.append({
'num': node_id,
'user': {
'shortName': node.get('user', {}).get('shortName', 'Unknown')
}
})
print("Node info parsed.")
return nodes
def on_receive(packet, interface, node_list):
try:
if packet['decoded'].get('portnum') == 'TEXT_MESSAGE_APP':
message = packet['decoded']['payload'].decode('utf-8')
fromnum = packet['fromId']
shortname = next((node['user']['shortName'] for node in node_list if node['num'] == fromnum), 'Unknown')
print(f"{shortname}: {message}")
except KeyError:
pass # Ignore KeyError silently
except UnicodeDecodeError:
pass # Ignore UnicodeDecodeError silently
def main():
print(f"Using node IP: {node_ip}")
# Retrieve and parse node information
node_info = get_node_info(node_ip)
node_list = parse_node_info(node_info)
# Print node list for debugging
print("Node List:")
for node in node_list:
print(node)
# Subscribe the callback function to message reception
def on_receive_wrapper(packet, interface):
on_receive(packet, interface, node_list)
pub.subscribe(on_receive_wrapper, "meshtastic.receive")
print("Subscribed to meshtastic.receive")
# Set up the TcpInterface for message listening
local = TCPInterface(hostname=node_ip)
print("TcpInterface setup for listening.")
# Keep the script running to listen for messages
try:
while True:
sys.stdout.flush()
time.sleep(1) # Sleep to reduce CPU usage
except KeyboardInterrupt:
print("Script terminated by user")
local.close()
if __name__ == "__main__":
main()