Issue
Hi all I am trying to create a door lock using the following
var gpio = require("pi-gpio");
var gpioPin = 4;
var isOpened=false;
var unlock = function(){
if(!isOpened){
/* Open the door lock */
gpio.write(gpioPin, 1, function() {
isOpened = true;
});
/*Lock door in 2 seconds*/
setTimeout(function () {
gpio.write(gpioPin, 0, function() {
isOpened = false;
});
}, 2000);}}
then the following:
var http = require('http'); var express = require('express');
var app = express();
var GPIOCtrl = require('./controller.js');
app.get('/unlock/', function(req, res){
GPIOCtrl.unlock();
});
app.listen(3000);
console.log('App Server running at port 3000');
When I run the program I get the message app server running at port 3000, however when I call it I get the following message:
TypeError: GPIOCtrl.unlock is not a function
What am I doing wrong?
Solution
You have to export your module as following in your controller.js
:
var gpio = require("pi-gpio"),
gpioPin = 4,
isOpened = false,
module.exports = {
unlock: function() {
if (!isOpened) {
/* Open the door lock */
gpio.write(gpioPin, 1, function() {
isOpened = true;
});
/*Lock door in 2 seconds*/
setTimeout(function() {
gpio.write(gpioPin, 0, function() {
isOpened = false;
});
}, 2000);
}
}
}
Answered By - Serge K.