Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import hmac
- import hashlib
- from paho.mqtt import client as mqtt_client
- from paho.mqtt.client import CallbackAPIVersion
- print("ā¶ Avvio script MQTT Tuya...")
- # ===== DATI TUYA =====
- ACCESS_ID = "xxx"
- ACCESS_SECRET = "xxxx"
- DEVICE_ID = "xxx"
- # ===== BROKER TUYA EU (CORRETTO) =====
- BROKER = "mqe.tuyaeu.com"
- PORT = 8883
- TOPIC = f"/sys/{DEVICE_ID}/thing/event/property/post"
- print("ā¶ Genero firma...")
- timestamp = str(int(time.time() * 1000))
- sign_content = ACCESS_ID + timestamp
- password = hmac.new(
- ACCESS_SECRET.encode(),
- sign_content.encode(),
- hashlib.sha256
- ).hexdigest().upper()
- client_id = f"{ACCESS_ID}|securemode=3,signmethod=hmacsha256,timestamp={timestamp}|"
- print("ā¶ Creo client MQTT...")
- def on_connect(client, userdata, flags, rc, properties=None):
- if rc == 0:
- print("ā CONNESSO A TUYA MQTT")
- print("š” Topic:", TOPIC)
- client.subscribe(TOPIC)
- else:
- print("ā Connessione fallita, codice:", rc)
- def on_message(client, userdata, msg):
- print("\nš EVENTO RICEVUTO DAL CAMPANELLO")
- print(msg.payload.decode())
- client = mqtt_client.Client(
- client_id=client_id,
- callback_api_version=CallbackAPIVersion.VERSION2
- )
- client.username_pw_set(ACCESS_ID, password)
- client.tls_set()
- client.on_connect = on_connect
- client.on_message = on_message
- print("ā¶ Mi connetto a Tuya...")
- client.connect(BROKER, PORT)
- print("ā¶ In ascolto...")
- client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment