B4J Tutorial [IoT] MQTT Protocol

Status
Not open for further replies.

rboeck

Well-Known Member
Licensed User
One question for retained objects - i have used this option and now i want to delete all the pending messages. As i unterstand, i have to send a new message with the same topic with a zero byte payload.

So i tried to send this with
B4X:
MQtt.Publish(mytopic,serializator.ConvertObjectToBytes(Null))
and this had no effect. Then i tried to directly send Null, but i got only syntax errors.
How can i send a zero byte payload?
 

Roberto P.

Well-Known Member
Licensed User
Thanks Erel
very interesting.
Could you help me understand briefly cases of use of this system and the difference compared to web-socket chat?
I sense that this technology can be used for notifications, correct? or to exchange messages, chat type, correct?
it would be very helpful to understand also the advantages and disadvantages of this system.
Thank you in advance
Greetings
 

Erel

Administrator
Staff member
Licensed User
There are cases where it is very clear. For example if you want to use browsers as clients then you need to use WebSockets.

If you have a large solution based on a http server then it also makes sense to use WebSockets.

WebSockets is a client / server channel. MQTT connects multiple clients.

If you are using a low level network solution (TCP + AsyncStreams) then you should consider switching to MQTT.

Running a publicly accessible MQTT broker on the internet is less secure compared to a web server.

Real push notifications should be implemented with GCM (Android)or APN (iOS).

These are just several points. I recommend you to try the examples to get a better feeling of this technilogy.
 

avalle

Active Member
Licensed User
On top of Erel's comments, I would add the following to explain why MQTT is a superior protocol to WebSockets, at least for IoT applications, multi-client access to a single resource or embedded hardware/microcontroller applications.
  • MQTT is a publish/subscribe protocol with support of QoS (Quality of Service). You can decide the QoS level based on how critical is your application and then get rid of redundancy if not required.
  • MQTT uses a binary format, while HTTP is based on (expanded) text strings. Binary is more efficient and uses less data/memory/bandwidth.
  • MQTT is a lightweight protocol, created specifically for small footprint, thus ideal for embedded hardware and microcontroller who have limited resources, especially flash memory for code and RAM. As an example, a message header in MQTT only uses 2 bytes, while HTTP can use dozen or even hundreds of bytes.
  • Lightweight also means less CPU cycles to execute, turning into lower power supply requirements, a critical aspect for battery operated devices and mobile clients.
Regards
Andrea
 

AzureCrystal

Member
Licensed User
What is MQTT?
Great library and tutorial, got it working with CloudMQTT, I can do circles on any number of devices, but when I send a the message "clear"(or any other text using same topic) via the CloudMQTT websocket console it fails, shouldn't this code work for a passed string as well? Grateful thanks for any insights ;)

B4X:
Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
   Dim obj As Object = serializator.ConvertBytesToObject(Payload) '<---fails here
   If obj Is CircleData Then
     If Topic = mytopic Then Return 'the circle was already drawn
     Dim cd As CircleData = obj
     DrawCircleData(cd)
   Else 'obj is string
     Dim s As String = obj
     Select s
       Case "clear"
         Canvas1.DrawColor(Colors.Black)
         Activity.Invalidate
       Case "close"
         Activity.Finish
     End Select
   End If
End Sub
 

AzureCrystal

Member
Licensed User
Never mind, I just saw the video of the B4J example and the text commands are coming from the B4J client, not from a generic MQTT chat window...
Wow, this is fast and opens a whole array of possibilities, great stuff Erel.
 
Last edited:

JakeBullet70

Well-Known Member
Licensed User
Never mind, I just saw the video of the B4J example and the text commands are coming from the B4J client, not from a generic MQTT chat window...
Wow, this is fast and opens a whole array of possibilities, great stuff Erel.
I have been working with MQTT for about a 1 1/2 year. And yeah, the possibilities are endless.
 

Danbicky

Member
Licensed User
Erel,

I have the same problem, I am sending the command clear from my mqtt broker as a string and as soon as it is arrived by the app i get the same error as azure above.

Dim obj As Object = serializator.ConvertBytesToObject(Payload) '<---fails here

Throws a java io error

How can this be resolved? I simply want to send commands from the broker to the android app for notification of events.

Thanks

Dans
 

Erel

Administrator
Staff member
Licensed User
Please start a new thread for this question. Note that you cannot send messages from the broker. All the communication happens between the clients (through the broker).
 

Danbicky

Member
Licensed User
This question is the same as azure's and seems related to this thread?, sorry I do post through a client to the broker using node red interface, message is sent to broker by node red, message is received on subscribed topic by client in android app and fails with the error as listed above.

Dans
 

techknight

Well-Known Member
Licensed User
I know this is an older thread, but I am posting this as a heads up:

I am going to save anyone new here hours of headache: The latest openSSL light 1.1.X does NOT come with the correct files needed for mosquito to run. Neeed the older 1.0 and earlier version. Not sure when or IF they will fix it.
 
Status
Not open for further replies.
Top