BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Bridge Examples for node.js with Raspberry Pi's as sender and receiver

New user to Blynk. I think it’s a fantastic product and I’ve had no issues so far getting the App both sending and receiving data with Raspberry Pi (node.js). Now that I’ve played for a bit, I’m ready to do something useful and I would like to send data back and forth between a couple RPI v3’s. I’ve searched all over for examples and can’t seem to find a solid one that covers both sides of the bridge in node.js…I’ve played around a little bit with some syntax based on some Arduino examples and one node.js example I found (https://github.com/vshymanskyy/blynk-library-js/blob/master/examples/nodejs/client-with-bridge.js), but I have not been successful in sending data. My code will run on each, I get connected to the server, but no data is exchanged. I’m sure I’m missing something simple.
…just need a solid example to start from.

Best Regards,

Matt

In what way doesn’t that example work?

Bridge is just a simple one-way “transfer” of data/control from the sender (the script with the bridge code) to the receiver (any script in your account that has the corresponding AUTH used by the sender).

In reality it is actually the senders script telling the server to pretend that you activated a widget (or GPIO) in the receivers project, thus performing that widget’s function (or setting GPIO state).

If you want a two-way “link”, then simply recreate the bridge code on the other RPi’s script, but referencing back to the first ones AUTH.

Thanks so much for your reply. Specifically I’m a bit confused on the nodejs syntax. For example…

Setting up a new virtual pin for the blynk app is pretty clear.
var v1 = new blynk.VirtualPin(1);

Setting up a bridge virtual pin also seems straightforward but where does the 31 from the example come from? Do I need to define a bridge for each virtual channel or are there multiple channels per bridge?
var bridge1 = new blynk.WidgetBridge(31);
var bridge2 = new blynk.WidgetBridge(32);

Writing a value back to blynk app on joystick event works fine.
v1.write(axis.value);

Syntax is a bit different for bridge virtual…I had to guess on this a bit in nodejs because I don’t see any examples.
bridge1.virtualWrite(31,axis.value);

Reading a value in the other PI sent from the blynk app works fine…
v1.on(‘write’, function(param) {
console.log(‘v1:’, param[0]);
});

Trying to read a value in the other PI sent from first PI. Again I had to guess on the syntax as I don’t have an example. What I tried doesn’t work. I also don’t know where to use the (31) previously defined for the bridge1.
bridge1.on(‘virtualWrite’, function(param) {
console.log(‘Bridge1:’, param[0]);
});

Additionally the nodejs example shows:
blynk.on(‘connect’, function() {
bridge1.setAuthToken(‘Your another api key’);
console.log(“Blynk ready.”);
});
blynk.on(‘disconnect’, function() { console.log(“DISCONNECT”); });

Looks like this sets up an auth key on the sender. How is the additional auth key used on the receiver? I assume both PI nodes still need the AUTH that was set up for the project on blynk app? I would still like to send data to the app as well.

Sorry so many questions. I’m quite new to nodejs, so I’m probably just missing something simple.

Thanks and Best Regards,
Matt

It is a vPin for the bridge… admittedly it is unclear of it’s necessity, but just assign an unused one from the senders side

syntax is different but concept and process is identical with the C++ examples. In this case the vPin used (you are again using 31?) is the one on the receivers side that you want to trigger/supply with the value from a variable.

The api key refers to the receivers AUTH code… this is how the server knows which receiving device to control with the bridge command.

The rest of the function is the JS equivalent of…

http://docs.blynk.cc/#blynk-firmware-blynktimer-blynk_connected

As mentioned, the Bridge process is the same on C++ of which there are more articles and examples in this forum to look at when reasoning out the process.

Thanks a lot for your help. Your advice on the C++ stuff helped a lot. Without too much trouble, I was able to get it working. The key thing I was missing was the understanding that bridge connects 2 projects, not 2 devices in the same project.

Best Regards,

Matt

I’m having similar trouble understanding bridges. Is there any reason to create a bridge when you have two devices (Raspi/Arduino) in the same project?

Yes, each device should have its own Auth code.

Pete.

yes i understand that part, i was just unsure of whether a bridge would be necessary (as opposed to just two separate tcp clients).

If you want to transfer data between the two devices then you need to use Bridge code.

Pete.

I do need to transfer data, however I am using the johnny-five library which should allow me to do the same thing.