如何将覆盆子pi连接到网络服务器

时间:2018-02-23 14:55:45

标签: javascript raspberry-pi webserver

经过一些编码,我得到了覆盆子pi。所以我可以用html上的2个按钮控制伺服开启和关闭。但这是在本地服务器上(raspberrpiIP:3000),我想在实时网站上控制树莓派。所以世界上每个人都应该能够控制它。经过大量研究后,我无法找到将我的覆盆子pi与脚本连接到实时网站的正确答案。这是用于控制伺服和连接到互联网的脚本。



var http = require('http');
var express = require('express');

var lockedState = 2200;
var unlockedState = 500;

var motorPin = 14;
var buttonPin = 17;

var app = express();

// *** Start code *** //

var locked = true;

//Setup servo
var Gpio = require(
        motor = new Gpio(motorPin, {mode: Gpio.OUTPUT});
//setup button
button = new Gpio(buttonPin, {mode: Gpio.INPUT,
        pullUpDown: Gpio.PUD_DOWN,
        edge: Gpio.EITHER_EDGE
  }),
  servo = new Gpio(motorPin, {mode: Gpio.OUTPUT});

button.on('interrupt', function () {
  motor.servoWrite(lockedState);
 setTimeout(function(){motor.servoWrite(0)}, 2000);
});



// ------------------------------------------------------------------------
// configure Express to serve index.html and any other static pages stored
// in the home directory
app.use(express.static(__dirname));


//lock rest get call
app.get('/uit', function (req, res) {
        motor.servoWrite(lockedState);
        locked = true;
        res.send('uit')
        console.log('uit gezet')
        //After 1.5 seconds, the door lock servo turns off to avoid stall current
 setTimeout(function(){motor.servoWrite(0)}, 2000);
 });

//unlock rest get call
app.get('/aan', function (req, res){
        motor.servoWrite(unlockedState);
        locked = false;
        res.send('aan')
        console.log('aan gezet')
        //After 1.5 seconds, the door lock servo turns off to avoid stall current
        setTimeout(function(){motor.servoWrite(0)}, 2000)


 });
// ------------------------------------------------------------------------
// Start Express App Server
//
app.listen(3000);
console.log('App Server is listening on port 3000');




0 个答案:

没有答案