meshtastic / python

The Python CLI and API for talking to Meshtastic devices
https://meshtastic.org
379 stars 160 forks source link

Q: in the example, how to loop forever? #114

Closed d33tah closed 2 years ago

d33tah commented 2 years ago

I found the following example in the documentation:

import meshtastic
from pubsub import pub

def onReceive(packet, interface): # called when a packet arrives
    print(f"Received: {packet}")

def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio
    # defaults to broadcast, specify a destination ID if you wish
    interface.sendText("hello mesh")

pub.subscribe(onReceive, "meshtastic.receive")
pub.subscribe(onConnection, "meshtastic.connection.established")
# By default will try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
interface = meshtastic.SerialInterface()

Thing is, it exits immediately. Is there a way to make it loop forever, printing incoming messages as they go?

a-f-G-U-C commented 2 years ago

You can run your main program loop in there (read inputs, print outputs etc), or if there is none, you can do what the example code does - sleep() in an infinite loop:

https://github.com/meshtastic/Meshtastic-python/blob/19613ff53bdc0e33c02f1f0781f4ab67309eec05/meshtastic/__main__.py#L515-L516

mkinney commented 2 years ago

@d33tah Can we close this issue?