Advertisement
Guest User

Untitled

a guest
May 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. // ************* PARAMETERS *************** //
  4. //
  5. // unlockedState and lockedState:
  6. // in microseconds.
  7. //
  8. // motorPin:
  9. // The GPIO pin the signal wire on your servo
  10. // is connected to
  11. //
  12. // buttonPin:
  13. // The GPIO pin the signal wire on button
  14. // is connected to. It is okay to have no button connected
  15. //
  16. // ledPin:
  17. // The GPIO pin led is connected
  18. //
  19. // blynkToken:
  20. // My token generated with project
  21. //
  22. // **************************************** //
  23. var unlockedState = 800;
  24. var lockedState = 2000;
  25.  
  26. var motorPin = 14;
  27. var buttonPin = 4
  28. var ledPin = 17
  29.  
  30. var blynkToken = 'b210ec614a5c44b586ce3812737cf418';
  31.  
  32. // *** Start code *** //
  33.  
  34. var locked = true
  35.  
  36. //Setup servo
  37. var Gpio = require('pigpio').Gpio,
  38. motor = new Gpio(motorPin, {mode: Gpio.OUTPUT}),
  39. button = new Gpio(buttonPin, {
  40. mode: Gpio.INPUT,
  41. pullUpDown: Gpio.PUD_DOWN,
  42. edge: Gpio.FALLING_EDGE
  43. }),
  44. led = new Gpio(ledPin, {mode: Gpio.OUTPUT});
  45.  
  46. //Setup blynk
  47. var Blynk = require('blynk-library');
  48. var blynk = new Blynk.Blynk(blynkToken);
  49. var v0 = new blynk.VirtualPin(0);
  50.  
  51. console.log("locking door")
  52. lockDoor()
  53.  
  54. button.on('interrupt', function (level) {
  55. console.log("level: " + level + " locked: " + locked)
  56. if (level == 0) {
  57. if (locked) {
  58. unlockDoor()
  59. } else {
  60. lockDoor()
  61. }
  62. }
  63. });
  64.  
  65. v0.on('write', function(param) {
  66. console.log('V0:', param);
  67. if (param[0] === '0') { //unlocked
  68. unlockDoor()
  69. } else if (param[0] === '1') { //locked
  70. lockDoor()
  71. } else {
  72. blynk.notify("Door lock button was pressed with unknown parameter");
  73. }
  74. });
  75.  
  76. blynk.on('connect', function() { console.log("Blynk ready."); });
  77. blynk.on('disconnect', function() { console.log("DISCONNECT"); });
  78.  
  79. function lockDoor() {
  80. motor.servoWrite(lockedState);
  81. led.digitalWrite(1);
  82. locked = true
  83.  
  84. //notify
  85. blynk.notify("Door has been locked!");
  86.  
  87. //After 1.5 seconds, the door lock servo turns off to avoid stall
  88. setTimeout(function(){motor.servoWrite(0)}, 1500)
  89. }
  90.  
  91. function unlockDoor() {
  92. motor.servoWrite(unlockedState);
  93. led.digitalWrite(0);
  94. locked = false
  95.  
  96. //notify
  97. blynk.notify("Door has been unlocked!");
  98.  
  99. //After 1.5 seconds, the door lock servo turns off to avoid stall current
  100. setTimeout(function(){motor.servoWrite(0)}, 1500)
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement