Closed nexuspcs closed 1 year ago
Here is my doorlock.js
file:
var BLYNK_TEMPLATE_ID = 'TMPLISLOZuxh'
var BLYNK_DEVICE_NAME = 'Door1Front'
var BLYNK_AUTH_TOKEN = 'my token is in here btw I have just hidden it for security purposes'
var SERVER_ADDR = 'https://sgp1.blynk.cloud/'
//*** 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 blynkToken = 'my token is in here btw I have just hidden it for security purposes';
// *** 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});
//Setup blynk
var Blynk = require('blynk-library');
var blynk = new Blynk.Blynk(blynkToken, options = {
connector : new Blynk.TcpClient()
});
var v0 = new blynk.VirtualPin(0);
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");
}
});
blynk.on('connect', function() { console.log("Blynk ready."); });
blynk.on('disconnect', function() { console.log("DISCONNECT"); });
function lockDoor() {
motor.servoWrite(lockedState);
led.digitalWrite(1);
locked = true
//notify
blynk.notify("Door has been locked!");
//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(0);
locked = false
//notify
blynk.notify("Door has been unlocked!");
//After 1.5 seconds, the door lock servo turns off to avoid stall current
setTimeout(function(){motor.servoWrite(0)}, 1500)
}
Changing to TCP results in a constant disconnect/reconnect cycle...
Look at the output below
doorpi@doorpi:~/myapp $ sudo node doorlock.js
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/
Give Blynk a Github star! => https://github.com/vshymanskyy/blynk-library-js
Connecting to TCP: blynk.cloud 80
locking door
Connected
Authorized
Blynk ready.
Disconnect blynk
DISCONNECT
REARMING DISCONNECT
Connecting to TCP: blynk.cloud 80
Connected
Authorized
Blynk ready.
Disconnect blynk
DISCONNECT
REARMING DISCONNECT
Connecting to TCP: blynk.cloud 80
Connected
Authorized
Blynk ready.
Disconnect blynk
DISCONNECT
REARMING DISCONNECT
Connecting to TCP: blynk.cloud 80
Connected
Authorized
Blynk ready.
Disconnect blynk
DISCONNECT
REARMING DISCONNECT
Connecting to TCP: blynk.cloud 80
Connected
Authorized
Blynk ready.
Disconnect blynk
DISCONNECT
REARMING DISCONNECT
Connecting to TCP: blynk.cloud 80
Connected
Authorized
Blynk ready.
Disconnect blynk
DISCONNECT
REARMING DISCONNECT
Connecting to TCP: blynk.cloud 80
Connected
Authorized
Blynk ready.
Hello??
If you would like to use a raspberry pi, I'd suggest using the latest version of the python library (1.0.0) instead.
Please use https://community.blynk.cc/ for such questions
Blynk library version: v.1.10 IDE: VSC IDE version: v.1.69 Board type: Raspberry Pi Model 3B+ Additional modules: Node.js / NPM
Scenario, steps to reproduce
Upon attempting to run my js file, I am greeted with an error which states that my token is invalid. I have tried creating new devices on the Blynk dashboard and updating my token, with no luck at all.
Expected Result
I was hoping that the Pi would connect the the Blynk dashboard.
Actual Result
When I run my JS file, it connects for a split second, and then disconnects: Here is the output: