Advertisement
SpeakeazyYT

Untitled

Oct 30th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. #!/usr/bin/python
  2. import paho.mqtt.client as mqtt
  3. import netsnmp
  4. import time
  5. import sys
  6. import re
  7.  
  8. mqttusername = "***"
  9. mqttpassword = "***"
  10. mqttserver = "***"
  11. mqttport = 1883
  12.  
  13. subscribe = {'#'}
  14.  
  15. def on_connect(client, userdata, flags, rc):
  16.     print('MQTT CONNECTED!')
  17.     for topic in subscribe:
  18.         print('mqtt subscribe on "' + topic + '"')
  19.         client.subscribe(topic, 0)
  20.  
  21. def on_disconnect(client, userdata, rc):
  22.     print("MQTT disconnected with code "+str(rc))
  23.     sys.exit(1)
  24.    
  25. def on_message(client, userdata, msg):
  26.     print(msg.topic + ' ' + msg.payload.decode('utf-8'))
  27.  
  28. # MQTT
  29. client = mqtt.Client(client_id="mqtt-to-snmp")
  30. client.on_connect = on_connect
  31. client.on_message = on_message
  32. client.on_disconnect = on_disconnect
  33. client.username_pw_set(mqttusername, mqttpassword)
  34. client.connect(mqttserver, mqttport, 60)
  35.  
  36. # Update Loop
  37. client.loop_start()
  38. while True:
  39.     print('...')
  40.     time.sleep(5)
  41. client.loop_stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement