blynkkk / blynk-library

Blynk library for IoT boards. Works with Arduino, ESP32, ESP8266, Raspberry Pi, Particle, ARM Mbed, etc.
https://blynk.io
MIT License
3.81k stars 1.38k forks source link

Helped need with connecting #569

Closed nexuspcs closed 1 year ago

nexuspcs commented 1 year ago

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:

doorpi@doorpi:~/myapp $ sudo node doorlock.js 

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/

  Give Blynk a Github star! => https://github.com/vshymanskyy/blynk-library-js

Connecting to TCP: blynk-cloud.com 80
locking door
Connected
Could not login: INVALID_TOKEN
node:events:518
    throw err; // Unhandled 'error' event
    ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('INVALID_TOKEN')
    at new NodeError (node:internal/errors:372:5)
    at Blynk.emit (node:events:516:17)
    at Blynk.onReceive (/home/doorpi/myapp/node_modules/blynk-library/blynk.js:466:20)
    at exports.TcpClient.<anonymous> (/home/doorpi/myapp/node_modules/blynk-library/blynk.js:607:50)
    at exports.TcpClient.emit (node:events:527:28)
    at Socket.<anonymous> (/home/doorpi/myapp/node_modules/blynk-library/blynk-node.js:50:14)
    at Socket.emit (node:events:527:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:285:11)
    at Socket.Readable.push (node:internal/streams/readable:228:10) {
  code: 'ERR_UNHANDLED_ERROR',
  context: 'INVALID_TOKEN'
}
doorpi@doorpi:~/myapp $ 
nexuspcs commented 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)
}
nexuspcs commented 1 year ago

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.
nexuspcs commented 1 year ago

Hello??

John93-Blynk commented 1 year ago

If you would like to use a raspberry pi, I'd suggest using the latest version of the python library (1.0.0) instead.

vshymanskyy commented 1 year ago

Please use https://community.blynk.cc/ for such questions