Guest User

Untitled

a guest
Jan 29th, 2026
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import time
  2. import hmac
  3. import hashlib
  4. from paho.mqtt import client as mqtt_client
  5. from paho.mqtt.client import CallbackAPIVersion
  6.  
  7. print("ā–¶ Avvio script MQTT Tuya...")
  8.  
  9. # ===== DATI TUYA =====
  10. ACCESS_ID = "xxx"
  11. ACCESS_SECRET = "xxxx"
  12. DEVICE_ID = "xxx"
  13.  
  14. # ===== BROKER TUYA EU (CORRETTO) =====
  15. BROKER = "mqe.tuyaeu.com"
  16. PORT = 8883
  17. TOPIC = f"/sys/{DEVICE_ID}/thing/event/property/post"
  18.  
  19. print("ā–¶ Genero firma...")
  20.  
  21. timestamp = str(int(time.time() * 1000))
  22. sign_content = ACCESS_ID + timestamp
  23.  
  24. password = hmac.new(
  25. ACCESS_SECRET.encode(),
  26. sign_content.encode(),
  27. hashlib.sha256
  28. ).hexdigest().upper()
  29.  
  30. client_id = f"{ACCESS_ID}|securemode=3,signmethod=hmacsha256,timestamp={timestamp}|"
  31.  
  32. print("ā–¶ Creo client MQTT...")
  33.  
  34. def on_connect(client, userdata, flags, rc, properties=None):
  35. if rc == 0:
  36. print("āœ… CONNESSO A TUYA MQTT")
  37. print("šŸ“” Topic:", TOPIC)
  38. client.subscribe(TOPIC)
  39. else:
  40. print("āŒ Connessione fallita, codice:", rc)
  41.  
  42. def on_message(client, userdata, msg):
  43. print("\nšŸ”” EVENTO RICEVUTO DAL CAMPANELLO")
  44. print(msg.payload.decode())
  45.  
  46. client = mqtt_client.Client(
  47. client_id=client_id,
  48. callback_api_version=CallbackAPIVersion.VERSION2
  49. )
  50.  
  51. client.username_pw_set(ACCESS_ID, password)
  52. client.tls_set()
  53. client.on_connect = on_connect
  54. client.on_message = on_message
  55.  
  56. print("ā–¶ Mi connetto a Tuya...")
  57.  
  58. client.connect(BROKER, PORT)
  59.  
  60. print("ā–¶ In ascolto...")
  61.  
  62. client.loop_forever()
  63.  
Advertisement
Add Comment
Please, Sign In to add comment