B4A Library MQTT - MQ Telemetry Transport for B4A

MqttTop.jpg


This library was made based on MQTT Protocol


Installation instructions:
- Copy the .xml and .jar(s) files to your custom libraries folder
- Select in your REFERENCED LIBRARIES


MqttRef.png




Version history:
V1.0.0 (First public release - Donationware):
- Initial version

V1.0.2 (Release - Donationware):
- Implement method to get independence of threads

V1.0.4 (Release - Donationware):
- Implement method to subscribe a topic with QoS

V1.0.5 (Release - Donationware):
- Now can be used isConnected method "BEFORE" the first connection

V1.0.6
(Release - Donationware):
- not published

V1.0.7 (Release - Donationware):
- New methods: Publish2, Subscribe3, Subscribe4
This new methods return the token, then after when arrived or publish the messages, you can compare

MQTT
Author:
Alberto Iglesias - [email protected]
Version: 1.07
  • MQTT
    Events:
    • connectionlost ( )
    • deliverycomplete (Token As String)
    • messageArrived (Topic As String, Message As String)
    • onConnect (Status as boolean As )
    • onDisconnect (Status as boolean As )
    • onInitialized ( )
    Fields:
    • CleanSession As Boolean
    • JavaDebug As Boolean
    • KeepAliveInterval As Int
    • QoS_AtLeastOnce As Int
    • QoS_AtMostOnce As Int
    • QoS_ExactlyOnce As Int
    • generatedClientId As String
    Methods:
    • About As String
    • Connect (broker As String, ClientID As String, Username As String, Password As String) As Boolean
    • Disconnect As Boolean
    • Initialize (eventname As String)
    • Publish (Topic As String, Message As String, Qos As Int, Retained As Boolean)
    • Publish2 (Topic AsString, Message AsString, Qos AsInt, Retained AsBoolean)
    • Subscribe (Topic As String)
    • Subscribe2 (Topic As String, QoS as int)
    • Subscribe3 (Topic As String)
    • Subscribe4 (Topic As String, QoS as int))
    • Unsubscribe (Topic As String)
    • Version As String
    • isConnected As Boolean
    Permissions:
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.INTERNET


MqttSample1.png



MqttRef2.png



How works the MQTT protocol
MqttScheme1.jpg



Communication Infrastructure

MqttScheme2.png



References:
http://mqtt.org/
http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
Mosquitto Broker: http://mosquitto.org/



* This version is fully functional, the only one difference is when publishing a message the text "Donationware" is delivery with your message.
Please consider a simple €10 donation and I send to you the Library without "Donationware" text in a publish method.

Already available in B4J too:
https://www.b4x.com/android/forum/threads/mqtt-mq-telemetry-transport-for-b4j.48184/

Already available in B4i too:
https://www.b4x.com/android/forum/threads/mqtt-mq-telemetry-transport-for-b4i.50036/


 

Attachments

  • MQTTB4ASample.zip
    7.8 KB · Views: 949
  • MQTT Library1.0.2.zip
    162 KB · Views: 664
  • MQTTSampleActivity.zip
    8.2 KB · Views: 779
  • SampleSubscribe2.zip
    8.3 KB · Views: 749
  • MQTT Library1.0.4.zip
    154.6 KB · Views: 568
  • MQTT Library1.0.5.zip
    154.9 KB · Views: 592
  • SampleIsConnected.zip
    8.3 KB · Views: 841
  • SampleWithToken.zip
    8.2 KB · Views: 592
  • MQTT Library1.0.7.zip
    155.6 KB · Views: 1,023
Last edited:

leitor79

Active Member
Licensed User
Longtime User
Hi! Is this still alive?

I'd need to know how could I know the referred message at Sub objMqtt_deliverycomplete(Token As String)

I've read somewhere "you should maintain your own mapping of delivery token to message" but I can't figure it out how to do it.

Thank you very much! Great library!
 

leitor79

Active Member
Licensed User
Longtime User
Hi, Alberto!

Thank you very much for your answer!

I was aware of the token, but I don't see how to relate that token to the delivered message. I've read that the token returned on deliverycomplete is the same token returned when the message was published, but I don't see how to get that.

My problem is... If I publish 2 or more messages, almost-at-the-same-moment, how do I know which token belongs to each one?

Thank you!
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Ahh Ok, I understanding what you saying... Look:

There are two styles of MQTT client, this one and IMqttAsyncClient.

  • IMqttClient provides a set of methods that block and return control to the application program once the MQTT action has completed.
  • IMqttAsyncClient provides a set of non-blocking methods that return control to the invoking application after initial validation of parameters and state. The main processing is performed in the background so as not to block the application programs thread. This non blocking approach is handy when the application wants to carry on processing while the MQTT action takes place. For instance connecting to an MQTT server can take time, using the non-blocking connect method allows an application to display a busy indicator while the connect action is occurring. Non-blocking methods are particularly useful in event-oriented programs and graphical programs where issuing methods that take time to complete on the the main or GUI thread can cause problems.

This library use the IMqttClient and don´t the IMqttAsyncClient, because this you don´t get the Token when Publish....

But..


I can create in the same library something method to use the Publin with IMqttAsyncClient, then you can get the token when send the message.

I just looking the way and tell you after ok?

Tks..
 

leitor79

Active Member
Licensed User
Longtime User
Thank you, Alberto!

I'm having an issue now with the connect method; I get the following exception trying to connect:

