Blynk new mqtt event

Blynk has recently released Mqtt support which is really an awesome feature to have and go beyond its possibilities.

I am able to connect to blynk mqtt server for my esp32 device and able to get the data from blynk mqtt using the topics using mqtt client.

For testing I have subscribed the below topic to get the real time data event when I click button, changed the slider through the mobile app or the web dashboard in Blynk playform.

downlink/#

But I am not able to figure out how to get the datastream value on realtime when its datastream v0 value changes from 1-48(an example). The V0 is the sensor data displayed as label in the Dashboard.

The downlink/# topic is not receiving the data while data on v0 changes. I am sending data from esp32 using the latest library using Blynk.virtualWrite(V0, height);

I am able to get the latest data from the http rest api though. Also able to get the latest data using the same above event if I publish to this topic client.publish(“get/ds/all”) or get/ds. I always have to request/publish to get the latest sensor data, which is not event based.

Is the mqtt event supposed to work only on buttons and sliders or everything else. Am I missing anything?

I’m a bit confused by your description of your setup, but you seem to be expecting that if you publish data to a datastream from your ESP32 then the same device should see the corresponding value being returned to the ESP32 on the downlink/# topic.

I suspect that his isn’t happening for the same reason that a Blynk.virtualWrite (V0, value) command doesn’t result in the BLYNK_WRITE(V0) callback function being triggered when you use C++.
This behaviour is there to prevent an infinite loop of write/recieve/write being created.
Let’s face it, if you’ve just published the value of 48 to V0 from your ESP32 you don’t need to see that value returned on the downlink/# topic because you already know the V0 value is 48 because you’ve just published that value from the very same ESP32 device.

Pete.

2 Likes

My esp32 device connects to blynk cloud via blynk library.

My mqtt client to blynk is connecting from a Node Js app from a different location.

My node js app need to know when the value changes in V0 via mqtt topic and get the latest value.

I will be sending some value to v0 from esp32 (1-48). I wont be sending infinitely. Just when the value changes.

Are both devices using the same Blynk Auth token?

Pete.

Yes both use same token.

I don’t think you’re meant to be doing that.

The C++ library only allows one connection per auth token, and the MQTT documentation says…

  • Blynk.Cloud currently disallows simultaneous connection of multiple MQTT clients using the same auth token.

I suspect it’s an oversight by Blynk that a C++ and MQTT connection is allowed for one auth token.

Pete.

We did discuss it, however we decided not to introduce any interaction between blynk and mqtt protocol simultaneous connections.

1 Like

I am using same token in esp32 and Node Js mqtt client.

Client 1: esp32 (token 1) using C++ library
Client 2: Node js server(not in esp32) (token 1) running in linux server.

For both I am able to connect and get the data from Blynk.

Esp32 sends one single sensor data in (1-5) seconds intervals randomly to blynk cloud. So I just want to know when that is sent to blynk using mqqt topic. So this makes it event based rather than polling.

You won’t be able to achieve what you want with two devices that share the same auth token, because as I said before Blynk is designed not to respond back to the device that writes the data.

The fact that you actually have two devices makes no difference, Blynk sees them as one device because they have one auth token.

There are alternative solutions, such as using two Blynk devices with separate auth tokens, or using Node-Red, or simply polling the ablynk server for changes as you’ve already suggested.

Pete.

If I can not communicate with already connected blynk devices with mqtt then this mqtt protocol is not helping.

If it was possible it would be great feature to have.

I think the purpose of the MQTT protocol is to provide an alternative method of connection to compliment the existing two methods - Blynk library and HTTP(S) API (or three if you count the Node-Red contrib, but this is really just emulating the Blynk library).

I can’t envisage a scenario where Blynk would mirror a message back to a device, because of the risk of the endless loop scenario I mentioned earlier.

Pete.

There should have been someway where we can talk from mqtt and C++ library and https and so on instead of having third party implementations. It would have been great.

I just want to control the existing devices which are already using blynk c++ library using MQTT protocol.

1 Like

Yes, we are considering to implement a mechanism for this interaction. This is much more complicated, but we also see a great value in it.
There’s no timeline on this feature yet.

1 Like

You guys already have a blynk protocol. Mqtt will be a interface on top of it and talk to each other. :blush:

Strange things is I am able to control blynk library connected devices from http. Why is there is no endless loop from those kinds of cases(http and blynk c++ client).

It’s not really strange at all - assuming that when you say “http” you’re referring to the Blynk HTTP(S) REST API.

The HTTP protocol doesn’t provide real-time responses to datastream changes. You need to call the API (effectively polling the server for the current datastream values).

An update via the app, the C++ library, or the API, or via MQTT doesn’t produce an automatic trigger on the device making the API call. You have to poll the server to know about that value change.

The MQTT protocol works basically the same way, it won’t receive an incoming value on the subscribed topic in response to a value published via MQTT, updated via an API call, or updated via C++. You’d sneed to request that datastream value via an MQTT publish to become aware of the value change.

The C++ library also doesn’t trigger a BLYNK_WRITE() in response to a Blynk.virtualWrite. It will however trigger a BLYNK_WRITE() in response to a change via the HTTP API, or MQTT, or a value change resulting from a widget in the mobile or web dashboard.

Pete.

To avoid this endless loop. If I use native mqtt client in esp32 and talk to blynk using mqtt from esp32.

Can I talk to blynk from third application which runs on node js in a linux server using same mqtt of blynk? And control the esp32 via blynk mqtt servers.

So multiple connections not allowed for free?

Please reply above?

As far as I’m aware, Blynk only allows one connection with a specific auth token.

Pete.