PubSub and Meshtastic Callback issues
I don’t know why, but you cannot use a closure for callbacks when subscribing to Meshtastic events. It took me days to figure out. Also, the parameter name in pubsub callbacks really matter. Even for positional arguments.
closures are broken
This doesn’t work:
import meshtastic
from pubsub import pub
class Foo:
def start(self):
def _on_recv(packet, interface):
pass # body here to do something with packet
pub.subscribe(_on_recv, "meshtastic.receive")
self.interface = meshtastic.serial_interface.SerialInterface("/dev/ttyACM0")
Or more precisely, it will work right up until it exits from the start method. Then, mysteriously, it will no longer work.
Instead, it needs to be more like this:
import meshtastic
from pubsub import pub
class Foo:
def on_recv(self, packet, interface):
pass # body here to do something with the packet
def start(self):
pub.subscribe(self.on_recv, "meshtastic.receive")
self.interface = meshtastic.serial_interface.SerialInterface("/dev/ttyACM0")
names matter
Another head-scratcher: when subscribing to “meshtastic.connection.established” you MUST name the arguments exactly.
This works:
def on_connect(self, interface, topic=pub.AUTO_TOPIC):
pass # do something here
But this does not work:
def on_connect(self, iface, topic=pub.AUTO_TOPIC):
pass # do something here
And the exception is weird:
ERROR:root:Unexpected error in deferred executionTraceback (most recent call last): File "/nix/store/lz6fkq96sgg6xv9gb6a0wjv446mhnkm1-python3.11-meshtastic-2.3.4/lib/python3.11/site-packages/meshtastic/util.py", line 244, in _run o() File "/nix/store/lz6fkq96sgg6xv9gb6a0wjv446mhnkm1-python3.11-meshtastic-2.3.4/lib/python3.11/site-packages/meshtastic/mesh_interface.py", line 650, in lambda: pub.sendMessage( ^^^^^^^^^^^^^^^^ File "/nix/store/rhzqg6ih8wmmh6sfpk43vbxw050gbscz-python3.11-pypubsub-4.0.3/lib/python3.11/site-packages/pubsub/core/publisher.py", line 216, in sendMessage topicObj.publish(**msgData) File "/nix/store/rhzqg6ih8wmmh6sfpk43vbxw050gbscz-python3.11-pypubsub-4.0.3/lib/python3.11/site-packages/pubsub/core/topicobj.py", line 433, in publish self._getListenerSpec().check(msgData) File "/nix/store/rhzqg6ih8wmmh6sfpk43vbxw050gbscz-python3.11-pypubsub-4.0.3/lib/python3.11/site-packages/pubsub/core/topicargspec.py", line 222, in check raise SenderMissingReqdMsgDataError( pubsub.core.topicargspec.SenderMissingReqdMsgDataError: Some required args missing in call to sendMessage('meshtastic.connection.established', interface): iface
Tags: python, index, meshtastic
Tags
Navigation
created: 2024-09-14
(re)generated: 2025-11-27