Looking for MQTT PUBACK from on_log callback using within a class fails, but it works in a flat script












0















The first script works, meaning the callbacks are called and it ends printing puback: True



The second script where I use class A to do the work does not work. Callbacks are not called, and it ends with a.puback: False



I'm not sure if my problem is that callbacks don't work this way, in which case how can I get my class to work with these Paho MQTT callbacks? or if it's something more subtle.



WORKS:



def on_log_puback(client, userdata, level, buf):
global puback
if 'PUBACK' in buf:
puback = True
print "PUBACK!"

def on_connect(client, userdata, flags, rc):
print "Connect code: ", rc

def on_disconnect(client, userdata, flags, rc=0):
print "Disconnect code: ", rc

def on_message(client, userdata, msg):
print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

def stop():
print ("stopping loop")
client.loop_stop()
print "disconnecting"
client.disconnect()

import paho.mqtt.client as mqtt
import time

client = mqtt.Client("Luke, I am your client")
mqtt.Client.connected_flag = False

client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_log = on_log_puback
client.on_message = on_message

status = client.connect(host="test.mosquitto.org",
keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)

print "loop_start"
client.loop_start()
time.sleep(1)

sret = client.subscribe("test_topic")
print "subscribe returns sret: ", sret
time.sleep(2)

# initialize global
puback = False

# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
payload="hello!",
qos=1, retain=True)
print "publish status: ", status
time.sleep(2)

print "puback: ", puback

stop()


DOESN'T WORK:



import paho.mqtt.client as mqtt 
import time

class A(object):
def __init__(self):

client = mqtt.Client("Luke, I am your client")
self.client = client
mqtt.Client.connected_flag = False

client.on_connect = self.on_connect
client.on_disconnect = self.on_disconnect
client.on_log = self.on_log_puback
client.on_message = self.on_message

status = client.connect(host="test.mosquitto.org",
keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)

print "loop_start"
client.loop_start()
time.sleep(1)

sret = client.subscribe("test_topic")
print "subscribe returns: ", sret
time.sleep(2)

# initialize global
puback = False

# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
payload="hello!",
qos=1, retain=True)
print "publish status: ", status
time.sleep(2)

self.puback = puback

def on_log_puback(client, userdata, level, buf):
global puback
if 'PUBACK' in buf:
puback = True
print "PUBACK!"

def on_connect(client, userdata, flags, rc):
print "Connect code: ", rc

def on_disconnect(client, userdata, flags, rc=0):
print "Disconnect code: ", rc

def on_message(client, userdata, msg):
print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

def stop(self):
print ("stopping loop")
self.client.loop_stop()
print "disconnecting"
self.client.disconnect()

a = A()

time.sleep(2)
print 'a.puback: ', a.puback
a.stop()









