Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- // ************* PARAMETERS *************** //
- //
- // unlockedState and lockedState:
- // in microseconds.
- //
- // motorPin:
- // The GPIO pin the signal wire on your servo
- // is connected to
- //
- // buttonPin:
- // The GPIO pin the signal wire on button
- // is connected to. It is okay to have no button connected
- //
- // ledPin:
- // The GPIO pin led is connected
- //
- // blynkToken:
- // My token generated with project
- //
- // **************************************** //
- var unlockedState = 800;
- var lockedState = 2000;
- var motorPin = 14;
- var buttonPin = 4
- var ledPin = 17
- var blynkToken = 'b210ec614a5c44b586ce3812737cf418';
- // *** 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);
- 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
- 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)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement