Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ client.connect(
password='large4cats',
key='AQ=='
)

# Start MQTT client loop until interrupted
# If you want your script to perform other activities with the MQTT loop in another thread.
# look at client.loop_start()
client.loop_forever()
```

### Callback System
Expand Down
43 changes: 28 additions & 15 deletions src/meshtastic_mqtt_json/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class MeshtasticMQTT(object):
def __init__(self):
'''Initialize the Meshtastic MQTT client'''

self.broadcast_id = 4294967295 # Our channel ID
self.key = None
self.names = {}
self.filters = None
self.callbacks = {} # Dictionary to store message type callbacks
self.client = None
self.broadcast_id = 4294967295 # Our channel ID
self.key = None
self.names = {}
self.filters = None
self.callbacks = {} # Dictionary to store message type callbacks
self.shutting_down = False


def register_callback(self, message_type: str, callback: callable):
Expand Down Expand Up @@ -113,11 +115,11 @@ def connect(self, broker: str, port: int, root: str, channel: str, username: str
'''

# Initialize the MQTT client
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id='', clean_session=True, userdata=None)
client.connect_timeout = 10
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id='', clean_session=True, userdata=None)
self.client.connect_timeout = 10

# Set the username and password for the MQTT broker
client.username_pw_set(username=username, password=password)
self.client.username_pw_set(username=username, password=password)

# Set the encryption key
self.key = '1PG7OiApB1nwvP+rz05pAQ==' if key == 'AQ==' else key
Expand All @@ -132,23 +134,32 @@ def connect(self, broker: str, port: int, root: str, channel: str, username: str
raise

# Set the MQTT callbacks
client.on_connect = self.event_mqtt_connect
client.on_message = self.event_mqtt_recv
client.on_disconnect = self.event_mqtt_disconnect
self.client.on_connect = self.event_mqtt_connect
self.client.on_message = self.event_mqtt_recv
self.client.on_disconnect = self.event_mqtt_disconnect

# Connect to the MQTT broker
try:
client.connect(broker, port, 60)
self.client.connect(broker, port, 60)
except Exception as e:
print(f'Error connecting to MQTT broker: {e}')
self.event_mqtt_disconnect(client, '', 1, None)
self.event_mqtt_disconnect(self.client, '', 1, None)

# Set the subscribe topic
self.subscribe_topic = f'{root}{channel}/#'

# Keep-alive loop
client.loop_forever()
def disconnect(self):
self.shutting_down = True
self.client.disconnect()

def loop_forever(self):
self.client.loop_forever()

def loop_start(self):
self.client.loop_start()

def loop_stop(self):
self.client.loop_stop()

def decrypt_message_packet(self, mp):
'''
Expand Down Expand Up @@ -413,6 +424,8 @@ def event_mqtt_recv(self, client, userdata, msg):
def event_mqtt_disconnect(self, client, userdata, rc, packet_from_broker=None, properties=None, reason_code=None):
'''Callback for when the client disconnects from the server.'''
print(f'Disconnected with result code: {rc}')
if self.shutting_down:
return
while True:
print('Attempting to reconnect...')
try:
Expand Down