I’m using a raspberry pi to scan for BLE beacons and perform an action (unlock/lock door). I’ve got the process of scanning the beacons and their information into my node JS file, but I can’t get it to work with blynk. For eaxmple, I have two virtual buttons set up. One to begin scanning/stop scanning of beacons and the other to manually control the motor. When I begin scanning by pressing virtual button v1, I have no output until I change the state of v0. And even then, it’s just blocks of the scan and not continuous. Am I missing something about processes in Node JS? Ultimately I want the app user to be able to choose: do I want beacons controlling the lock or do I want to manually press the button each time. So two states. The state of continuous scanning, which I will fine later on with an array or something. And the state of manual pressing of virtual pin v0.
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') { //enable BLE return
//Bleacon.startScanning(uuid);
console.log('turning on scan..')
Bleacon.on('discover', function(bleacon) {
if (bleacon.uuid == uuid) {
console.log("uuid matches");
if (bleacon.proximity == 'immediate') {
//console.log('immediate proximity: ' + bleacon.rssi);
console.log("unlock");
unlockDoor();
} else if (bleacon.proximity == 'near') {
console.log('near proximity: ' + bleacon.rssi);
console.log("lock");
lockDoor();
}
}
});
} else if (param[0] == '1') { //not scanning
console.log('stop scanning');
Bleacon.stopScanning();
} else {
blynk.notify("Door lock button was pressed with unknown parameter");
}
//console.log('bleacon found: ' + JSON.stringify(bleacon));
});
blynk.on('connect', function() {
//console.log("Blynk ready.");
});
My Terminal Output:
login as: pi
pi@192.168.1.2's password:
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Mar 19 16:50:07 2017
SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
pi@raspberrypi:~ $ cd /home/pi/DoorBeacon/node-bleacon/
pi@raspberrypi:~/DoorBeacon/node-bleacon $ sudo node doorlock.js
scanning...
OnOff mode
Connecting to: blynk-cloud.com 8441
locking door
SSL authorization...
Connected
Authorized
V0: [ '1' ]
V1: [ '0' ]
turning on scan..
V0: [ '0' ]
uuid matches
near proximity: -88
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -93
lock
uuid matches
near proximity: -92
lock
uuid matches
near proximity: -96
lock
uuid matches
near proximity: -99
lock
uuid matches
near proximity: -84
lock
uuid matches
near proximity: -83
lock
uuid matches
near proximity: -85
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -74
lock
uuid matches
near proximity: -89
lock
uuid matches
near proximity: -90
lock
uuid matches
near proximity: -89
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -78
lock
uuid matches
near proximity: -88
lock
uuid matches
near proximity: -71
lock
uuid matches
near proximity: -89
lock
uuid matches
near proximity: -77
lock
uuid matches
near proximity: -72
lock
uuid matches
near proximity: -79
lock
uuid matches
near proximity: -75
lock
uuid matches
near proximity: -86
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -78
lock
uuid matches
near proximity: -84
lock
uuid matches
near proximity: -74
lock
uuid matches
near proximity: -79
lock
uuid matches
near proximity: -85
lock
uuid matches
near proximity: -72
lock
uuid matches
near proximity: -70
lock
uuid matches
near proximity: -80
lock
uuid matches
near proximity: -79
lock
^C2017-03-19 16:57:27 sigHandler: Unhandled signal 2, terminating