new mqtt-python
This commit is contained in:
parent
c463b39c56
commit
2d622688e6
55
mqtt-python
55
mqtt-python
|
@ -1,55 +0,0 @@
|
|||
import paho.mqtt.client as mqttClient
|
||||
from influxdb import InfluxDBClient
|
||||
import time
|
||||
|
||||
TOPIC = "test1"
|
||||
broker_address= "localhost"
|
||||
port = 1883
|
||||
database = "thermo-bibli"
|
||||
nom_client = "capteurs1"
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print("Connected with result code "+str(rc))
|
||||
client.subscribe(TOPIC, 2)
|
||||
|
||||
def addData(database, time, numero_grappe, numero_capteur, temp, hum, batterie): # ajoute donnée à base de donnée influxDB
|
||||
client = InfluxDBClient(host='localhost', port=8086)
|
||||
client.create_database(database)
|
||||
client.switch_database(database)
|
||||
data = [
|
||||
{
|
||||
"measurement": numero_grappe,
|
||||
"tags": {"numero_capteur": numero_capteur},
|
||||
"time": time,
|
||||
"fields": {"temperature": temp, "humidite": hum, "batterie": batterie}
|
||||
|
||||
}
|
||||
]
|
||||
client.write_points(data)
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
message = (message.payload).decode("utf-8") # convertit type bytes en string
|
||||
print("Message reçu")
|
||||
tab = message.split("|")
|
||||
temp = tab[2].split()
|
||||
hum = tab[3].split()
|
||||
batterie = tab[4]
|
||||
L = len(temp)
|
||||
for i in range(L):
|
||||
addData(database, tab[0], tab[1], "capteur" + str(i+1), temp[i], hum[i], batterie)
|
||||
|
||||
client = mqttClient.Client(nom_client, False)
|
||||
client.username_pw_set(username="capteurs",password="Fablab")
|
||||
client.on_connect= on_connect #callback
|
||||
client.on_message= on_message #callback
|
||||
client.connect(broker_address, port, 65535)
|
||||
client.loop_start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(0.1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print ("exiting")
|
||||
client.disconnect()
|
||||
client.loop_stop()
|
95
mqtt-python.py
Normal file
95
mqtt-python.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
#lib pour faire le timestamp
|
||||
#from datetime import datetime
|
||||
|
||||
#lib pour ecoute mqtt
|
||||
import paho.mqtt.client as mqttClient
|
||||
import time
|
||||
import sys
|
||||
|
||||
#http request lib
|
||||
import requests
|
||||
from aiohttp import ClientSession
|
||||
|
||||
##################################################################################################
|
||||
|
||||
#var mqtt
|
||||
TOPIC = "test"
|
||||
broker_address= "localhost"
|
||||
port = 1883
|
||||
nom_client = "serveur-test"
|
||||
|
||||
#var influx
|
||||
influx_bucket = "Fablab" #database
|
||||
influx_token = "QRzx929_X67sunqmY6xpS65fM7ff7Gy_Fz4ZD2qAT7eaR8Jps-9CZtEkot5Z01CQ-q1tq87IRyahXGebsSEzxV==" #token
|
||||
influx_org="Fablab"
|
||||
influx_url=f"http://localhost:8086/api/v2/write?org={influx_org}&bucket={influx_bucket}&precision=s"
|
||||
|
||||
#########################################################################################################
|
||||
|
||||
#http
|
||||
async def get(session: object, url: object) -> object:
|
||||
async with session.get(url) as response:
|
||||
return await response.text()
|
||||
|
||||
#enregistre dans un fichier pour debug
|
||||
def log(time):
|
||||
f = open("/var/log/mqtt-influxdb/date.txt", "a")
|
||||
f.write(time + "\n")
|
||||
f.close()
|
||||
|
||||
def dump_log(text):
|
||||
f = open("/var/log/mqtt-influxdb/dumped_message_thermo-bibli_test", "a")
|
||||
f.write(text + "\n")
|
||||
f.close()
|
||||
|
||||
#envois les donnees a influx via l'apiv2
|
||||
def addData(time, numero_grappe, numero_capteur, temp, hum, batterie): # ajoute donnée à base de donnée influxDB
|
||||
Headers = {"Authorization": f"Token {influx_token}"}
|
||||
|
||||
#data envoye a l'api
|
||||
Data=f"{numero_grappe},numero_capteur={numero_capteur} temperature={temp},humidite={hum},batterie={batterie} {time}"
|
||||
response = requests.post(influx_url, headers=Headers, data=Data)
|
||||
|
||||
#debug api, fonctionne correctement malgres le code retour 204, quand il y a un code 204 le json response fait une erreur
|
||||
#print("Status Code", response.status_code)
|
||||
#print("JSON Response ", response.json())
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print("Connected with result code "+str(rc))
|
||||
client.subscribe(TOPIC, 2)
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
message = (message.payload).decode("utf-8") # convertit type bytes en string
|
||||
print("Message reçu")
|
||||
tab = message.split("|")
|
||||
temp = tab[2].split()
|
||||
hum = tab[3].split()
|
||||
batterie = tab[4]
|
||||
L = len(temp)
|
||||
time=tab[0]
|
||||
|
||||
#envois la valeur temps reçu pour le debug
|
||||
#log(time + " reçu")
|
||||
for i in range(L):
|
||||
addData(time, tab[1], "capteur" + str(i+1), temp[i], hum[i], batterie)
|
||||
#log("---------")
|
||||
|
||||
#dump_log(message)
|
||||
|
||||
client = mqttClient.Client(nom_client, False)
|
||||
client.username_pw_set(username="capteurs",password="Fablab")
|
||||
client.on_connect= on_connect #callback
|
||||
client.on_message= on_message #callback
|
||||
client.connect(broker_address, port, 65535)
|
||||
client.loop_start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(0.1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print ("exiting")
|
||||
client.disconnect()
|
||||
client.loop_stop()
|
Loading…
Reference in a new issue