Python MQTT Client The Loop and Callbacks Explained

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 มิ.ย. 2024
  • When using the Paho Python MQTT client you will encounter the loop function .
    In this video tutorial you will learn what the loop does and about the various loop methods .
    You will learn how the loop trigger callbacks and what callbacks are and how they work.
    You will also learn what to do if you have multiple client connections.
    We take a detailed look at the on_connect callback and do some basic troubleshooting including how to make sense of any output you see from print statements with in the script as because of the asynchronous nature of callbacks they can occur anywhere in the output.
    Finally we look at how we stop the loop from writing the script
    Tutorial on Site
    Understanding and Using Callbacks -www.steves-internet-guide.com/...
    the loop
    www.steves-internet-guide.com/...
    Understanding MQTT Clean Sessions
    • Understanding MQTT Cle...
    --------
    MQTT Keep Alives Explained
    • MQTT Keep Alives Expla...
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 27

  • @silverian
    @silverian 2 ปีที่แล้ว

    Thank You for great material!

  • @pareshkurdekar1256
    @pareshkurdekar1256 4 ปีที่แล้ว +1

    Your videos were extremely helpful.So, thank you for that.
    I am trying to write a code so that two systems can publish and subscribe to each other.
    Also after the on_message function, where does it return to?

    • @stevecope
      @stevecope  4 ปีที่แล้ว

      Hi
      If you go to the site and use the ask steve page to contact me and I can send you some sample scripts via email
      rgds
      steve

    • @stevecope
      @stevecope  4 ปีที่แล้ว

      Hi
      Take a look at this tutorial it should help it also has demo code.
      www.steves-internet-guide.com/two-way-communication-mqtt-python

  • @haarishk2302
    @haarishk2302 6 ปีที่แล้ว

    Steve how to send data or message from one system to another system using mqtt protocol

    • @stevecope
      @stevecope  6 ปีที่แล้ว +1

      You subscribe on one system and publish on the other see this tutorial
      www.steves-internet-guide.com/send-file-mqtt/

  • @michaels8297
    @michaels8297 4 ปีที่แล้ว

    Thanks for the videos steve. How can I wait to do an action on a flask server after I publish a message to a raspberry pi? Currently, the raspberry pi responds to the server with a confirmation the pump was turned on once the action was taken. The flask app on the server imports a script i wrote to handle callbacks...so I can view the message received from the pi. I am just confused as to how I can return the callback value to the flask app. Normally I have been writing the values to a text file and accessing them via the flask app. But I just use arbitary time pauses to wait until the message from the pi is written into the text file... is there a better way to do this?

    • @michaels8297
      @michaels8297 4 ปีที่แล้ว

      would I use something like redis & rq?

    • @michaels8297
      @michaels8297 4 ปีที่แล้ว

      I ended up solving this by simply appending the callback from the pi to a list and accessing that list from the flask app. The flask app will perform some sort of while loop until the sent value appears in the list and then return a value to the browser. I dont know if this is a great fix but it should work for my purposes

    • @stevecope
      @stevecope  4 ปีที่แล้ว +1

      Sorry I don't know as I've never used flask but it is something I need to do.
      Rgds
      Steve

  • @francisgriffin1
    @francisgriffin1 3 ปีที่แล้ว

    I have had trouble taking in parts of Steve's videos. Whilst this is largely due to my inadequacies I don't think it is helped by the editing of the audio that seems to have removed the natural speech breaks and therefore one doesn't have time to understand the point being made before moving on to the next issue. I know that one can stop and rewind but that also has its drawbacks. Overall very good but a bit slower and a few more examples would be great.

    • @stevecope
      @stevecope  3 ปีที่แล้ว

      Tks for the useful feedback
      Rgds
      Steve

  • @AlqGo
    @AlqGo 6 ปีที่แล้ว

    Which thread call the callback functions? Is it the same thread that runs loop() in the background (if using loop_start()), or is it the main thread?

    • @AlqGo
      @AlqGo 6 ปีที่แล้ว

      Nvm. I just had a quick look at the source code. So, loop_start() runs loop_forever() in the background (daemon) thread, and loop_forever() calls loop(), and loop() calls loop_read(), and loop_read() (indirectly) calls callback functions. So, in effect, the callback functions are called in the same daemon thread where loop is run.

    • @stevecope
      @stevecope  6 ปีที่แล้ว

      It is the thread that starts the loop.

    • @AlqGo
      @AlqGo 6 ปีที่แล้ว

      That's not what I asked. I asked which thread calls the callback functions?

    • @stevecope
      @stevecope  6 ปีที่แล้ว

      When you use loop_start() it starts a new thread. This new thread will check the send/receive buffers. So as far as I understand it it is this thread that calls the callback function

    • @yagzyalcntas553
      @yagzyalcntas553 5 ปีที่แล้ว

      @AL isnt Steves answer is correct? you asked Which thread call the callback functions and the answer is, the thread that starts(or get started by) the loop call the callback functions right?

  • @AnushaYadav01
    @AnushaYadav01 ปีที่แล้ว

    when we assign a callback function why we are not including the parameters when it is called?

    • @stevecope
      @stevecope  ปีที่แล้ว

      The callback function is already defined in the client code. You can create your own in which case it would include the parameters as you should see in the demo scripts.
      Rgds
      Steve

  • @mohammedsameer5295
    @mohammedsameer5295 5 ปีที่แล้ว

    how to make a client not to disconnect from the broker and keep listening to broker

    • @stevecope
      @stevecope  5 ปีที่แล้ว

      You need to us while loop.
      Once you are subscribed the on_message callback will handle incoming messages so you just need to wait.
      At the end of the script use
      while True:
      continue
      This will stop the script from exiting and it will listen indefinitely..
      Your code would be
      create client and enable callbacks
      connect
      start a loop using loop.start()
      subscribe
      wait in while loop

    • @mohammedsameer5295
      @mohammedsameer5295 5 ปีที่แล้ว

      @@stevecope thankyou

    • @mohammedsameer5295
      @mohammedsameer5295 5 ปีที่แล้ว

      @@stevecope should the "client.subscribe" be inside the loop.start() ?

    • @mohammedsameer5295
      @mohammedsameer5295 5 ปีที่แล้ว

      @@stevecope IF the "client.subscribe" is out of the loop it is unable to receive messages

    • @stevecope
      @stevecope  5 ปีที่แล้ว +1

      Yes you put the loop_start immediately after the connect.
      Usually the subscribe goes in the on_connect callback so the subscribe happens only when connected..
      Use the ask steve page on the site and I'll send you a very simple demo script.
      rgds
      steve