Advertisement
Guest User

Untitled

a guest
Sep 12th, 2018
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2.  
  3.  
  4. MQTT_Broker = "iot.eclipse.org"
  5. MQTT_Port = 1883
  6. Keep_Alive_Interval = 60
  7. MQTT_Topic = 'Errors1'
  8.  
  9. #Subscribe to all Sensors at Base Topic
  10. def on_connect(mosq, obj, rc):
  11. mqttc.subscribe(MQTT_Topic, 0)
  12.  
  13.  
  14. #Save Data into DB Table
  15. def on_message(mosq, obj, msg):
  16. # This is the Master Call for saving MQTT Data into DB
  17. # For details of "sensor_Data_Handler" function please refer "sensor_data_to_db.py"
  18. print("MQTT Data Received...")
  19. print("MQTT Topic: " + msg.topic)
  20. print("Data: " + msg.payload)
  21.  
  22.  
  23. def on_subscribe(mosq, obj, mid, granted_qos):
  24. pass
  25.  
  26. mqttc = mqtt.Client()
  27.  
  28.  
  29. # Assign event callbacks
  30. mqttc.on_message = on_message
  31. mqttc.on_connect = on_connect
  32. mqttc.on_subscribe = on_subscribe
  33.  
  34. # Connect
  35. x = mqttc.connect(MQTT_Broker, int(MQTT_Port), int(Keep_Alive_Interval))
  36.  
  37. print("connected")
  38.  
  39.  
  40.  
  41. # Continue the network loop
  42. mqttc.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement