Working with retained last will messages

Now, we will check what happens when the MQTT client that controls the vehicle disconnects unexpectedly from the MQTT server and our vehicle remote control application disconnects unexpectedly, too. Pay attention to all the steps because we will manually interrupt the execution of both programs to understand a specific problem that we will solve by taking advantage of the last will and testament feature combined with the retained flag value.

You will have to be quick to run the next steps. Hence, make sure you read all the steps and then execute them.

Execute the following line to start the vehicle remote control example on any computer or device that you want to use as the MQTT client and uses Linux or macOS:

    python3 vehicle_mqtt_remote_control.py
  

In Windows, you must execute the following line:

    python vehicle_mqtt_remote_control.py
  

Go to the device and window in which you executed the Python script that controlled the vehicle and processed the received commands, that is, vehicle_mqtt_client.py. After you see the following output, press Ctrl + C to interrupt the execution of the script before all the commands are received:

    Received message payload: b'{"CMD": "PARK_IN_SAFE_PLACE"}'
    vehiclepi01: Parking in safe place
    Received message payload: b'{"CMD": "SET_MAX_SPEED", "MPH": 30}'
    vehiclepi01: Setting maximum speed to 30 MPH

After you press Ctrl + C, you will see an output with a traceback similar to the following lines:

    ^CTraceback (most recent call last):
      File "vehicle_mqtt_client.py", line 198, in <module>
        time.sleep(1)
        KeyboardInterrupt

We interrupted the connection between the MQTT client that controls the vehicle and processes the received commands and the MQTT server. We didn't wait for all the commands to be received and we disconnected the MQTT client unexpectedly from the MQTT server. The vehicle remote control application doesn't know that the remote control application has been interrupted and it waits until the last command it sent is processed.

Go to the device and window in which you executed the previous Python script, named vehicle_mqtt_remote_control.py. Press Ctrl + C  to interrupt the execution of the script. After you press Ctrl + C, you will see an output with a traceback similar to the following lines:

    ^CTraceback (most recent call last):
      File "vehicle_mqtt_remote_control.py", line 93, in <module>
        client.loop()
      File "/Users/gaston/HillarMQTT/01/lib/python3.6/site-
packages/paho/mqtt/client.py", line 988, in loop
socklist = select.select(rlist, wlist, [], timeout) KeyboardInterrupt

Go back to the device and window in which you executed the Python script that controlled the vehicle and processed the received commands, that is, vehicle_mqtt_client.py. Execute the following line to start this script again on any computer or device that you want to use as the MQTT client and uses Linux or macOS:

    python3 vehicle_mqtt_client.py 

In Windows, you must execute the following line:

    python vehicle_mqtt_client.py 

Wait a few seconds and you will only see the following output that indicates the connection to the MQTT server has been accepted. No commands have been received:

Result from connect: Connection Accepted.

The following screenshot shows two Terminal windows running on a computer with macOS. The Terminal on the left-hand side is displaying the messages shown by the Python client that publishes commands and works as the remote control for the vehicle, that is, the vehicle_mqtt_remote_control.py script. The Terminal on the right-hand side is displaying the results of running the code for the Python client that controls the vehicle and processes the received commands, that is, the vehicle_mqtt_client.py script after the previously explained interruptions:

When we started the vehicle_mqtt_client.py script, the code generated a new MQTT client and established a connection with the MQTT server and subscribed to vehicles/vehiclepi01/commands. The last will message published to this topic when we interrupted the execution of the vehicle_mqtt_remote_control.py script had been published with the Retained flag set to False, therefore, the message wasn't retained by the MQTT server and any new subscription to a topic filter that matches the topic to which the retained last will message has been sent doesn't receive it.

Open the existing vehicle_mqtt_remote_control.py Python file and replace the lines that call the client.will_set method within the __main__ method with the following code. The code file for the sample is included in the mqtt_python_gaston_hillar_05_03 folder, in the vehicle_mqtt_remote_control.py file:

    client.will_set(topic=commands_topic,  
        payload=last_will_payload,  
        qos=2, 
        retain=True) 

