Need help with NodeJS

I have 2 problems:
I want to have a virtual lamp, when gpio 21 is high and i want a virtual lamp when gpio 21 is low (on v2 and v3). I really dont know what i did wrong but nothing works.

My second problem is that i have this errors when i want to start a example from: https://examples.blynk.cc/?board=Ubuntu&shield=System%20default&example=Widgets%2FTerminal

/home/pi/temperatur-blynk/index.js:51
#define BLYNK_PRINT stdout
^

SyntaxError: Invalid or unexpected token
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions…js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMain (module.js:611:10)
at run (bootstrap_node.js:387:7)
at startup (bootstrap_node.js:153:9)

Sorry, those examples are using C++ Arduino style code.

You are using Javascript. not as many examples for tha with Blynk… but look here…

and search this forum for keywords like nodejs, javascript

1 Like

You would need to show us the code you have so far…

Please paste it here and format it properly as directed…

Blynk - FTFC

1 Like

This would be a simple NodeJS example of repeating back what you type into your terminal widget with Time and Date added on.

var term = new blynk.WidgetTerminal(vPin);  // Setup Terminal Widget.  Use number only, no V

// Repeat terminal input
term.on('write', function(data) {
 term.write('You wrote:' + data + " @ " + new Date().toTimeString() + " " + new Date().toDateString() + '\n');
});
1 Like

Thank you and do you know how the code is for this programm. I see now i tryed a lot of shit and i delete this trash.

I now this dont work but i want something like this.

if gpio 21 high
do blynk.write(v2, high)
else
if gpio 21 low
do blynk.write(v3, high)
else
blynk.write(v2, low)
blynk.write(v3, low)
fi
fi

Read through all those NodeJS examples, in the link above, to get a better idea of the various commands and syntax… Blynk NodeJS requires a bit more learning and effort on your part

The GPIO state detection will depend on the GPIO library you uses, here are a few. I currently use pigpio

pigpio - npm

You define your GPIO pin something like this

const Gpio = require('pigpio').Gpio;
const Pin21 = new Gpio(21, {mode: Gpio.INPUT, pullUpDown: Gpio.PUD_DOWN, edge: Gpio.EITHER_EDGE});

Detect the GPIO state and control your vPin like this

Pin21.on('interrupt', function (value) {
    blynk.virtualWrite(2, value);  // send GPIO state to vPin 2
});

I also have a small NodeJS example for the Joystick widget that controls servos… so hopefully you can get some further syntax ideas from it as well.

1 Like

Can you please give me the full code? i really dont understand this :disappointed_relieved:

There is no “full code” to give… your setup and situation is totally different from mine.

All I could do it write it all for you from scratch… and no, I am not going to do that :stuck_out_tongue: I have a difficult enough time writing my own code.

You need to learn how to do some basic programming with NodeJS if you want to use it for Blynk. All the tools are here, and some Google searching would probably go a long way as well.

var blynkLib = require('blynk-library'),
   const Gpio = require('pigpio').Gpio;
   const Pin21 = new Gpio(21, {mode: Gpio.INPUT, pullUpDown: Gpio.PUD_DOWN, edge: Gpio.EITHER_EDGE});
    sys = require('util'),
    exec = require('child_process').exec,
    child;

var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

var blynk = new blynkLib.Blynk(AUTH);

setInterval(function()
    {
        Pin21.on('interrupt', function (1) {
            blynk.virtualWrite(1, 1);  // send GPIO state to vPin 1
            blynk.virtualWrite(2, 0);  // send GPIO state to vPin 2
        });

        Pin21.off('interrupt', function (0) {
            blynk.virtualWrite(2, 1);  // send GPIO state to vPin 2
            blynk.virtualWrite(1, 0);  // send GPIO state to vPin 1
        });



    }
   , 3000);

What is wrong ?

And i have this Problem:
/home/pi/temperatur-blynk/test.js:2
const Gpio = require(‘pigpio’).Gpio;
^^^^^

SyntaxError: Unexpected token const