Skip to content

Commit acff793

Browse files
authored
Updates and bug fixes meshtastic_serial_message_reader.py
1 parent 53b5850 commit acff793

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

examples/meshtastic_serial_message_reader.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
# About :
77
# This script will print messages as they arrive from a meshtastic node connected via serial port USB.
88
# If you have multiple nodes attached, you will need to edit this script and specify the node to monitor.
9-
# https://gist.github.com/henri/a6584d55813f971e5b1a4ee940c07d25
109
#
11-
# Requirements :
10+
# Requirements :
1211
# You will need to install python meshtastic libraries : https://github.com/meshtastic/python
1312
#
1413
# Version History :
1514
# 1.0 - initial release
16-
# 1.1 - added support for sender id and bug fixs
15+
# 1.1 - added support for sender id and bug fixes
16+
# 1.2 - added date and time reporting to each message
17+
# 1.3 - bug fixes and improved error handling
1718

1819
import time
20+
from datetime import datetime, timezone
1921
import meshtastic
2022
import meshtastic.serial_interface
2123
from pubsub import pub
@@ -24,22 +26,33 @@ def onReceive(packet, interface):
2426
# DEBUGGING
2527
# print(f"message arrived")
2628
# print(f"{packet}")
29+
# print(f"-----------------------------------------------------------------")
2730
try:
2831
if packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP':
29-
message = packet['decoded']['text']
30-
channel_num = packet['channel']
31-
sender_id = packet['fromId']
32-
print(f"{channel_num} : {sender_id} : {message}")
33-
except KeyError as e:
34-
print(f"unable to decode message")
32+
try:
33+
message = packet['decoded']['text']
34+
try:
35+
channel_num = packet['channel']
36+
except KeyError as e1:
37+
channel_num = 0
38+
sender_id = packet['fromId']
39+
message_time = datetime.now().strftime(f"%a %b %d %Y %H:%M:%S {tz_name}")
40+
print(f"{message_time} : {channel_num} : {sender_id} : {message}")
41+
except KeyError as e2:
42+
print(f"unable to decode message")
43+
return
44+
except KeyError as e3:
3545
return
3646

47+
# configure the local time zone
48+
tz_name = time.tzname[time.localtime().tm_isdst > 0]
49+
50+
# registrer for incomming messages
3751
#pub.subscribe(onReceive, "meshtastic.receive.text")
3852
pub.subscribe(onReceive, "meshtastic.receive")
3953

40-
# try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
54+
# attempt to locate a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
4155
interface = meshtastic.serial_interface.SerialInterface()
4256

4357
while True:
4458
time.sleep(10) # wait for the next message
45-

0 commit comments

Comments
 (0)