Created
March 31, 2014 11:21
-
-
Save mohitathwani/9890177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//We first load the express framework and make it available for our script | |
var express = require('express'); | |
//We load the pi-gpio library here | |
var gpio = require("pi-gpio"); | |
//Here we initialize the express framework | |
var app = express(); | |
//We are going to GET the pin number and the state to set the pin to from the URL from the browser | |
//For example http://yourdomain.com/25/1 -----> This turns the state of pin number 25 to HIGH | |
//Similarly: http://yourdomain.com/25/0 -----> This turns the state of pin number 25 to LOW | |
//NOTE: We can also send a json packet to the server as part of this request, but we won't cover that for now ! | |
app.get('/:pin/:state', function(req, res) { | |
//We need to convert the pin and state to integers so that they can be given as input to the pi-gpio library | |
var pin = parseInt(req.params.pin); | |
var state = parseInt(req.params.state); | |
//This is the code block that will connect to the GPIO pin and turn it On/Off | |
gpio.open(pin, "output", function(err){ | |
gpio.write(pin, state, function(){ | |
gpio.close(pin); | |
}); | |
}); | |
res.send({pin:pin, state: state}); | |
}); | |
//We start the server on port number 3000 and allow it work on all domains. | |
app.listen(4000, '0.0.0.0'); | |
console.log('Listening on port 4000...'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment