Actualiser mqtt-python.py
This commit is contained in:
parent
42f7f3331d
commit
8c9f015b08
|
@ -3,7 +3,7 @@
|
||||||
#lib pour faire le timestamp
|
#lib pour faire le timestamp
|
||||||
#from datetime import datetime
|
#from datetime import datetime
|
||||||
|
|
||||||
#lib pour ecoute mqtt
|
#lib pour ecoute mqtt
|
||||||
import paho.mqtt.client as mqttClient
|
import paho.mqtt.client as mqttClient
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
@ -15,16 +15,16 @@ from aiohttp import ClientSession
|
||||||
##################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
#var mqtt
|
#var mqtt
|
||||||
TOPIC = "test"
|
TOPIC = "test"
|
||||||
broker_address= "localhost"
|
broker_address= "localhost"
|
||||||
port = 1883
|
port = 1883
|
||||||
nom_client = "serveur-test"
|
nom_client = "serveur-test"
|
||||||
|
|
||||||
#var influx
|
#var influx
|
||||||
influx_bucket = "Fablab" #database
|
influx_bucket = "Fablab" #database
|
||||||
influx_token = "QRzx929_X67sunqmY6xpS65fM7ff7Gy_Fz4ZD2qAT7eaR8Jps-9CZtEkot5Z01CQ-q1tq87IRyahXGebsSEzxV==" #token
|
influx_token = "QRzx929_X67sunqmY6xpS65fM7ff7Gy_Fz4ZD2qAT7eaR8Jps-9CZtEkot5Z01CQ-q1tq87IRyahXGebsSEzxV==" #token
|
||||||
influx_org="Fablab"
|
influx_org="Fablab"
|
||||||
influx_url=f"http://localhost:8086/api/v2/write?org={influx_org}&bucket={influx_bucket}&precision=s"
|
influx_url=f"http://localhost:8086/api/v2/write?org={influx_org}&bucket={influx_bucket}&precision=s"
|
||||||
|
|
||||||
#########################################################################################################
|
#########################################################################################################
|
||||||
|
|
||||||
|
@ -49,9 +49,13 @@ def addData(time, numero_grappe, numero_capteur, temp, hum, batterie): # ajoute
|
||||||
Headers = {"Authorization": f"Token {influx_token}"}
|
Headers = {"Authorization": f"Token {influx_token}"}
|
||||||
|
|
||||||
#data envoye a l'api
|
#data envoye a l'api
|
||||||
Data=f"{numero_grappe},numero_capteur={numero_capteur} temperature={temp},humidite={hum},batterie={batterie} {time}"
|
int_time = int(time)
|
||||||
|
#if (int_time > 1817589156):
|
||||||
|
# time = str(int_time - 368384901)
|
||||||
|
Data=f"{numero_grappe},numero_capteur={numero_capteur} temperature={temp},humidite={hum},batterie={batterie} {time}"
|
||||||
|
print(Data)
|
||||||
response = requests.post(influx_url, headers=Headers, data=Data)
|
response = requests.post(influx_url, headers=Headers, data=Data)
|
||||||
|
print(response.status_code)
|
||||||
#debug api, fonctionne correctement malgres le code retour 204, quand il y a un code 204 le json response fait une erreur
|
#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("Status Code", response.status_code)
|
||||||
#print("JSON Response ", response.json())
|
#print("JSON Response ", response.json())
|
||||||
|
@ -71,19 +75,20 @@ def on_message(client, userdata, message):
|
||||||
time=tab[0]
|
time=tab[0]
|
||||||
|
|
||||||
#envois la valeur temps reçu pour le debug
|
#envois la valeur temps reçu pour le debug
|
||||||
#log(time + " reçu")
|
#log(time + " reçu")
|
||||||
for i in range(L):
|
for i in range(L):
|
||||||
addData(time, tab[1], "capteur" + str(i+1), temp[i], hum[i], batterie)
|
addData(time, tab[1], "capteur" + str(i+1), temp[i], hum[i], batterie)
|
||||||
#log("---------")
|
#log("---------")
|
||||||
|
|
||||||
|
#envois la valeur message pour le debug
|
||||||
#dump_log(message)
|
#dump_log(message)
|
||||||
|
|
||||||
client = mqttClient.Client(nom_client, False)
|
client = mqttClient.Client(nom_client, False)
|
||||||
client.username_pw_set(username="capteurs",password="Fablab")
|
client.username_pw_set(username="capteurs",password="Fablab")
|
||||||
client.on_connect= on_connect #callback
|
client.on_connect= on_connect #callback
|
||||||
client.on_message= on_message #callback
|
client.on_message= on_message #callback
|
||||||
client.connect(broker_address, port, 65535)
|
client.connect(broker_address, port, 65535)
|
||||||
client.loop_start()
|
client.loop_start()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
@ -93,3 +98,11 @@ except KeyboardInterrupt:
|
||||||
print ("exiting")
|
print ("exiting")
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
client.loop_stop()
|
client.loop_stop()
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print ("exiting")
|
||||||
|
client.disconnect()
|
||||||
|
client.loop_stop()
|
Loading…
Reference in a new issue