|PDF Title :
|Internet of Things with Python
|Total Page :
|Gaston C. Hillar
|PDF Size :
|PDF Link :
Here on this page, we have provided the latest download link for Internet of Things with Python PDF. Please feel free to download it on your computer/mobile. For further reference, you can go to packtpub.com
Internet of Things with Python – Book
Whenever there is a new message received in the topic to which we have subscribed, the specified callback in the self.client.on_messsage attribute will be executed, that is, the on_message static method (marked with the @staticmethod decorator). This static method receives the mqtt.Client instance that established the connection with the MQTT broker in the client argument and an mqtt.MQTTMessage instance in the msg argument.
The mqtt.MQTTMessage class describes an incoming message. First, the static method checks whether the msg.topic attribute, that indicates the topic in which the message has been received, matches the value in the topic class attribute. In this case, whenever the on_message method is executed, the value in msg.topic will always match the value in the topic class attribute because we just subscribed to one topic. However, in case we subscribe to more than one topic, is is always necessary to check which is the topic in which the message was sent and in which we are receiving the message.
The code prints the message that has been received, that is, the msg.payload attribute. Then, the code assigns the result of the json.loads function to deserialize msg.payload to a Python object and assigns the results to the message_dictionary local variable. In case the contents of msg.payload are not JSON, a ValueError exception will be captured and no more code will be executed in the method. In case the contents of msg.payload are JSON, we will have a dictionary in the message_dictionary local variable.
Internet of Things with Python PDF
Know more about our initiative