Answer the question
In order to leave comments, you need to log in
How to convert messages from MQTT broker to SNMP?
Hello. I have a code written in Python that can be used to receive messages from an MQTT broker:
#!/usr/bin/python
import paho.mqtt.client as mqtt
import time
import sys
mqttusername = "***"
mqttpassword = "***"
mqttserver = "***"
mqttport = 1883
subscribe = {
'#',
}
def on_connect(client, userdata, flags, rc):
print('MQTT CONNECTED!')
for topic in subscribe:
print('mqtt subscribe on "' + topic + '"')
client.subscribe(topic, 0)
def on_disconnect(client, userdata, rc):
print("MQTT disconnected with code "+str(rc))
sys.exit(1)
def on_message(client, userdata, msg):
print('mqtt ' + msg.topic + ' ' + msg.payload.decode('utf-8'))
# MQTT
client = mqtt.Client(client_id="mqtt-to-snmp")
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.username_pw_set(mqttusername, mqttpassword)
client.connect(mqttserver, mqttport, 60)
# Update Loop
client.loop_start()
while True:
print('...')
time.sleep(5)
client.loop_stop()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question