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: 859
  • MQTT Library1.0.2.zip
    162 KB · Views: 543
  • MQTTSampleActivity.zip
    8.2 KB · Views: 668
  • SampleSubscribe2.zip
    8.3 KB · Views: 620
  • MQTT Library1.0.4.zip
    154.6 KB · Views: 465
  • MQTT Library1.0.5.zip
    154.9 KB · Views: 506
  • SampleIsConnected.zip
    8.3 KB · Views: 713
  • SampleWithToken.zip
    8.2 KB · Views: 500
  • MQTT Library1.0.7.zip
    155.6 KB · Views: 906
Last edited:

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

Have you done some experiments with putting this in a service in the background and keeping the connection open? What is the best approach to get the best results that messages always will be delivered? Trying to investigate if this can replace GCM.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Yes! It´s fantastic and independent way to do fast communication with others devices with a short messages, with this you have more control then GCM and the time can be much more faster, depends only the your server or MQTT rent service (like this for example: http://www.cloudmqtt.com/)

In a GCM we have problems with response time, can be late more than 30 minutes in some cases.

I do in a project inside a car company (imagine thousands/millions of simultaneous connections...) and we try before with GCM, but after we have a lot of success with MQTT way.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Ok, just wondering with what settings on the server and client you are using it. I notice that it drops connections in a service and looses subscriptions. What QOS flags are you using on both sides? Would be nice if you can inform us a little more on the best settings to work with Android.

Till now I haven't been able to get a stable version with a service in the background.

Some questions:
- what QOS are you using on the server,
- are you using persistent on the server,
- do you use the stuff in an Android sticky service,
- how to keep topic subscriptions if connection has dropped.

Do you also have experience with Mosquito and the amount of users limit? Can it be run on a modest VPS with 5000-10000 users (connections).
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello,

I used in some projects the MOSQUITTO server with more than 5000 devices....

If you need more then 1 million , I recommend use some server like IBM MESSAGESIGHT (http://www-03.ibm.com/software/products/en/messagesight)

or some cloud service dedicated to do this.

- The QoS we used in our projects depend of your message and propose of that, normally "2" but depends...
- Persistent on server, the same... depends of your type of service, if you can describe a little more, I can help you.
- Yes, I used in some android projects, receiving messages through the service, with no problem
- This is easily controlled by the events

If you need help for projects.. tell me.

Tks
 

adrianstanescu85

Active Member
Licensed User
Longtime User
Hello Alberto,

Is there any online reliable server other than the one at tcp://broker.mqtt-dashboard.com:1883 ? I'm asking this since this one tends to get stuck from time to time.

Thank you!
Adrian
 

adrianstanescu85

Active Member
Licensed User
Longtime User
Thank you for the tip, I just created an account and got my details for the Kitty plan!

They appear to use usernames and passwords, how can I integrate that into your sample app?

I have an URL that's like:
B4X:
mqtt://username:[email protected]:portnumber

Maybe you can give an example based on that
 
Last edited:

adrianstanescu85

Active Member
Licensed User
Longtime User
Cause if I try to replace that URL as it is shown on my account, I get an error: "An error has occured in sub: java.lang.IllegalArgumentException on: mqtt://..."
 

imbault

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

MQTT
Author:
Alberto Iglesias - [email protected]
Version: 1.01
  • MQTT
    Events:
    • connectionlost ( As )
    • deliverycomplete (Token As String)
    • messageArrived (Topic As String, Message As String)
    • onConnect (Status as boolean As )
    • onDisconnect (Status as boolean As )
    • onInitialized ( As )
    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)
    • Subscribe (Topic As String)
    • 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: look in https://www.b4x.com/android/forum/threads/mqtt-mq-telemetry-transport-for-b4j.48184/



Hi @Alberto Iglesias ,
Maybe my question is stupid, I'm thinking of a domotic solution for my house
Is your project and this protocol can be used for domotic projects?

Do you know domotics sensors which use that protocols?

Thanks
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello Andrian,

You need to use like this: Look my account in the same server

Dim strBroker As String = "tcp://m20.cloudmqtt.com:15968"
Dim strUsername As String = "myusername"
Dim strPassword As String = "mypassword"
Dim bRet As Boolean = objMqtt.Connect(strBroker,"",strUsername,strPassword)
 

adrianstanescu85

Active Member
Licensed User
Longtime User
I have a quick question, it's actually not that important but it may help a lot...

I tried setting up a repeated message sending (and receiving) i.e. send a different text each 200ms. It seems that the "top speed" to achieve that is about 1 second though even if that 200ms timer ticks - the messages can't be sent faster. It's like the messages are queued and one has to wait for the previous one to be transmitted, which takes time. It doesn't seem to be a bandwidth issue since the messages are very very short, basically one or two characters long, so there's not a lot of info and even the free plan on CloudMQTT which is about 10kbps could handle that I guess.

I tried speeding this up with DoEvents when sending, but got no results. Is this a syncronization issue (so that an 'async' would work)?

Any ideas over this...?

Adrian
 

adrianstanescu85

Active Member
Licensed User
Longtime User
I just noticed that if I switch from QoS_ExactlyOnce to QoS_AtLeastOnce this works crazily well!!! Solved!!
 

coslad

Well-Known Member
Licensed User
Longtime User
Hello Alberto

Is it possible to run a mqtt broker on a Android os?
 

coslad

Well-Known Member
Licensed User
Longtime User
Mqtt is a good choice in local net , but in a local net without internet is necessary to have a own broker.
The economics way is to install Mosquito on a raspberry pi board , but i don't like it because RB pi works in linux .
I prefer the "Banana Pi board" that is powerful than Raspberry and it work on Android .
And i can even use a smartphone (in a local net ) as broker
 

coslad

Well-Known Member
Licensed User
Longtime User
Alberto, are you answering to me? I didn't understand

Is the broker that saves the message and sends it to the device when it is online ? So like a chat message application !
 
Top