1er commit

This commit is contained in:
Gabriel
2024-05-07 10:31:34 +02:00
commit 80f30d90a3
4 changed files with 65 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
mqtt_config.json

11
README.md Normal file
View File

@@ -0,0 +1,11 @@
# Programme test MQTT
- S'abonne à un topic
- Récupère la partie "data" du JSON
- Parse et afficher les données encodées en Cayenne LPP puis en base64
## Usage
- Copier le fichier mqtt_config_sample.json en mqtt_config.json
- Éxécuter le script

47
mqtt.py Normal file
View File

@@ -0,0 +1,47 @@
import paho.mqtt.client as mqtt
import json
import base64
from cayennelpp import LppFrame
import datetime
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT broker")
client.subscribe(topic)
else:
print("Failed to connect, return code %d\n", rc)
def on_message(client, userdata, message):
#print("Received message: ", str(message.payload.decode("utf-8")))
parsed_json = json.loads(str(message.payload.decode("utf-8")))
data_part = parsed_json["data"]
# print("Data:", data_part)
decoded_data = base64.b64decode(data_part)
#print("data :", decoded_data.hex())
bytearray_data = bytearray.fromhex(decoded_data.hex())
frame = LppFrame().from_bytes(bytearray_data)
print(datetime.datetime.now())
for d in frame.data:
print(d)
print("---------------------------------")
def read_config(config_file):
with open(config_file, 'r') as f:
config = json.load(f)
return config
if __name__ == "__main__":
config = read_config('mqtt_config.json')
broker_address = config['broker_address']
port = config['port']
topic = config['topic']
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_address, port)
client.loop_forever()

6
mqtt_config_sample.json Normal file
View File

@@ -0,0 +1,6 @@
{
"broker_address": "",
"port": 1883,
"topic": ""
}