RPI上的Javascript:GPIOCtrl不是一个函数

时间:2017-10-20 14:44:06

标签: javascript raspberry-pi

大家好我正在尝试使用以下

创建一个门锁
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);}}

然后是以下内容:

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');

当我运行程序时,我得到消息应用服务器在端口3000运行,但是当我调用它时,我收到以下消息:

TypeError: GPIOCtrl.unlock is not a function

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您必须在controller.js

export模块中library
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);
    }
  }
}