0

I´m trying to connect to a mosquitto broker using autobahn python. If I use sub.py that has this code inside:

import mosquitto

def on_connect(mosq, obj, rc):
    print("rc: "+str(rc))

def on_message(mosq, obj, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

def on_publish(mosq, obj, mid):
    print("mid: "+str(mid))

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))

def on_log(mosq, obj, level, string):
    print(string)

mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
mqttc.on_log = on_log
mqttc.connect("localhost", 1883, 60)
mqttc.subscribe("control", 0)


rc = 0
while rc == 0:
    rc = mqttc.loop()

print("rc: "+str(rc))

it is connecting to the broker and retrieving all messages that a client publish to control channel.

I´d like to push somehow those messages using websockets to a webpage for that I am trying to use websocket autobahn py and modify the example from here http://autobahn.ws/python/getstarted#yourfirstserver

My code is like this

import sys
import mosquitto

from twisted.internet import reactor
from twisted.python import log

from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS


class EchoServerProtocol(WebSocketServerProtocol):
    def on_connect(mosq, obj, rc):
    print("rc: "+str(rc))

    def on_message(mosq, obj, msg):
        print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

    def onMessage(self, msg, binary):
      print "sending echo:", msg
      self.sendMessage(msg, binary)

    def on_publish(mosq, obj, mid):
        print("mid: "+str(mid))

    def on_subscribe(mosq, obj, mid, granted_qos):
        print("Subscribed: "+str(mid)+" "+str(granted_qos))

mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
mqttc.on_log = on_log
mqttc.connect("192.168.2.109", 1883, 60)
mqttc.subscribe("control", 0)


rc = 0
while rc == 0:
    rc = mqttc.loop()

print("rc: "+str(rc))   
if __name__ == '__main__':

   log.startLogging(sys.stdout)

   factory = WebSocketServerFactory("ws://192.168.2.109:8899", debug = TRUE)
   factory.protocol = EchoServerProtocol
   listenWS(factory)

   reactor.run()

but I receive this error when I try to run it:

root@Ubuntu:~/authobahn# python myserver.py Traceback (most recent call last): File "myserver.py", line 30, in mqttc.on_message = on_message NameError: name 'on_message' is not defined

1 Answer 1

0

The problem is that you have defined your on_message() function inside the EchoServerProtocol class. This means it is not visible to the global mqttc variable. You probably want to put all of the mqttc code inside your class as well, although it depends on what you actually want to achieve.

You could do something like the code below:

import mosquitto

class MyMQTTClass:
    def __init__(self, clientid=None):
        self._mqttc = mosquitto.Mosquitto()
        self._mqttc.on_message = self.mqtt_on_message
        self._mqttc.on_connect = self.mqtt_on_connect
        self._mqttc.on_publish = self.mqtt_on_publish
        self._mqttc.on_subscribe = self.mqtt_on_subscribe

    def mqtt_on_connect(self, mosq, obj, rc):
        print("rc: "+str(rc))

    def mqtt_on_message(self, mosq, obj, msg):
        print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

    def mqtt_on_publish(self, mosq, obj, mid):
        print("mid: "+str(mid))

    def mqtt_on_subscribe(self, mosq, obj, mid, granted_qos):
        print("Subscribed: "+str(mid)+" "+str(granted_qos))

    def mqtt_on_log(self, mosq, obj, level, string):
        print(string)

    def run(self):
        self._mqttc.connect("test.mosquitto.org", 1883, 60)
        self._mqttc.subscribe("$SYS/#", 0)

        rc = 0
        while rc == 0:
            rc = self._mqttc.loop()
        return rc


# If you want to use a specific client id, use
# mqttc = MyMQTTClass("client-id")
# but note that the client id must be unique on the broker. Leaving the client
# id parameter empty will generate a random id for you.
mqttc = MyMQTTClass()
rc = mqttc.run()

print("rc: "+str(rc))
1
  • Thsnk you for the replay. In did that was the problem. Jun 26, 2013 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.