We specified the True value for the retain argument that, in the previous version of the code, used the default False value. This way, the last will message is going to be a retained message.

Execute the following line to start the vehicle remote control example on any computer or device that you want to use as the MQTT client and uses Linux or macOS:

    python3 vehicle_mqtt_remote_control.py

In Windows, you must execute the following line:

    python vehicle_mqtt_remote_control.py

Go to the device and window in which you executed the Python script that controlled the vehicle and processed the received commands, that is, vehicle_mqtt_client.py. After you see the following output, press Ctrl + C to interrupt the execution of the script before all the commands are received:

    Received message payload: b'{"CMD": "PARK_IN_SAFE_PLACE"}'
    vehiclepi01: Parking in safe place  

After you press Ctrl + C, you will see an output with a traceback similar to the following lines:

^CTraceback (most recent call last):
File "vehicle_mqtt_client.py", line 198, in <module>
time.sleep(1)
KeyboardInterrupt

We interrupted the connection between the MQTT client that controls the vehicle and processes the received commands and the MQTT server. We didn't wait for all the commands to be received and we disconnected the MQTT client unexpectedly from the MQTT server. The vehicle remote control application doesn't know that the remote control application has been interrupted and it waits until the last command it sent is processed.

Go to the device and window in which you executed the previous Python script, named vehicle_mqtt_remote_control.py. Press Ctrl + C to interrupt the execution of the script. After you press Ctrl + C, you will see an output with a traceback similar to the following lines:

    ^CTraceback (most recent call last):
      File "vehicle_mqtt_remote_control.py", line 93, in <module>
        client.loop()
      File "/Users/gaston/HillarMQTT/01/lib/python3.6/site-   
packages/paho/mqtt/client.py", line 988, in loop
socklist = select.select(rlist, wlist, [], timeout) KeyboardInterrupt

Go back to the device and window in which you executed the Python script that controlled the vehicle and processed the received commands, that is, vehicle_mqtt_client.py. Execute the following line to start this script again on any computer or device that you want to use as the MQTT client and uses Linux or macOS:

    python3 vehicle_mqtt_client.py

In Windows, you must execute the following line:

    python vehicle_mqtt_client.py 

Wait a few seconds and you will only see the output which indicates the connection to the MQTT server has been accepted, and a message, which indicates the retained last will message that instructs the vehicle to park in a safe place has been received and processed. Hence, the vehicle will be parked in a safe place:

Result from connect: Connection Accepted.
Received message payload: b'{"CMD": "PARK_IN_SAFE_PLACE"}'
vehiclepi01: Parking in safe place

The following screenshot shows two Terminal windows running on a computer with macOS. The Terminal on the left-hand side is displaying the messages shown by the Python client that publishes commands and works as the remote control for the vehicle, that is, the vehicle_mqtt_remote_control.py script. The Terminal on the right-hand side is displaying the results of running the code for the Python client that controls the vehicle and processes the received commands, that is, the vehicle_mqtt_client.py script after the previously explained interruptions:

With the new code, when we started the vehicle_mqtt_client.py script, the code generated a new MQTT client, established a connection with the MQTT server, and subscribed to the vehicles/vehiclepi01/commands. The last will message published to this topic when we interrupted the execution of the vehicle_mqtt_remote_control.py script had been published with the Retained flag set to True, and therefore, the message was retained by the MQTT server and any new subscription to a topic filter that matches the topic to which the retained last will message has been sent receives it. The retained last will message allows us to make sure that the message arrives as the first message when a new connection to the MQTT server subscribes to a matching topic.

In this case, we always want to make sure that the vehicle receives the last will message if the MQTT client created in the vehicle_mqtt_client.py script loses the connection with the MQTT server and then establishes a new connection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.113