Android Question Question about JSON and MQTT

ciapw

Member
Licensed User
Hi. all. I want to send a data for ex: card id and balance from my database running in a raspberry pi to an user interface (android apps) in a android tablet. So, my tablet will show the data .
My first idea is to send the data (card id and balance) as a json package, send it through mqtt , and then my tablet will receive the mqtt data, decode the json package and show the data being sent. After that my tablet will also be used to send the feedback (for ex send chosen data to the raspi) using the same method .
My question is , is this possible or is there any other option to do this? Thankyou so much
 

ciapw

Member
Licensed User
Thanks for the reply .. I want to ask more about MQTT. Can we differ the different payload? I have sent multiple message in different topics. How could we use them separately? for example.. in topic 'A' if i receive "hello" i will update label1, but if topic 'B' if i receive "hey" i will update label2?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi @ciapw ,
I do like MQTT a lot and I fond it very useful in a few projects. Now, since you control both sides of your implementation (Raspy and Tablet), Erel's suggestion to use B4XSerializator is to be carefully taken in account (it makes things very easy about data exchange). When MQTT (or FCM) is to be preferred is when you send a message "in the wild"..not knowing if the intented listening device it's listening at that time.
Anyway, in both cases you could code a "spec field", so the receiving device could know where to show the received message (or, more generally what to do with that message).
With B4XSerializator is trivial: you define a type where a field indicates what to do with the message field (I would add a message-type field too).
Using MQTT, you could use a single topic, where each message begins with a "code" (i.e "001" means direct the remaining part of the message to label1 and so on). In my implementations I generally devote a topic as the "control codes" channel and one or more topics to real data; this is not needed, but I prefer it that way.
 
Upvote 0
Top