B4X:
Error occurred on line: 914 (Config)
java.lang.NullPointerException
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(MqttAsyncClient.java:480)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(MqttAsyncClient.java:455)
    at com.visualnet.mqtt.Connect(mqtt.java:315)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:712)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:244)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:4445)
    at android.view.View$PerformClick.run(View.java:18446)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5146)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
    at dalvik.system.NativeStart.main(Native Method)
Config - GetMQTT - java.lang.Exception:  java.lang.NullPointerException

my connect line:

B4X:
objMqtt.Connect(strBroker, "","","")

my broker (the IP is right):

B4X:
Dim strBroker As String = "tcp://192.168.1.125:1883"

my mosquitto server:

eget20.png


thank you very much!
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Strange... I tried right now with v1.0.7 and 2 Androids and look: (And tried too with my iphone with another library b4i)

testbroker.png



This Mosquitto Broker is in my MAC, but I tried too in another broker in a cloud
deinmac:sbin deinmac$ ./mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
1440575364: mosquitto version 1.4.2 (build date 2015-05-08 13:55:22-0700) starting
1440575364: Config loaded from /usr/local/etc/mosquitto/mosquitto.conf.
1440575364: Opening ipv4 listen socket on port 1883.
1440575364: Opening ipv6 listen socket on port 1883.
1440575387: New connection from 192.168.20.1 on port 1883.
1440575387: New client connected from 192.168.20.1 as 06E0FC55-0A55-4CBA-BF74-FEBC4F964983 (c1, k60, u'').
1440575392: Client 06E0FC55-0A55-4CBA-BF74-FEBC4F964983 disconnected.
1440575491: New connection from 192.168.20.8 on port 1883.
1440575491: New client connected from 192.168.20.8 as paho822778997283 (c1,
k60).
 

leitor79

Active Member
Licensed User
Longtime User
Hi Alberto, thank you very much for your answer. I see what you say. However, I've tried with the free&public mqtt server, and I'm getting the same error. Here is my code, maybe I'm doing something wrong:

B4X:
objMqtt.JavaDebug = True
objMqtt.CleanSession = False
objMqtt.Initialize("objMqtt")
objMqtt.Connect(strBroker, "","","")

My broker:

Dim strBroker As String = "tcp://broker.mqtt-dashboard.com:1883"

I've tried with and without the "tcp://".


Thank you veru much!
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
leitor79, I want to help you but I can´t find any errors....

Look my test with mqtt-dashboard.com in files:

I suggest:

* Try to update the .jar dependency ( org.eclipse.paho.client.mqttv3-1.0.1.jar) in your library folder
* Try to update you java compiler
* I uploaded my test compiled (mqtt.apk) is exactly the example (sample.zip) compiled in my computer with this broker (tcp://broker.mqtt-dashboard.com:1883), try to installed and run... If work, I think is your compiler, the code is the same
 

Attachments

  • org.eclipse.paho.client.mqttv3-1.0.1.jar
    167.4 KB · Views: 273
  • Sample.zip
    13.1 KB · Views: 301
  • test1.jpg
    test1.jpg
    342.1 KB · Views: 291
  • website dashboard.jpg
    website dashboard.jpg
    189.7 KB · Views: 288
  • MQTT.apk
    213.9 KB · Views: 263

sarmanho

Member
Licensed User
Longtime User
Alberto. Boa noite querido.
Tens um link de servidor BROKER para JAVA FOR WINDOWS ou compatível com WIN32/WIN64 ?
 

leitor79

Active Member
Licensed User
Longtime User
leitor79, I want to help you but I can´t find any errors....

Look my test with mqtt-dashboard.com in files:

I suggest:

* Try to update the .jar dependency ( org.eclipse.paho.client.mqttv3-1.0.1.jar) in your library folder
* Try to update you java compiler
* I uploaded my test compiled (mqtt.apk) is exactly the example (sample.zip) compiled in my computer with this broker (tcp://broker.mqtt-dashboard.com:1883), try to installed and run... If work, I think is your compiler, the code is the same


Hi Alberto, thank you for your answer!

I've tried updating the .jar dependency file, but get the same results. I don't know how to update my java compiler? Or where do I check my current version of it?

However, I've tried "downgrading" the library version; using the 1.05, I connect OK. The issue is when I upgrade to 1.07. Does that fact rings any bells? Am I missing something when updating? Or could this be evidence of outdated java compiler?

Regards!
 

leitor79

Active Member
Licensed User
Longtime User
Hi Alberto;

I'll add: using your test project, I can connect OK, however getting an ¿error? on the connect clause:

sshot-545.png

(I don't see anything new unfiltering the logs)

Regards!
 

leitor79

Active Member
Licensed User
Longtime User
One thing it occurs to me... must the mqtt object declared in an activity? I have it in a module... maybe it has to do with the problem...

Thank you very much!
 

leitor79

Active Member
Licensed User
Longtime User
Posting to update; it seems the problem has to be with the mqtt object being in a code module. Declaring (and using) the mqtt object in an activity or in a service module connects and publish ok; just getting the Unexpected event (missing RaiseSynchronousEvents): objmqtt_onconnect error on the logs.

Regards!
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Alberto.
It is possible a example that send messages from one android device to another ?
I think that is very usefull for a lot colleagues
if work will be my great pleasure make donation for this great library
Thank you
Marco
 

sarmanho

Member
Licensed User
Longtime User
Alberto,
Which brokers MQTT do you recommend for thousands of simultaneous connections?
What is the best brokers MQTT?
 
Top