B4A Library JG MQTT Library

Discussion in 'Additional libraries, classes and official updates' started by freedom2000, Mar 15, 2015.

  1. freedom2000

    freedom2000 Well-Known Member Licensed User

    Hi all,

    This is my very first library :)

    It implements MQTT standard V3.1.1

    MQTT is a lightweight publish/subscribe messaging protocol.

    for further information see http://mqtt.org/.


    The MQTT protocol is based on the principle of publishing messages and subscribing to topics, or "pub/sub". Multiple clients connect to a broker and subscribe to topics that they are interested in. Clients also connect to the broker and publish messages to topics. Many clients may subscribe to the same topics and do with the information as they please. The broker and MQTT act as a simple, common interface for everything to connect to.

    Quality of Service
    MQTT defines three levels of Quality of Service (QoS). The QoS defines how hard the broker/client will try to ensure that a message is received.

    Implemented levels are :

    • 0: The broker/client will deliver the message once, with no confirmation.

    • 1: The broker/client will deliver the message at least once, with confirmation required.

    • 2: The broker/client will deliver the message exactly once by using a four step handshake.
    Clean session / Durable connections
    On connection, a client sets the "clean session" flag, which is sometimes also known as the "clean start" flag. If clean session is set to false, then the connection is treated as durable. This means that when the client disconnects, any subscriptions it has will remain and any subsequent QoS 1 or 2 messages will be stored until it connects again in the future. If clean session is true, then all subscriptions will be removed for the client when it disconnects.

    Limitation : Persistency of sessions is not implemented yet on the client side (this library)

    Retained Messages
    All messages may be set to be retained. This means that the broker will keep the message even after sending it to all current subscribers.

    Last Will Testament
    When a client connects to a broker, it may inform the broker that it has a will. This is a message that it wishes the broker to send when the client disconnects unexpectedly.

    Version 1.0

    This is the first release. It is provided with a very basic sample test App.

    The use is totally free, with no limitation.


    Connect to a broker (here iot.eclipse.org)

    Events :

    Methods :

    test App :

    it is limited to :
    • connect/disconnect to broker and pinging to test connection
    • sending/receiving messages
    • subscribing/unsubscribing to topics
    • selecting QoS

    Enjoy !
    And give me your feedback

    Attached Files:

  2. coslad

    coslad Well-Known Member Licensed User

    Good work !!
  3. alexhi

    alexhi Member Licensed User

    Good job!!! I tested its on ESP8266 (Lua) work OK.
  4. freedom2000

    freedom2000 Well-Known Member Licensed User

    Thank you Alexhi :)

    BTW I did the same --> I followed your track on ESP8266 and you are right it's a marvellous device.
    And Sooo cheap !

    I tried first Alberto's library but as it is 4 times more expensive than the ESP8266 :D I decided to have a look at the specifications and then to code my own library.
    As also I took benefit of all other members of B4A community without sharing anything... it was a good occasion for me de renvoyer l'ascenseur (send the lift back -as we say in France-)
    Last edited: Mar 17, 2015
  5. daemon

    daemon Active Member Licensed User

    Last edited: Mar 18, 2015
  6. Rabbit

    Rabbit Member Licensed User

    Thank you @freedom2000 for this library.

    Can you please share the library source to understand it and get a good practice to MQTT programming.
  7. freedom2000

    freedom2000 Well-Known Member Licensed User


    Frankly speaking seeing the code wouldn't be very helpful. You should rather have a look to the excellent specification.
    I was a totaly noob with MQTT last week. I read this doc and coded the lib during one week end long... Just a matter of patience, the protocol itself is simple.
  8. alexhi

    alexhi Member Licensed User

    Is this DONATIONWARE Lib ?

    I see in log:
    ** Activity (main) Create, isFirst = false **
    ** Activity (main) Resume **
    [objMqtt_messageArrived] Topic = MQTT_2RELE_10442788 / Message = ** DONATIONWARE ** !SetR0
    PUBACK 1
    [objMqtt_messageArrived] Topic = MQTT_2RELE_10442788 / Message = !SetL1
    [objMqtt_messageArrived] Topic = MQTT_2RELE_10442788 / Message = !LED1ON
    [objMqtt_messageArrived] Topic = MQTT_2RELE_10442788 / Message = !SetR1
    [objMqtt_messageArrived] Topic = MQTT_2RELE_10442788 / Message = !LEDON
  9. freedom2000

    freedom2000 Well-Known Member Licensed User


    NO My lib (attached to first post) is a totally free lib

    I believe that you are refering to Alberto's one which IS DONATIONWARE
  10. klarsys

    klarsys Active Member Licensed User

    I don't see any OnDisconnect event.
    How do I know if connection failed or disconnected?
  11. freedom2000

    freedom2000 Well-Known Member Licensed User


    You are right there is no "OnDisconnect event"
    If connection OK you get the On connect event
    You should check disconnection with a Ping (there is a Ping event)

    I will check how to add this event in a future release
  12. gprandst

    gprandst Member Licensed User

    I tested your lib on an old, slow HTC, but unfortunately there are lost messages. Even when QOS is set to 1. Especially retainded messages which automatic are sent direct after subscribe. Please check this.
    Thank you,
  13. freedom2000

    freedom2000 Well-Known Member Licensed User

    Thanks for thé feedback.
    I don't know how it's possible to loose messages on client side but I don't see how to check this...
  14. gprandst

    gprandst Member Licensed User

    the problem also occurs on a modern 4-core tablet. I have an account at www.dioty.co, there are the paths of the topics some more longer, maybe this has influence. There are 4 items with the flag retained. If I make a subscription to /PathToTopic/#, then the items are sent immediately, but not all arrive.
    Computing power certainly has an impact, but apps like Simple MQTT or MyMQTT don't lose messages.
    Thank you,
  15. gprandst

    gprandst Member Licensed User

    Correction: it should be topics, not items. Sorry for bad english.
  16. freedom2000

    freedom2000 Well-Known Member Licensed User

    Ok I will try to reproduce and fix it.

    Thanks for your feedback
  17. freedom2000

    freedom2000 Well-Known Member Licensed User

    Hi again,

    I have created an account on dioty.co and successfully sent and received messages.
    I try now to send messages with retain flag to check possible collision while reception.

    But I do not succeed in storing on dioty.co several retained messages. How do you do this please ?

    My assumption for the bug is that I assume that Only one message is sent by server at a time. This could be wrong if "a lot" of retained messages are sent in the same tcp packet...
    SO I really need to reproduce your testing conditions first !
  18. gprandst

    gprandst Member Licensed User

    Hi JP!

    My real world messages are published by ESP8266 running NodeMCU.
    But there is a handy java tool: mqtt-spy. Window/perspectives/detailed allow you to set retained flag in the published topic.
    An other android tool is SimpleMQTT, it has a checkbox for retained publishing.
    To delete a retained topic you have to publish an empty topic (no data) to dioty.
    Hope this helps!
    Have a nice day,
  19. freedom2000

    freedom2000 Well-Known Member Licensed User

    I'll try simpleMQTT

    Thanks for the help with dioty
  20. Tem

    Tem Member Licensed User

    Your library is realy good job :)
    What means the last four parameters in Connect command:

    0 (zero) ?
    False ?
    True - OK is Clean session :)
    60 timeout?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice