Clarification of pub-sub topics in Python API

Second round of questions from me! I assume it’s best to start it in a new thread like this…
Now I’m trying to understand the publish-subscribe topics, and keep thinking in terms of MQTT topics, but those are different. I feel close but can’t quite interpret the definitions over at meshtastic-python.


Q1
The documentation is not clear - can I define my own topics? Or sub-topics at least. It’s looking like the topics are baked in and assigned upon receipt of data or other events. There isn’t a ‘topic’ parameter in the sendText or sendData definition. I believe that means I’ll need to examine the actual data packet to trigger other functions.

Seems like I can define my own handlers to any random topic, but I don’t think that helps…

def bar(packet, interface):
   print(f"bar bar {packet}")
pub.subscribe(bar, "foo")

Q2
meshtastic.connection.lost - What constitutes a lost connection? From playing around with it, it seems that this is triggered when I unplug the USB device from the computer. But I don’t know how to drop it from the mesh network to otherwise test (turning off other radios didn’t do it)


Q3

  • meshtastic.receive.user(packet)
  • meshtastic.receive.data(packet)

These two are not really defined. I see there are .position, .user, and .data under meshtastic.receive.

Is ‘packet’ part of the topic?
Would .data refer to something sent via sendData, or sendText?
Topic .user must be to do with node devices connecting to form the mesh network?

Thank you!

1 Like

Q1: You can define your own topics etc, but only if you want to use pubsub for yourself. the meshtastic lib only uses the topics we list. We are just using a standard python pubsub framework: https://pypubsub.readthedocs.io/en/v4.0.3/

Q2: lost connection simply means your serial (or eventually bluetooth) connection to the local device was dropped, it doesn’t know anything about the mesh. Devices don’t really ‘drop’ from the mesh, rather we just stop hearing from them for a while. You can subscribe to “meshtastic.node.updated” to get notifications when a node changes state. The parameter on the publish is the “node info” describing the current state of that node. Or you can poll/look at the nodes property to see a dictionary of current node infos.

Q3: We receive position, user, or data packets from the mesh. You probably only care about meshtastic.receive.data. The first argument for that publish will be the packet. Text or binary data packets (from sendData or sendText) will both arrive this way. If you print packet you’ll see the fiedls in the dictionary. decoded.data.payload will contain the raw bytes that were sent. If the packet was sent with sendText, decoded.data.text will also be populated with the decoded string. For ASCII these two strings will be the same, but for unicode scripts they can be different.

The prototype of your callback should look like:

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