You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In src/robot/mqtt/init.py :
Provides clear package exports, and requires no modifications.
In src/robot/mqtt/mqtt_msg.py :
MODIFICATION #1: Make _next_id thread-safe to prevent race conditions when multiple threads create messages simultaneously.
Problem: Connection is not guaranteed yet. Should wait for _on_connect callback to confirm success.
MODIFICATION #1: Set _connected = True only after successful connection in _on_connect(). (set _connected = True only in _on_connect())
def _connect(self):
try:
if self.user_name:
self._client.username_pw_set(self.user_name, self.password)
self._client.connect(self.server, self.port, keepalive=60)
self._client.loop_start()
print("Connecting to MQTT broker...")
except Exception as e:
print(f"MQTT connection error: {e}")
def _on_connect(self, client, userdata, flags, rc):
if rc == 0:
self._connected = True
else:
print(f"MQTT: Connection failed with code {rc}")
MODIFICATION #2: Add automatic reconnect in _on_disconnect() to handle network drops.
def _on_disconnect(self, client, userdata, rc):
self._connected = False
print("Connection lost, retrying...")
while not self._connected:
try:
self._client.reconnect()
except Exception:
time.sleep(5)
MODIFICATION #3: Validate QoS values in publish() or subscribe(). By validating the value before sending, the client prevents invalid QoS settings that would otherwise cause broker errors or message delivery failures.
if qos not in [0, 1, 2]:
raise ValueError("QoS must be 0, 1, or 2")
When a custom robot is created as an object, it fails with an exception since topic "v1/localization/update" doesn't exist during the very first instance. Therefore, initially a message had to be published to the mentioned topic before testing #my_test_robot.py in order to work as intended.
Activity
[-]src/robot/mqtt[/-][+]src/robot/mqtt & mqtt_client.py[/+]TharinduHimantha commentedon Nov 13, 2025
Reviewed
Rinali-P commentedon Nov 14, 2025
In src/robot/mqtt/init.py :
Provides clear package exports, and requires no modifications.
In src/robot/mqtt/mqtt_msg.py :
MODIFICATION #1: Make
_next_idthread-safe to prevent race conditions when multiple threads create messages simultaneously.In src/robot/mqtt/robot_mqtt_client.py :
Problem: Connection is not guaranteed yet. Should wait for
_on_connectcallback to confirm success.MODIFICATION #1: Set
_connected = Trueonly after successful connection in_on_connect(). (set_connected = Trueonly in_on_connect())MODIFICATION #2: Add automatic reconnect in
_on_disconnect()to handle network drops.MODIFICATION #3: Validate QoS values in
publish()orsubscribe(). By validating the value before sending, the client prevents invalid QoS settings that would otherwise cause broker errors or message delivery failures.TharinduHimantha commentedon Nov 15, 2025
When a custom robot is created as an object, it fails with an exception since topic "v1/localization/update" doesn't exist during the very first instance. Therefore, initially a message had to be published to the mentioned topic before testing #my_test_robot.py in order to work as intended.