B4A Library MQTT - MQ Telemetry Transport for B4A

Discussion in 'Additional libraries, classes and official updates' started by Alberto Iglesias, Dec 14, 2014.

  1. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    [​IMG]

    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


    [​IMG]



    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


    [​IMG]


    [​IMG]


    How works the MQTT protocol
    [​IMG]


    Communication Infrastructure

    [​IMG]


    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:
    http://www.b4x.com/android/forum/threads/mqtt-mq-telemetry-transport-for-b4j.48184/

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


    [​IMG]
     

    Attached Files:

    Last edited: Aug 18, 2015
    bravospedy, hookshy, wonder and 10 others like this.
  2. woniol

    woniol Active Member Licensed User

    Hi, I'm very interested in this lib.
    Do You think i can work with B4J?
     
  3. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Last edited: Dec 14, 2014
    DonManfred and inakigarm like this.
  4. bluedude

    bluedude Well-Known Member Licensed User

    This is awesome, now need to find a stable hosted MQTT broker.
     
  5. woniol

    woniol Active Member Licensed User

  6. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

  7. alwaysbusy

    alwaysbusy Expert Licensed User

    Thanks for the lib Alberto! Can this be used to send messages from one android device to another? And is there a limit to the length of the message you are sending?
     
  8. little3399

    little3399 Active Member Licensed User

    Hi,

    Does this Lib can be transplant to B4i ? ;)
     
  9. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Hi..

    The MQTT protocol says:

    " The length is limited to 64k but within that there are no limits to the number of levels in a topic tree.
    • There can be any number of root nodes; that is, there can be any number of topic trees."
    But the MQTT was constructed to be lightweight, to be fast!


    I think works, but you can try...



    And for B4i, YES, I think in construct a library in January, because in this holydays I can´t :-( Sorry.....


    Thanks!
     
    alwaysbusy likes this.
  10. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    A Free broker to test with you aplication:

    URL: broker.mqtt-dashboard.com
    Port: 1883
     
    DonManfred likes this.
  11. bluedude

    bluedude Well-Known Member Licensed User

    Interesting and works like a breeze. I have a broker running on CentOS VPS and it was only a few minutes work.

    The B4A sample seems to have some threading problems, I cannot intercept the message and bind it to a view. Any suggestions?

    Should I put the sample in a service?
     
  12. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Hello bluedude,

    what type of threading problems? You try use another library? This wrapper is based on PAHO library, and this library not intercept any message.


    Tks!
     
    ocalle likes this.
  13. bluedude

    bluedude Well-Known Member Licensed User

    In objMqtt_messageArrived(Topic As String, Message As String)

    i'm trying to put the received Message in an edittext on the layout, this is not allowed and i'm getting an error about main threads. Logging it works fine but for the rest I cannot do anything.

    I'm not using any other libraries just your stuff.
     
  14. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    I see... Look this example with thread and EDITTEXT.

    I think this is resolve your problem.


    I put this free broker in this sample

    tcp://broker.mqtt-dashboard.com:1883

    you can follow the messages in
    Dashboard: http://mqttdashboard.com/dashboard

    Best regards!
     

    Attached Files:

  15. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    hey bluedude, I implement new method in library to works independent of thread Library.

    Version 1.0.2 available with another sample (MQTTSampleActivity.zip)
     
  16. bluedude

    bluedude Well-Known Member Licensed User

    Works great but what i'm missing is a Quality of Service flag in the subscribe method. I see you have it in publish but not in subscribe.
     
  17. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    You right bluedude!

    I put in this new version (1.0.4) and available for download.

    I upload to a sample with subscribe2 method.

    Thanks!
     
  18. bluedude

    bluedude Well-Known Member Licensed User

    Great, I will make a donation a.s.a.p. MQTT is a huge benefit over Google Cloud Messaging which sucks in my opinion. JSON over MQTT is perfect for many scenario's, instead of polling for new data a server can update clients through topics which is much better.
     
  19. Aidan Ruff

    Aidan Ruff Member Licensed User

    Hola Alberto
    I just sent you 10 euros - nice work, thanks. If you get a chance to send me the full version without the added text, I'd appreciate it!
     
  20. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Hello Aidan,

    I sent to you in your email, check please!

    Thanks!

    :)
     
Loading...
  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