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 - alberto@visualnet.inf.br
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: 637
  • MQTT Library1.0.2.zip
    162 KB · Views: 313
  • MQTTSampleActivity.zip
    8.2 KB · Views: 453
  • SampleSubscribe2.zip
    8.3 KB · Views: 423
  • MQTT Library1.0.4.zip
    154.6 KB · Views: 271
  • MQTT Library1.0.5.zip
    154.9 KB · Views: 336
  • SampleIsConnected.zip
    8.3 KB · Views: 490
  • SampleWithToken.zip
    8.2 KB · Views: 314
  • MQTT Library1.0.7.zip
    155.6 KB · Views: 648
Last edited:

engvidal

Member
Licensed User
Hi Alberto
Can you help me ? I already use mqtt with MQTTlens, raspberry, PIC's, ESP8266, so I believe my environment is ok, but when I try to compile your sample for Android, i get this message?

Compiling generated Java code: Error
Cannot find C:\program files (x86)\Basic4android\libraries\org.eclipse;paho.client.mqttv3-1.0.1.jar


Thank You
 

engvidal

Member
Licensed User
It is running now.
Very simple.
Just a question ? Where are you located ? I saw an answer in good portuguese. Are you in Brazil ??
I am asking because my credit card only accept US$ convertions, not euros.. Is there any way I can pay for it in US$ directly ?
 

cas6678

Active Member
Licensed User
Hi Alberto.
I set your 1.07 library as you suggested.
Im trying to use "CloudMQTT"

B4X:
Dim strBroker As String    = "m10.cloudmqtt.com:17612"
   Dim strUsername As String  = "XXXXXXXX"
   Dim strPassword As String  = "XXXXXXXXX"
   Dim bRet As Boolean = objMqtt.Connect(strBroker,"1",strUsername,strPassword)

"1" might be Client ID, is right?
But each time i clicked Connect button i received this log error.
Could you suggest me what am i missing?

** Activity (main) Create, isFirst = true **
[objMqtt_onInitialized]
MQTT About = MQTT Library - By Alberto Iglesias (alberto@visualnet.inf.br)
MQTT Version = 1.0.7 ** DONATIONWARE **
** Activity (main) Resume **
Error occurred on line: 49 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:457)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:273)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
at com.visualnet.mqtt.Connect(mqtt.java:313)
at com.visualnet.mqtttest.main._button1_click(main.java:433)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

EDIT: Same issue with my RPi as MQTT broker running mosquitto at ip:1883.
 
Last edited:

cas6678

Active Member
Licensed User
Now works well!!!
I need to compare your last Sample with the old one.
More questions when i find differences.
Thanks!!
 

Doñate

New Member
Licensed User
Hi Alnerto, I like your library, I test here: http://cloud.iot-playground.com/ and work very well when I publish and subscribe.

I will a make a donation later next week, when I do this, you still send me a library without "Donationware" right ?

Thanks in advance!
 

Alberto Iglesias

Well-Known Member
Licensed User
Hello my friend, I'm not using B4x anymore, you can compare with this library, I create mine based on this: org.eclipse.paho.client.mqttv3

 

amorosik

Well-Known Member
Licensed User
Hello my friend, I'm not using B4x anymore, you can compare with this library, I create mine based on this: org.eclipse.paho.client.mqttv3


Also jMqtt is based on this project, then is difficult find pro/cons of jMqtt vs your library
"..I'm not using B4x anymore.." can we know if you have moved to other developer enviroment?
 

amorosik

Well-Known Member
Licensed User
Hi Alberto, could you tell me if your library, based on the Eclipse Paho project, allows automatic client reconnection, and high availability (alternative server use if main not available)?
 
Top