|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Released Under GNU GPLv3 |
| 4 | +# Copyright 2025 Henri Shustak |
| 5 | +# |
| 6 | +# About : |
| 7 | +# This script will print messages as they arrive from a meshtastic node connected via serial port USB. |
| 8 | +# If you have multiple nodes attached, you will need to edit this script and specify the node to monitor. |
| 9 | +# |
| 10 | +# Requirements : |
| 11 | +# You will need to install python meshtastic libraries : https://github.com/meshtastic/python |
| 12 | +# |
| 13 | +# Version History : |
| 14 | +# 1.0 - initial release |
| 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 |
| 18 | + |
| 19 | +import time |
| 20 | +from datetime import datetime, timezone |
| 21 | +import meshtastic |
| 22 | +import meshtastic.serial_interface |
| 23 | +from pubsub import pub |
| 24 | + |
| 25 | +def onReceive(packet, interface): |
| 26 | + # DEBUGGING |
| 27 | + # print(f"message arrived") |
| 28 | + # print(f"{packet}") |
| 29 | + # print(f"-----------------------------------------------------------------") |
| 30 | + try: |
| 31 | + if packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP': |
| 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: |
| 45 | + return |
| 46 | + |
| 47 | +# configure the local time zone |
| 48 | +tz_name = time.tzname[time.localtime().tm_isdst > 0] |
| 49 | + |
| 50 | +# registrer for incomming messages |
| 51 | +#pub.subscribe(onReceive, "meshtastic.receive.text") |
| 52 | +pub.subscribe(onReceive, "meshtastic.receive") |
| 53 | + |
| 54 | +# attempt to locate a meshtastic device, otherwise provide a device path like /dev/ttyUSB0 |
| 55 | +interface = meshtastic.serial_interface.SerialInterface() |
| 56 | + |
| 57 | +while True: |
| 58 | + time.sleep(10) # wait for the next message |
0 commit comments