B4J Tutorial [MQTT - Message Queuing Telemetry Transport] A For Dummies Experience on MS Windows

Mashiane

Expert
Licensed User
Ola

My first steps towards IoT.

1. Download Eclipse Mosquitto
2. There are a lot of examples in the forum about coding this, however I needed a GUI tool to first experiment with this. A variety exists but I like MQTT.fx. This has an awesome GUI. Experience it for yourself.
3. They recommend you use the 32bit versions of the downloads on 1 and 2 above.

To be continued based on my experiences...

#WatchThisSpace
 

Mashiane

Expert
Licensed User
Now what?

1. Mosquitto is installed as a service on your windows computer, so go to Services, locate it and start it. It runs on port 1883 I think. This is called a "broker". On Erels post there are links on learning more about MQTT, so we read together.
2. When you run MQTT.fx, it will check if you have an updated version and will ask you to install that version to continue.

MQTTFX.gif
 
Last edited:

avalle

Active Member
Licensed User
Adafruit IO is free if the volume of data is limited. I don't know of any service that's completely free. They pay for connectivity...

Also, as you mentioned MQTTX... EMQ Technologies has its own MQTT IoT Cloud service based on their EMQX open source broker.
See https://cloud.emqx.io
It's a commercial service but offers a 180 days free trial if you just want to learn and experiment.

EMQX is also a great alternative to Mosquitto as an open source broker that's very easy to configure and run on your own servers.
 

joulongleu

Active Member
Licensed User
The ones I found could not work then (via code), fortunately I found one now that I am testing on. If you have a list that you could share it would be great.

This should have

1. Protocol
2. Broker / Host
3. Port
4. Path

Thanks a lot.

Hi:Use banano You can use the following,Thanks Alwaysbusy
code:
    Sub BANano_Ready

    Public mqtt As BANanoMQTTClient
    Dim clientId As String = "mqttjoul"
    mqtt.Initialize("mqtt", "broker.mqtt-dashboard.com",8000, "/mqtt", False, clientId)
    Dim ops As BANanoMQTTConnectOptions
    ops.Initialize("Your account", "Your password")
    ops.CleanSession = False
    mqtt.Connect2(ops)
    end  sub

    ''===MQTT 20201201======
Sub mqtt_Connected (Success As Boolean)
    Log("connected " & Success)
end sub
Private Sub mqtt_Disconnected
    Log("Disconnected")
end sub

Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    Log("mqtt_MessageArrived")
end sub
 
Last edited:

Mashiane

Expert
Licensed User
Download

I have figured that the best way for myself to learn this is by understanding everything about it and create our own MQTT broker client in pure JavaScript.

Not bad at all, after all one was not very much concerned about the "make-up", but at least one explored some pure CSS application by doing the buttons some justice, adding rounded borders, etc.

1610332190906.png


I managed to find some other freely publicly MQTT brokers to test this and so far the experience has been good.

We are able to trap a variety of events here..

1610332276061.png


Lets look at the structure of the project. There is a will, options and the client connection itself.
 
Last edited:

Mashiane

Expert
Licensed User
The will

What is it?

In MQTT, you use the Last Will and Testament (LWT) feature to notify other clients about an ungracefully disconnected client.

This is set under the options of the connection..

TheMashMQTTWill
  • getIsDirty As Boolean
    get IsDirty
  • settopic (vtopic As String)
    set the topic
  • gettopic As String
    get the topic
  • setpayload (vpayload As String)
    set the payload
  • getpayload As String
    get the payload
  • setQoS (vqos As Int)
    set the QoS
  • getQoS As Int
    get the qos
  • setretain (vretain As Boolean)
    set retain
  • getretain As Boolean
    get retain
  • getOptions As Map
    get the will structure
 

Mashiane

Expert
Licensed User
The connection options

These are options that are passed to make the connection. We have created a variety of these to meet our needs. Thus only V3 options have been added.

