|
| 1 | +"""Reply message demo script. |
| 2 | + To run: python examples/replymessage.py |
| 3 | + To run with TCP: python examples/replymessage.py --host 192.168.1.5 |
| 4 | + To run with BLE: python examples/replymessage.py --ble 24:62:AB:DD:DF:3A |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import time |
| 9 | +from typing import Any, Optional, Union |
| 10 | +from pubsub import pub |
| 11 | +import meshtastic.serial_interface |
| 12 | +import meshtastic.tcp_interface |
| 13 | +import meshtastic.ble_interface |
| 14 | +from meshtastic.mesh_interface import MeshInterface |
| 15 | + |
| 16 | +def onReceive(packet: dict, interface: MeshInterface) -> None: # pylint: disable=unused-argument |
| 17 | + """Reply to every received packet with some info""" |
| 18 | + text: Optional[str] = packet.get("decoded", {}).get("text") |
| 19 | + if text: |
| 20 | + rx_snr: Any = packet.get("rxSnr", "unknown") |
| 21 | + hop_limit: Any = packet.get("hopLimit", "unknown") |
| 22 | + print(f"message: {text}") |
| 23 | + reply: str = f"got msg '{text}' with rxSnr: {rx_snr} and hopLimit: {hop_limit}" |
| 24 | + print("Sending reply: ", reply) |
| 25 | + interface.sendText(reply) |
| 26 | + |
| 27 | +def onConnection(interface: MeshInterface, topic: Any = pub.AUTO_TOPIC) -> None: # pylint: disable=unused-argument |
| 28 | + """called when we (re)connect to the radio""" |
| 29 | + print("Connected. Will auto-reply to all messages while running.") |
| 30 | + |
| 31 | +parser = argparse.ArgumentParser(description="Meshtastic Auto-Reply Feature Demo") |
| 32 | +group = parser.add_mutually_exclusive_group() |
| 33 | +group.add_argument("--host", help="Connect via TCP to this hostname or IP") |
| 34 | +group.add_argument("--ble", help="Connect via BLE to this MAC address") |
| 35 | + |
| 36 | +args = parser.parse_args() |
| 37 | + |
| 38 | +pub.subscribe(onReceive, "meshtastic.receive") |
| 39 | +pub.subscribe(onConnection, "meshtastic.connection.established") |
| 40 | + |
| 41 | +iface: Optional[Union[ |
| 42 | + meshtastic.tcp_interface.TCPInterface, |
| 43 | + meshtastic.ble_interface.BLEInterface, |
| 44 | + meshtastic.serial_interface.SerialInterface |
| 45 | +]] = None |
| 46 | + |
| 47 | +# defaults to serial, use --host for TCP or --ble for Bluetooth |
| 48 | +try: |
| 49 | + if args.host: |
| 50 | + # note: timeout only applies after connection, not during the initial connect attempt |
| 51 | + # TCPInterface.myConnect() calls socket.create_connection() without a timeout |
| 52 | + iface = meshtastic.tcp_interface.TCPInterface(hostname=args.host, timeout=10) |
| 53 | + elif args.ble: |
| 54 | + iface = meshtastic.ble_interface.BLEInterface(address=args.ble, timeout=10) |
| 55 | + else: |
| 56 | + iface = meshtastic.serial_interface.SerialInterface(timeout=10) |
| 57 | +except KeyboardInterrupt as exc: |
| 58 | + raise SystemExit(0) from exc |
| 59 | +except Exception as e: |
| 60 | + print(f"Error: Could not connect. {e}") |
| 61 | + raise SystemExit(1) from e |
| 62 | + |
| 63 | +try: |
| 64 | + while True: |
| 65 | + time.sleep(1) |
| 66 | +except KeyboardInterrupt: |
| 67 | + pass |
| 68 | +finally: |
| 69 | + try: |
| 70 | + if iface: |
| 71 | + iface.close() |
| 72 | + except AttributeError: |
| 73 | + pass |
0 commit comments