diff --git a/slave/photocellServer.js b/slave/photocellServer.js new file mode 100755 index 0000000..2960d15 --- /dev/null +++ b/slave/photocellServer.js @@ -0,0 +1,75 @@ +/** + * Created by rpodiuc on 8/9/14. + */ +var http = require("http"); +var url = require("url"); +var express = require('express'); +var getRawBody = require('raw-body'); +var cors = require('cors'); +var gpio = require("pi-gpio"); + +var argv = process.argv; +var port = argv[2]; +var pin = argv[3]; +if (pin == undefined) + pin = 7; +console.log("arguments are port", port, " pin", pin); +var app = express(); + +//handle post/put +app.use(function (req, res, next) { + getRawBody(req, { + length: req.headers['content-length'], + limit: '1mb', + encoding: 'utf8' + }, function (err, string) { + if (err) + return next(err); + req.text = string; + next(); + }) +}); + +app.use(cors()); + +function setDirection(pin){ + console.log("I am in set dirrection"); + gpio.open(pin, "input", function(err) { + console.log("---set direction to pin ", pin, " to input---"); + console.log("---error is ------:", err); + setInterval(function(){readLightState(pin);},3000); + //gpio.setDirection(pin, "input" , function(){ + //console.log("-----i am in set dirrection------"); + //readLightState(pin, undefined); + //}); + }); +} + +function readLightState(pin, res) { + gpio.read(pin, function(err, value) { + console.log("READ pin------------------", pin, " value-----", value); + if(err) throw err; + console.log("on pin" + pin + " state is: " , value); // The current state of the pin + var state = value; + if (res !== undefined) { + console.log("send a response with led state: ", value); + if (state == 1) { + res.send("something's there ^_^"); + } else { + res.send("nothing detected"); + } + } + return state; + //gpio.close(pin); + }); +} + +app.get("/", function(req, res) { + console.log("GET /, led state is: ", ledState); + readLightState(pin, res); +}) + + +app.listen(port); +setDirection(pin); +//startReading(pin); diff --git a/slave/proximityServer.js b/slave/proximityServer.js index a0896ef..bf4409b 100644 --- a/slave/proximityServer.js +++ b/slave/proximityServer.js @@ -37,7 +37,7 @@ function setDirection(pin){ setInterval(function(){readProximityState(pin);},3000); //gpio.setDirection(pin, "input" , function(){ //console.log("-----i am in set dirrection------"); - //readProximityState(pin, undefined); + //readLightState(pin, undefined); //}); }); } @@ -56,6 +56,7 @@ function readProximityState(pin, res) { res.send("nothing detected"); } } + return state; //gpio.close(pin); }); } @@ -65,13 +66,6 @@ app.get("/", function(req, res) { readProximityState(pin, res); }) -function startReading(pin) -{ - //setDirection(pin); - console.log("-----------proximity sensor on--------------"); - - setInterval(readProximityState(pin),3000); -} app.listen(port); setDirection(pin);