B4A Library JG MQTT Library

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/.

MQTT_pubsub.jpg


Publish/Subscribe
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.

Utilisation

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

B4X:
MyMQTT.Connect("iot.eclipse.org","1883", "test", "", "", "/android/", "dead", 0, False, True, 60)

Events :
MQTT_events.jpg



Methods :
MQTT_methods.jpg


test App :

MQTT_App.jpg

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
 

Attachments

  • JG_MQTT_V1.0.zip
    12.2 KB · Views: 618

freedom2000

Well-Known Member
Licensed User
Longtime 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:

Rabbit

Member
Licensed User
Longtime 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.
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

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.
 

alexhi

Member
Licensed User
Longtime User
Hi,freedom2000!
Is this DONATIONWARE Lib ?

I see in log:
** Activity (main) Create, isFirst = false **
Disconnect...
** 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
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi

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
 

klarsys

Active Member
Licensed User
Longtime User
I don't see any OnDisconnect event.
How do I know if connection failed or disconnected?
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

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
 

gprandst

Member
Licensed User
Longtime User
Hi,
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,
Gottfried
 

freedom2000

Well-Known Member
Licensed User
Longtime 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...
 

gprandst

Member
Licensed User
Longtime User
Hi,
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,
Gottfried
 

freedom2000

Well-Known Member
Licensed User
Longtime 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 !
 

gprandst

Member
Licensed User
Longtime 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,
Gottfried
 

Tem

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

B4X:
MyMQTT.Connect("iot.eclipse.org","1883", "SesionID", "", "", "/android/", "dead", 0, False, True, 60)

0 (zero) ?
False ?
True - OK is Clean session :)
60 timeout?
 
Top