share|improve this question





























    0















    The first script works, meaning the callbacks are called and it ends printing puback: True



    The second script where I use class A to do the work does not work. Callbacks are not called, and it ends with a.puback: False



    I'm not sure if my problem is that callbacks don't work this way, in which case how can I get my class to work with these Paho MQTT callbacks? or if it's something more subtle.



    WORKS:



    def on_log_puback(client, userdata, level, buf):
    global puback
    if 'PUBACK' in buf:
    puback = True
    print "PUBACK!"

    def on_connect(client, userdata, flags, rc):
    print "Connect code: ", rc

    def on_disconnect(client, userdata, flags, rc=0):
    print "Disconnect code: ", rc

    def on_message(client, userdata, msg):
    print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

    def stop():
    print ("stopping loop")
    client.loop_stop()
    print "disconnecting"
    client.disconnect()

    import paho.mqtt.client as mqtt
    import time

    client = mqtt.Client("Luke, I am your client")
    mqtt.Client.connected_flag = False

    client.on_connect = on_connect
    client.on_disconnect = on_disconnect
    client.on_log = on_log_puback
    client.on_message = on_message

    status = client.connect(host="test.mosquitto.org",
    keepalive=60, port=1883)
    print "connect status: ", status
    time.sleep(2)

    print "loop_start"
    client.loop_start()
    time.sleep(1)

    sret = client.subscribe("test_topic")
    print "subscribe returns sret: ", sret
    time.sleep(2)

    # initialize global
    puback = False

    # test publish with qos=1
    status, msg_id = client.publish(topic="test_topic",
    payload="hello!",
    qos=1, retain=True)
    print "publish status: ", status
    time.sleep(2)

    print "puback: ", puback

    stop()


    DOESN'T WORK:



    import paho.mqtt.client as mqtt 
    import time

    class A(object):
    def __init__(self):

    client = mqtt.Client("Luke, I am your client")
    self.client = client
    mqtt.Client.connected_flag = False

    client.on_connect = self.on_connect
    client.on_disconnect = self.on_disconnect
    client.on_log = self.on_log_puback
    client.on_message = self.on_message

    status = client.connect(host="test.mosquitto.org",
    keepalive=60, port=1883)
    print "connect status: ", status
    time.sleep(2)

    print "loop_start"
    client.loop_start()
    time.sleep(1)

    sret = client.subscribe("test_topic")
    print "subscribe returns: ", sret
    time.sleep(2)

    # initialize global
    puback = False

    # test publish with qos=1
    status, msg_id = client.publish(topic="test_topic",
    payload="hello!",
    qos=1, retain=True)
    print "publish status: ", status
    time.sleep(2)

    self.puback = puback

    def on_log_puback(client, userdata, level, buf):
    global puback
    if 'PUBACK' in buf:
    puback = True
    print "PUBACK!"

    def on_connect(client, userdata, flags, rc):
    print "Connect code: ", rc

    def on_disconnect(client, userdata, flags, rc=0):
    print "Disconnect code: ", rc

    def on_message(client, userdata, msg):
    print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

    def stop(self):
    print ("stopping loop")
    self.client.loop_stop()
    print "disconnecting"
    self.client.disconnect()

    a = A()

    time.sleep(2)
    print 'a.puback: ', a.puback
    a.stop()









    share|improve this question



























      0












      0








      0








      The first script works, meaning the callbacks are called and it ends printing puback: True



      The second script where I use class A to do the work does not work. Callbacks are not called, and it ends with a.puback: False



      I'm not sure if my problem is that callbacks don't work this way, in which case how can I get my class to work with these Paho MQTT callbacks? or if it's something more subtle.



      WORKS:



      def on_log_puback(client, userdata, level, buf):
      global puback
      if 'PUBACK' in buf:
      puback = True
      print "PUBACK!"

      def on_connect(client, userdata, flags, rc):
      print "Connect code: ", rc

      def on_disconnect(client, userdata, flags, rc=0):
      print "Disconnect code: ", rc

      def on_message(client, userdata, msg):
      print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

      def stop():
      print ("stopping loop")
      client.loop_stop()
      print "disconnecting"
      client.disconnect()

      import paho.mqtt.client as mqtt
      import time

      client = mqtt.Client("Luke, I am your client")
      mqtt.Client.connected_flag = False

      client.on_connect = on_connect
      client.on_disconnect = on_disconnect
      client.on_log = on_log_puback
      client.on_message = on_message

      status = client.connect(host="test.mosquitto.org",
      keepalive=60, port=1883)
      print "connect status: ", status
      time.sleep(2)

      print "loop_start"
      client.loop_start()
      time.sleep(1)

      sret = client.subscribe("test_topic")
      print "subscribe returns sret: ", sret
      time.sleep(2)

      # initialize global
      puback = False

      # test publish with qos=1
      status, msg_id = client.publish(topic="test_topic",
      payload="hello!",
      qos=1, retain=True)
      print "publish status: ", status
      time.sleep(2)

      print "puback: ", puback

      stop()


      DOESN'T WORK:



      import paho.mqtt.client as mqtt 
      import time

      class A(object):
      def __init__(self):

      client = mqtt.Client("Luke, I am your client")
      self.client = client
      mqtt.Client.connected_flag = False

      client.on_connect = self.on_connect
      client.on_disconnect = self.on_disconnect
      client.on_log = self.on_log_puback
      client.on_message = self.on_message

      status = client.connect(host="test.mosquitto.org",
      keepalive=60, port=1883)
      print "connect status: ", status
      time.sleep(2)

      print "loop_start"
      client.loop_start()
      time.sleep(1)

      sret = client.subscribe("test_topic")
      print "subscribe returns: ", sret
      time.sleep(2)

      # initialize global
      puback = False

      # test publish with qos=1
      status, msg_id = client.publish(topic="test_topic",
      payload="hello!",
      qos=1, retain=True)
      print "publish status: ", status
      time.sleep(2)

      self.puback = puback

      def on_log_puback(client, userdata, level, buf):
      global puback
      if 'PUBACK' in buf:
      puback = True
      print "PUBACK!"

      def on_connect(client, userdata, flags, rc):
      print "Connect code: ", rc

      def on_disconnect(client, userdata, flags, rc=0):
      print "Disconnect code: ", rc

      def on_message(client, userdata, msg):
      print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

      def stop(self):
      print ("stopping loop")
      self.client.loop_stop()
      print "disconnecting"
      self.client.disconnect()

      a = A()

      time.sleep(2)
      print 'a.puback: ', a.puback
      a.stop()









      share|improve this question
















      The first script works, meaning the callbacks are called and it ends printing puback: True



      The second script where I use class A to do the work does not work. Callbacks are not called, and it ends with a.puback: False



      I'm not sure if my problem is that callbacks don't work this way, in which case how can I get my class to work with these Paho MQTT callbacks? or if it's something more subtle.



      WORKS:



      def on_log_puback(client, userdata, level, buf):
      global puback
      if 'PUBACK' in buf:
      puback = True
      print "PUBACK!"

      def on_connect(client, userdata, flags, rc):
      print "Connect code: ", rc

      def on_disconnect(client, userdata, flags, rc=0):
      print "Disconnect code: ", rc

      def on_message(client, userdata, msg):
      print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

      def stop():
      print ("stopping loop")
      client.loop_stop()
      print "disconnecting"
      client.disconnect()

      import paho.mqtt.client as mqtt
      import time

      client = mqtt.Client("Luke, I am your client")
      mqtt.Client.connected_flag = False

      client.on_connect = on_connect
      client.on_disconnect = on_disconnect
      client.on_log = on_log_puback
      client.on_message = on_message

      status = client.connect(host="test.mosquitto.org",
      keepalive=60, port=1883)
      print "connect status: ", status
      time.sleep(2)

      print "loop_start"
      client.loop_start()
      time.sleep(1)

      sret = client.subscribe("test_topic")
      print "subscribe returns sret: ", sret
      time.sleep(2)

      # initialize global
      puback = False

      # test publish with qos=1
      status, msg_id = client.publish(topic="test_topic",
      payload="hello!",
      qos=1, retain=True)
      print "publish status: ", status
      time.sleep(2)

      print "puback: ", puback

      stop()


      DOESN'T WORK:



      import paho.mqtt.client as mqtt 
      import time

      class A(object):
      def __init__(self):

      client = mqtt.Client("Luke, I am your client")
      self.client = client
      mqtt.Client.connected_flag = False

      client.on_connect = self.on_connect
      client.on_disconnect = self.on_disconnect
      client.on_log = self.on_log_puback
      client.on_message = self.on_message

      status = client.connect(host="test.mosquitto.org",
      keepalive=60, port=1883)
      print "connect status: ", status
      time.sleep(2)

      print "loop_start"
      client.loop_start()
      time.sleep(1)

      sret = client.subscribe("test_topic")
      print "subscribe returns: ", sret
      time.sleep(2)

      # initialize global
      puback = False

      # test publish with qos=1
      status, msg_id = client.publish(topic="test_topic",
      payload="hello!",
      qos=1, retain=True)
      print "publish status: ", status
      time.sleep(2)

      self.puback = puback

      def on_log_puback(client, userdata, level, buf):
      global puback
      if 'PUBACK' in buf:
      puback = True
      print "PUBACK!"

      def on_connect(client, userdata, flags, rc):
      print "Connect code: ", rc

      def on_disconnect(client, userdata, flags, rc=0):
      print "Disconnect code: ", rc

      def on_message(client, userdata, msg):
      print " Message: ", str(msg.payload.decode("utf-8", "ignore"))

      def stop(self):
      print ("stopping loop")
      self.client.loop_stop()
      print "disconnecting"
      self.client.disconnect()

      a = A()

      time.sleep(2)
      print 'a.puback: ', a.puback
      a.stop()






      python python-2.7 callback mqtt paho






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 6:02







      uhoh

















      asked Nov 22 '18 at 5:39









      uhohuhoh

      1,4301937




      1,4301937
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Found it. When I moved the callbacks into the class, e.g. on_log_puback(self,...) I'd simply forgotten to add self to the beginning of the arguments. With that, it works nicely now.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424512%2flooking-for-mqtt-puback-from-on-log-callback-using-within-a-class-fails-but-it%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Found it. When I moved the callbacks into the class, e.g. on_log_puback(self,...) I'd simply forgotten to add self to the beginning of the arguments. With that, it works nicely now.






            share|improve this answer




























              0














              Found it. When I moved the callbacks into the class, e.g. on_log_puback(self,...) I'd simply forgotten to add self to the beginning of the arguments. With that, it works nicely now.






              share|improve this answer


























                0












                0








                0







                Found it. When I moved the callbacks into the class, e.g. on_log_puback(self,...) I'd simply forgotten to add self to the beginning of the arguments. With that, it works nicely now.






                share|improve this answer













                Found it. When I moved the callbacks into the class, e.g. on_log_puback(self,...) I'd simply forgotten to add self to the beginning of the arguments. With that, it works nicely now.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 7:18









                uhohuhoh

                1,4301937




                1,4301937






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424512%2flooking-for-mqtt-puback-from-on-log-callback-using-within-a-class-fails-but-it%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Marschland