MQTT en Python MQTT:en Python
La bibliothèque Python a été installée sur le Raspberry Pi en même temps que le broker MQTT Mosquitto. La bibliothèque paho-mqtt, anciennement nommée python-mosquitto, permet d’interagir avec le broker Mosquitto. Le projet Eclipse Paho vise à créer des implémentations open source du protocole MQTT pour différents langages de programmation (C, Python, Arduino, Java, JavaScript, C#, etc.). Projet:Eclipse Paho
À défaut d’une installation MQTT complète, il est possible d’installer uniquement la bibliothèque MQTT Python pour Python 2.7 (encore très utilisée sur Raspberry Pi).
sudo pip install paho-mqtt
1. test-mqtt-client-sub.py
Le script de test test-mqtt-client-sub.py indique comment utiliser la bibliothèque MQTT pour réaliser une souscription sur le broker. Python:MQTT souscription
Le script test-mqtt-client-sub.py est disponible sur le référentiel GitHub suivant : https://github.com/mchobby/la-maison-pythonic/tree/master/python/divers
Seul le fichier test-mqtt-client-sub.py doit être transféré sur le Raspberry Pi pour tester la souscription.
01: # coding: utf-8
02: """ Souscription au topic "demo/#" sur le broker Eclipse Mosquitto.
03:
04: Utilise une authentification login/mot-de-passe sur le broker
05: """
06: import paho.mqtt.client as mqtt_client
07: ...