Using the threaded client interface
Now, we will write a new version of the vehicle remote control application to use the threaded interface, also known as the threaded loop. Open the existing vehicle_mqtt_remote_control.py
Python file and replace the lines that define the publish_command
function with the following lines. The code file for the sample is included in the mqtt_python_gaston_hillar_05_04
folder, in the vehicle_mqtt_remote_control.py
file:
def publish_command(client, command_name, key="", value=""): command_message = build_command_message( command_name, key, value) result = client.publish(topic=commands_topic, payload=command_message, qos=2) time.sleep(1) return result
We removed the following line before the call to time.sleep(1)
:
client.loop()
The threaded loop will automatically call client.loop
in another thread, and therefore, we don't need to include a call to client.loop
within the publish_command
method anymore.
Open the existing vehicle_mqtt_remote_control...