Error Blynk and OnOff

Hi everyone, I have a mistake that is making me sleepy. a few days ago I was doing a project for atomizing university. I was able to make the code work just fine. As I have my raspberry pi some time decided to buy a bigger SD card. so I had to format and install a new system.

So far so good. When I installed NODE and NPM I had a bit of trouble, but searching I was able to learn how to handle this installation. In other words, I can now have all NODE versions.

But after installing everything right, install the libary, when I started my code, it started to give error. And I did not understand why. I’m running after this mistake for a while, but all I do is no use. I’ll make my code available here. Please help me.


#!/usr/bin/env node

//*** SMARTPHONE DOORLOCK ***//

// ************* PARAMETERS *************** //
// 
// Parameters: unlockedState and lockedState
// These parameters are in microseconds.
// The servo pulse determines the degree 
// at which the horn is positioned. In our
// case, we get about 100 degrees of rotation
// from 1ms-2.2ms pulse width. You will need
// to play with these settings to get it to
// work properly with your door lock
//
// Parameters: motorPin
// The GPIO pin the signal wire on your servo
// is connected to
//
// Parameters: buttonPin
// The GPIO pin the signal wire on your button
// is connected to. It is okay to have no button connected
//
// Parameters: ledPin
// The GPIO pin the signal wire on your led
// is connected to. It is okay to have no ledconnected
//
// Parameter: blynkToken
// The token which was generated for your blynk
// project
//
// **************************************** //
var unlockedState = 1000;
var lockedState = 2200;

var motorPin = 14;
var buttonPin = 4
var ledPin = 17
var relay1 = 22
var relay2 = 23

var blynkToken = 'CODECORRECT';

// *** Start code *** //

var locked = true

//Setup servo
var Gpio = require('pigpio').Gpio,
  motor = new Gpio(motorPin, {mode: Gpio.OUTPUT}),
  button = new Gpio(buttonPin, {
    mode: Gpio.INPUT,
    pullUpDown: Gpio.PUD_DOWN,
    edge: Gpio.FALLING_EDGE
  }),
  led = new Gpio(ledPin, {mode: Gpio.OUTPUT});
  relay1 = new Gpio(relay1, {mode: Gpio.OUTPUT});
  relay2 = new Gpio(relay2, {mode: Gpio.OUTPUT});

//Setup blynk
var Blynk = require('blynk-library');
var blynk = new Blynk.Blynk(blynkToken);
var v0 = new blynk.VirtualPin(0);
var v1 = new blynk.VirtualPin(1);
var v2 = new blynk.VirtualPin(2);

console.log("locking door")
lockDoor()

button.on('interrupt', function (level) {
	console.log("level: " + level + " locked: " + locked)
	if (level == 0) {
		if (locked) {
			unlockDoor()
		} else {
			lockDoor()
		}
	}
});

v0.on('write', function(param) {
	console.log('V0:', param);
  	if (param[0] === '0') { //unlocked
  		unlockDoor()
  	} else if (param[0] === '1') { //locked
  		lockDoor()
  	} else {
  		blynk.notify("Door lock button was pressed with unknown parameter");
  	}
});

v1.on('write', function(param) {
	console.log('V1:', param);
  	if (param[0] === '0') { //unlocked
  		relay1d()
  	} else if (param[0] === '1') { //locked
  		relay1l()
  	} else {
  		blynk.notify("Door lock button was pressed with unknown parameter");
  	}
});

v2.on('write', function(param) {
	console.log('V2:', param);
  	if (param[0] === '0') { //unlocked
  		relay2d()
  	} else if (param[0] === '1') { //locked
  		relay2l()
  	} else {
  		blynk.notify("Door lock button was pressed with unknown parameter");
  	}
});

blynk.on('connect', function() { console.log("Blynk ready."); });
blynk.on('disconnect', function() { console.log("DISCONNECT"); });

function relay1l() {
	relay1.digitalWrite(1);
	//notify
  	blynk.notify("Radio desligado");
	
}

function relay1d() {
	relay1.digitalWrite(0);
	//notify
  	blynk.notify("Radio ligado");
	
}

function relay2l() {
	relay2.digitalWrite(1);
	//notify
  	blynk.notify("Luz 2 apagada");
	
}

function relay2d() {
	relay2.digitalWrite(0);
	//notify
  	blynk.notify("Luz 2 acesa");
	
}

function lockDoor() {
	motor.servoWrite(lockedState);
	led.digitalWrite(0);
	locked = true

	//notify
  	blynk.notify("Porta esta fechada!");
  	
  	//After 1.5 seconds, the door lock servo turns off to avoid stall current
  	setTimeout(function(){motor.servoWrite(0)}, 1500)
}

function unlockDoor() {
	motor.servoWrite(unlockedState);
	led.digitalWrite(1);
	locked = false

	//notify
  	blynk.notify("Porta esta aberta!"); 

  	//After 1.5 seconds, the door lock servo turns off to avoid stall current
  	setTimeout(function(){motor.servoWrite(0)}, 1500);
}

@Kefbr the error message contains a reference to line 62 of your sketch i.e. the definition of the Blynk connection with the Blynk token at line 42. The token declaration looks ok but I have had occasions where hidden characters appear in sketches / scripts if you copy and paste code.

Double check that the token is exactly as it’s shown in your sketch.

Thank you for the quick response and your attention. this same code worked before I formatted my raspberry. The problem would be in the token that blynk gave me?

