#mqtt_Thread
Connexion à un server mqtt via un thread
Envoi et réception des messages mqtt en Python
Sauvegarde des données reçues au format csv
INSTALLATION :
py -m pip install mqtt_thread
ou
py -m pip install mqtt_thread==x.x.x (mettre numéro de version)
exemple :
Trace de graphe mathPlotlib temps réel
---------------------
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
from matplotlib.ticker import FormatStrFormatter
import numpy as np
import time
import mqtt_thread as MQTT # bibliothèque pypi
Creation de la figure pour la charge
fig = plt.figure(num=1, figsize=(10, 6), dpi=120, facecolor='w', edgecolor='k')# configuration du graphe
ax = fig.add_subplot(1, 1, 1)
labelGraphe = "Titre du graphe"
def messageArrived(): # fonction appelée par le thread MQTT quand un message arrive
if m.verbose:
print("msg arrivé")
try:
print("temp : ",m.lastLinedata[1])
except:
print("pas de dernière ligne")
def acquisition(i, xs, ys):
# Dessine les graphes
ax.clear()
ax.plot(xs, ys, 'r-o', label=labelGraphe)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
# Format du graphe
plt.xticks(rotation=45, ha='right')# rotation et alignement du texte axe x
plt.subplots_adjust(bottom=0.20)
plt.title('Graphe temps réel avec Python')
plt.ylabel('Valeur 1')
plt.xlabel('temps (s)')
plt.legend()
plt.grid()# la grille sera affichée
m = MQTT.MQTT_Thread("mqtt.url.fr",443,"login","pwd") # crée le thread
m.selectTopic(["node_iot2020/#","FABLAB_21_22/contes/bureau/temperature/out/"]) # topics auxquels on s'abonne
m.selectKey([["ecl","temps"],["temp","pression"]]) # selection des clés des données voulues , les données seront dans m.data[0][], m.data[1][],...
le dernier élement de m.data[i] est le temps de réception du message en secondes (donné par python avec la fonction time)
m.messageArrived = messageArrived # personnalisation fonction qui sera appelée à chaque réception de message
m.nomFichier = "donnees" # nom du fichier csv sans extension qui enregistrera les données
m.record = True # enregistre les données dans un fichier csv
m.eraseFile = True # efface le fichier csv avant d'enregistrer les données
m.verbose = True # affiche les messages MQTT
m.start() # démarre le thread, (exécution indépendante du programme principal)
time.sleep(1)
#m.client.publish("node_iot2020/test/in",payload="{"pression":1024}",qos=0)#publication d'un message vers MQTT
Appelle la fonction animation périodiquement
#m.data[0][1] est une liste !!!
ani = animation.FuncAnimation(fig, acquisition, fargs=(m.data[1][2], m.data[1][0]), frames=20,interval=1000,repeat=True)# intervall temps en ms entre 2 animations
plt.show()
Metadata
Release files for mqtt-thread 1.6.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mqtt_thread-1.6.5.tar.gz | 6.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mqtt_thread-1.6.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.1 kB
Release files / mqtt_thread-1.6.5.tar.gz
| Download URL | mqtt_thread-1.6.5.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5bcd68230f82e5674a4addc488d176f4d46575328550fac6017d0018afb91cfb
|
|
BLAKE2b-256 checksum How to use checksums |
b0c63f06630f382fc59f5dcdf00bc3b7429367dd5f02c8107d058ba94e4f417f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|
Release files / mqtt_thread-1.6.5-py3-none-any.whl
| Download URL | mqtt_thread-1.6.5-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d9c453cc268f6cd4d8f55ce2e2849fca04e6c32fc2169426220dbf1ea303d161
|
|
BLAKE2b-256 checksum How to use checksums |
d6883b2697840049186d152185faa3fd486c6018b05795279ae14789efecaee5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|