Let’s start with how similar MQTT and Blynk are…
MQTT is very similar to Blynk. There’s a server/device relationship (in MQTT speak the server is know as the Broker) that maintains a connection with a heartbeat, and the server has the ability to know when a device has disappeared off the radar.
The server can then update a topic to indicate that the device is offline (dead). This is known as the Last Will and Testament and is very similar to Blynk’s Online/Offline indication functionality.
With MQTT the device can subscribe to topics (more about those below), and when a message appears on that topic it triggers a callback. The value sent in the message can be retrieved and used in the code that you want to execute when the message arrives.
This is virtually identical to the BLYNK_WRITE(vPin)
and param.asString()
functionality in Blynk.
Devices can write to topics - known as “Publishing” in MQTT speak - and this is virtually identical to the Blynk.virtualWrite()
functionality in Blynk.
Because of these similarities, the logical implementation would be to allow devices to subscribe to datastreams on the Blynk server, so the device is notified when a datastream value changes, and to publish data to datastreams. The datastreams would therefore be your topics.
Topics in MQTT
Topics are very similar to a folder structure in Windows. Everyone designs their folder structure to (hopefully) provide a logical structure to organise your data, depending on what you’re trying to achieve.
Also like Windows you can use wildcards in MQTT. This is where the implementation in Blynk could get messy - but maybe not.
Imagine you have a situation where you have 6 lamps in your living room, which are all controlled by different devices, and you want the ability to turn them all on or off with a single command.
MQTT allows you to structure your topics like this:
Livingroom/Lamp1
/Lamp2
/Lamp3
/Lamp4
/Lamp5
/Lamp6
and allows you to issue a wildcard command at the Livingroom
level that will be cascaded down to all of the child devices, which could be used to turn them all on or off.
To achieve this in Blynk you’d need a structure - similar to the organisation structure - to allow you to group datastreams. I don’t see how this could easily be implemented and TBH I dont think it’s that desirable.
So, I think the Blynk implementation of MQTT shouldn’t support wildcards.
If you did want to implement it, then you’d probably need to allow each datastream to have an MQTT topic defined for it, but that still looks very messy to me - especially from a server-side processing perspective.
I don’t personally use a topic structure that lends itself to wildcards, and in the above example I don’t have a problem with writing to six consecutive topics with an on or off command, in the same way that would in Blynk with six consecutive Blynk.virtualWrite
commands.
Therefore, my preferred method of referencing the Blynk datastreams wold be to treat them as topics grouped by device. I think I’d like to see the topic being:
auth_token/vPin/value
when you’re writing data to it, and:
auth_token/vpin
when you’re subscribing to it.
This would make the implementation far simpler from Blynk’s point of view.
EDITED TO ADD…
Something I forgot to mention is that you’d probably need to add the facility to do things like Log an Event, Sync a virtual pin, Set a widget property etc via an MQTT command. I guess all of the things that you can do via the REST API.
As Events are Template based you might do this by sending the command to a MQTT topic like this:
event_code/event_text
The datastream specific commands like setProperty might be something like this:
auth_token/vPin/setProperty/OnColor/value
End of Edit
Hopefully this helps. I’d be very than happy to share more info or get involved in testing.
Pete.