When I test my project without this.,then he dont crash…

check this is what it appears to be.

i do not know the rpi version of the lib, but in arduino the token has to be a char array, not just a string.

Token format is fine for Pi’s

@Costas What I do now? image

Are you sure the script you have is the same as it was before you reformatted the SD card?

Was it the same token or is this a new token?

Are you doing SSL or TCP?

Yes, i copy before i formated

It the same token i dont make any change

I dont know how i see this.

image

You will be SSL as you are using the cloud server, TCP is mainly used for local server.

The only thing I can suggest is to ensure there are no hidden characters in the token. It can happen when you cut and paste text.

Maybe take a new token and remove all from ‘xxxxxxxxxxxx’ and manually type it in.

@Costas
I use SSL. i try make a new project and take a new token… i type it manually and aigan same error. =/

Can you recommend me another way to connect to blynk?

90% of what we do is with the WeMos D1 Mini. Around $5 a piece or less if you buy the WeMos clones.

I understand, I’m surprised because before I formatted it was working. And how is that possible? Thx for helping

comic … I searched the internet for a ready project of anything, installed all the modules and called. To my surprise it worked quietly.
I thought to myself: what if I put my elbow on top of this created code?
was what I did, put all the variaves and everything right (just did not bring the variable that starts the blynk)
And guess what? It worked … follow the code …
If you analyze, there is NO difference.
How is this possible? LOL

var unlockedState = 1000;
var lockedState = 2200;

var motorPin = 14;
var buttonPin = 4
var ledPin = 17
var relay1 = 22
var relay2 = 23
var blynkLib = require('blynk-library');
var sensorLib = require('node-dht-sensor');

var AUTH = 'CODECORRECT!';

// Setup Blynk
var blynk = new blynkLib.Blynk(AUTH);

// *** Start code *** //

var locked = true

//Setup servo
var Gpio = require('pigpio').Gpio,
  motor = new Gpio(motorPin, {mode: Gpio.OUTPUT}),
  button = new Gpio(buttonPin, {
    mode: Gpio.INPUT,
    pullUpDown: Gpio.PUD_DOWN,
    edge: Gpio.FALLING_EDGE
  }),
  led = new Gpio(ledPin, {mode: Gpio.OUTPUT});
  relay1 = new Gpio(relay1, {mode: Gpio.OUTPUT});
  relay2 = new Gpio(relay2, {mode: Gpio.OUTPUT});
  
 //Setup blynk
var v0 = new blynk.VirtualPin(0);
var v1 = new blynk.VirtualPin(1);
var v2 = new blynk.VirtualPin(2);

console.log("locking door")
lockDoor()
button.on('interrupt', function (level) {
	console.log("level: " + level + " locked: " + locked)
	if (level == 0) {
		if (locked) {
			unlockDoor()
		} else {
			lockDoor()
		}
	}
});

v0.on('write', function(param) {
	console.log('V0:', param);
  	if (param[0] === '0') { //unlocked
  		unlockDoor()
  	} else if (param[0] === '1') { //locked
  		lockDoor()
  	} else {
  		blynk.notify("Door lock button was pressed with unknown parameter");
  	}
});

v1.on('write', function(param) {
	console.log('V1:', param);
  	if (param[0] === '0') { //unlocked
  		relay1d()
  	} else if (param[0] === '1') { //locked
  		relay1l()
  	} else {
  		blynk.notify("Door lock button was pressed with unknown parameter");
  	}
});

v2.on('write', function(param) {
	console.log('V2:', param);
  	if (param[0] === '0') { //unlocked
  		relay2d()
  	} else if (param[0] === '1') { //locked
  		relay2l()
  	} else {
  		blynk.notify("Door lock button was pressed with unknown parameter");
  	}
});

blynk.on('connect', function() { console.log("Blynk ready."); });
blynk.on('disconnect', function() { console.log("DISCONNECT"); });

function relay1l() {
	relay1.digitalWrite(1);
	//notify
  	blynk.notify("Radio desligado");
	
}

function relay1d() {
	relay1.digitalWrite(0);
	//notify
  	blynk.notify("Radio ligado");
	
}

function relay2l() {
	relay2.digitalWrite(1);
	//notify
  	blynk.notify("Luz 2 apagada");
	
}

function relay2d() {
	relay2.digitalWrite(0);
	//notify
  	blynk.notify("Luz 2 acesa");
	
}

function lockDoor() {
	motor.servoWrite(lockedState);
	led.digitalWrite(0);
	locked = true

	//notify
  	blynk.notify("Porta esta fechada!");
  	
  	//After 1.5 seconds, the door lock servo turns off to avoid stall current
  	setTimeout(function(){motor.servoWrite(0)}, 1500)
}

function unlockDoor() {
	motor.servoWrite(unlockedState);
	led.digitalWrite(1);
	locked = false

	//notify
  	blynk.notify("Porta esta aberta!"); 

  	//After 1.5 seconds, the door lock servo turns off to avoid stall current
  	setTimeout(function(){motor.servoWrite(0)}, 1500);
}

@Costas
In your post : [SOLVED] How to run Blynk.run() only when WiFi connection is established

U use void setup and void loop , can I use it without setup and loop?

Yes, the sketch you refer to is C++ code rather than a nodejs script.

I did write a thread about using C++ with Blynk on the Pi but I think it’s easier to stick with nodejs.