Understanding callbacks
The previous code uses the recently installed paho-mqtt
version 1.3.1 module to establish an encrypted connection with the MQTT server, subscribe to the vehicles/vehiclepi01/tests
topic filter, and run code when we receive messages in the topic. We will use this code to understand the basics of paho-mqtt
. The code is a very simple version of an MQTT client that subscribes to a topic filter and we will definitely improve it in the next sections.
The first line imports the variables we have declared in the previously coded config.py
file. The second line imports paho.mqtt.client
as mqtt
. This way, whenever we use the mqtt
alias, we will be referencing paho.mqtt.client
.
When we declare a function, we pass this function as an argument to another function or method, or we assign this function to an attribute and then some code calls this function at some time; this mechanism is known as a callback. The name callback is used because the code calls back a function at some...