Hi im trying to write a script so that the LED widget lights up when a button is pressed.
But for now im just trying to get the LED widget to turn on.
Ive read some post but cant seem to find anything that works for me.
I found the following script in another post but doesn’t seem to work for me.
Basically i just want to turn on the Led widget.
#!/usr/bin/env node
var Blynk = require('/home/pi/node_modules/blynk-library-js');
var AUTH = '********';
var blynk = new Blynk.Blynk(AUTH, options = {
connector : new Blynk.TcpClient()
});
var v1 = new blynk.VirtualPin(1);
var v2 = new blynk.VirtualPin(2);
var v8 = new blynk.WidgetLED(8);
var v9 = new blynk.WidgetLED(9);
v1.on('write', function(param) {
console.log('V1:', param);
});
blynk.on('connect', function() {
console.log("Blynk ready.");
v2.write(255);
v8.turnOn();
v9.setValue(255);
});
blynk.on('disconnect', function() { console.log("DISCONNECT"); });
Have you set the RPI with npm, node etc?
Is the RPI just a client or client and server?
If just client are you using the Blynk cloud server?
Paste console from running the script.
yes the rpi has npm etc im already running my DHT11 and blinders on it.
its running just as a client,im using the Blynk cloud server.
Im not getting an error in the console,the led just doesn’t do anything.
Like i said i just got this script from a post on this forum, i figured at least i would manage to turn the led on for testing but it doesn’t work.Its not my code, i don’t know whats wrong with it.
#!/usr/bin/env node
var BlynkLib = require('/usr/lib/node_modules/blynk-library/');
var AUTH = 'TOKEN_HERE';
var blynk = new BlynkLib.Blynk(AUTH, options= {
connector : new BlynkLib.TcpClient(options = {addr:"192.168.0.25", port:8442 })
});
var v1 = new blynk.VirtualPin(1);
var v8 = new blynk.WidgetLED(8);
v1.on('write', function(param) {
console.log('V1:', param);
if(param == '1')
{
v8.turnOn();
}
else
{
v8.turnOff();
}
});
blynk.on('connect', function() { console.log("Blynk ready."); });
blynk.on('disconnect', function() { console.log("DISCONNECT"); });
It works a little different form the Arduino, but it’s more of a syntax problem than something else. Pushing the V1 button now turns on/off the LED on V8.
And more, the examples for NodeJS for Blynk are very limited, I’ll give you that
Good question, to be honest, I have absolutely no idea. That is more a javascript/RasPi issue than something else. You have to pull a pin high/low with a button I guess and read the GPIO pin in the Javascript, but I wouldn’t know how.
v1.on says: when button attached to v1 on app is pressed, pass along it’s value (function(param) into this function
In the function itself (basically from { until }, you can meddle with that value. For now it’s 0 or 1, but you can imagine it could be a value like 150 (for PWM led dimming and such, via a slider widget).
The console.log simply logs the parameter to the console when running the script.
There is a load of syntaxis in there, or grammar if you will. I’m afraid the grammar is something you just have to learn (but for many languages it’s more or less the same).
There are control structures (like if, then etc), loops (for next, while) and loads of other stuff to influence any data coming into your script.
#!/usr/bin/env node
var BlynkLib = require('/usr/lib/node_modules/blynk-library/');
var gpio = require('/usr/lib/node_modules/pi-gpio');
var AUTH = 'XXXXXXXXXXXXXXXX';
var blynk = new BlynkLib.Blynk(AUTH);
var v8 = new blynk.WidgetLED(8); {
gpio.open(16, 'input', function(param) {
gpio.read(16, function(param) {
if(param == '1')
{
v8.turnOn();
}
else
{
v8.turnOff();
}
gpio.close(16);
});
});
blynk.on('connect', function() { console.log("Blynk ready."); });
blynk.on('disconnect', function() { console.log("DISCONNECT"); });
So i played around a little bit and i get this error:
/home/pi/led4.js:27
});
^
SyntaxError: Unexpected end of input
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions…js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3