TheMashMQTTOptions
  • setresubscribe (vresubscribe As Boolean)
    set resubscribe - if connection is broken and reconnects, subscribed topics are automatically subscribed again (default true)
  • getmresubscribe As Boolean
    get resubscribe
  • setclientID (clientid As String)
    set the client id
  • getclientID As String
    get the client id
  • setreschedulePings (vreschedulePings As Boolean)
    set reschedulePings - reschedule ping messages after sending packets (default true)
  • setqueueQoSZero (vqueueQoSZero As Boolean)
    set queueQoSZero - if connection is broken, queue outgoing QoS zero messages (default true)
  • getqueueQoSZero As Boolean
    get queueQoSZero
  • setpassword (pwd As String)
    set the password - the password required by your broker, if any
  • getpassword As String
    get the password
  • setuserName (usrname As String)
    set the username - the username required by your broker, if any
  • getuserName As String
    get username
  • getIsDirty As Boolean
    get IsDirty
  • setkeepalive (vkeepalive As Int)
    set keep alive - heartbeat time, default 60 seconds, set 0 to disabled.
  • getkeepalive As Int
    get keep alive
  • setprotocolId (vprotocolId As String)
    set protocolId
  • getprotocolId As String
    get the protocolid
  • setprotocolVersion (vprotocolVersion As Int)
    set the protocolVersion
  • getprotocolVersion As Int
    get the protocol version
  • setclean (vclean As Boolean)
    set clean - set to false to receive QoS 1 and 2 messages while offline.
  • getclean As Boolean
    get clean
  • setreconnectPeriod (vreconnectPeriod As Int)
    set reconnect period - 1000 milliseconds, interval between two reconnections. Disable auto reconnect by setting to 0.
  • getreconnectPeriod As Int
    get reconnect period
  • setconnectTimeout (vconnectTimeout As Int)
    set connectTimeout - 30 * 1000 milliseconds, time to wait before a CONNACK is received
  • getconnectTimeout As Int
    get connectTimeout
  • getOptions As Map
    get options
 

Mashiane

Expert
Licensed User
The mqtt client, its events, its methods and all that stuff.

This was one of the most interesting things we have done during this learning process. This is what makes the demo project shine.

TheMashMQTTClient
  • connect
    connect to the mqtt broker, events raised are
    _onreconnect
    _onerror
    _onconnect
    _onclose
    _ondisconnect
    _onoffline
    _onmessage
    _onpacketsend
    _onpacketreceive
    _onsubscribe
    _onunsubscribe
    _onpublish
  • getMessage (m As BANanoObject) As String
    convert payload to string
  • getMessageFromBytes (Payload() As Byte) As String
    convert payload to string using byte array
  • publish (topic As String, message As String, iQoS As Int, bRetain As Boolean, bMarkAsDuplicate As Boolean)
    publish a message to a topic
  • subscribe (topic As String, iQoS As Int)
    subscribe to a topic
  • unsubscribe (topic As String)
    unsubscribe from a topic
  • close (bForce As Boolean)
    close the client
  • setProtocol (vconnectionType As String)
    set protocol
  • getProtocol As String
    get protocol
  • getHost As String
    get the host
  • setBroker (broker As String)
    set the broker
  • getBroker As String
    get the broker
  • setPort (port As Int)
    set the port
  • getPort As Int
    get the port
  • setPath (path As String)
    set the path
  • getPath As String
    get the path
  • getconnected As Boolean
    get the connected state
  • getnextId As Int
    get the nextId
  • getpassword As String
    get the password
  • getuserName As String
    get username
  • getclientID As String
    get the client id
  • removeOutgoingMessage (mID As Int)
    Remove a message from the outgoingStore
  • reconnect
    Connect again using the same options as connect
  • getLastMessageId As Int
    get last message id. This is for sent messages only.
  • getreconnecting As Boolean
    set to true if the client is trying to reconnect to the server.
 

alwaysbusy

Expert
Licensed User
unfortunately it has some limitations that are not helping me at the moment.
Just out of curiosity, can you give an example of what is missing? BANanoMQTT is using the same Paho MQTT library all the other B4X implementations (jMQTT, iMQTT, etc) are using so it is the same across all B4X platforms and has the same syntax/usage. I can't see anything special that the Paho implementation is missing or would be needed. Paho is the most used across many languages and IoT devices. E.g. we use it with tens of thousands of handheld scanners running the Python version of Paho on it for years now sending hundreds of thousands of messages daily to the B4J jMQTT and it works perfectly.

MQTT.js can indeed also work in a browser, but is more intended for Node.js use.

Alwaysbusy
 

Mashiane

Expert
Licensed User
The limitations with the implemented BananoMQTT I found were on the options that one can set and also the events raised as compared to what MQTTs has implemented as per this example. All the documentation above indicates all the options, events and methods.

I guess it's because perhaps the other events and options are not needed mostly and then are not implemented, but for my project, I needed them. I could be wrong and stand to be corrected.

I have not read about Paho and dont know about the internals of the other libraries. The intention is to learn the forum examples and play around until I am well versed on this technology for my own personal needs.
 

alwaysbusy

Expert
Licensed User
set and also the events raised as compared to what MQTTs has implemented
The whole purpose of BANano is to be as compatible with normal B4X code as possible (so same events and options where possible). I personally haven't encountered a case where I needed more than what Erels MQTT exposes, but maybe there are fringe cases.

Alwaysbusy
 
Top