Skip to content

Commit

Permalink
Added web control
Browse files Browse the repository at this point in the history
  • Loading branch information
jazeee committed Jan 1, 2016
1 parent 19fd227 commit db840b2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
49 changes: 47 additions & 2 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
gpio = require "pi-gpio"
Promise = require "bluebird"
http = require "http"
dispatcher = require "httpdispatcher"
fs = require "fs"

Promise.promisifyAll gpio

Expand All @@ -13,6 +16,46 @@ if isReverseDirection
ports = [12, 16, 7, 18]
outputs = [1, 1, 0, 0]

dispatcher.onGet "/up", (request, response) ->
response.writeHead 200, {'Content-type': "text/plain"}
response.end "Moving Up"
moveMotor true
dispatcher.onGet "/down", (request, response) ->
response.writeHead 200, {'Content-type': "text/plain"}
response.end "Moving Down"
moveMotor false
dispatcher.onGet "/stop", (request, response) ->
response.writeHead 200, {'Content-type': "text/plain"}
response.end "Stopping"
clearInterval motorInterval

dispatcher.onGet "/", (request, response) ->
index = fs.createReadStream "index.html"
index.pipe response

server = http.createServer (request, response) ->
try
console.log request.url
dispatcher.dispatch request, response
catch error
console.error error

motorInterval = undefined

moveMotor = (isReverseDirection) ->
ports = [18, 16, 7, 12]
if isReverseDirection
ports = [12, 16, 7, 18]
clearInterval motorInterval
motorInterval = setInterval( ->
for port, index in ports
output = outputs[index]
if isDebug
console.log port, "->", output
gpio.writeAsync port, output
outputs= [outputs.splice(1)..., outputs...]
1)

Promise.all do ->
portsToOpen = []
for port in ports
Expand All @@ -29,8 +72,7 @@ Promise.all do ->
gpio.writeAsync port, output
outputs= [outputs.splice(1)..., outputs...]
i++
if i > 10000
closePorts()
if i > 10
clearInterval interval
1)
.catch ->
Expand All @@ -40,3 +82,6 @@ closePorts = ->
console.log "********** Closing **********"
for port in ports
gpio.close port

server.listen 8000, ->
console.log "Listenting on port 8000"
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
</head>
<body>
<h1>Chopper</h1>
<button value="up">Up</button>
<button value="down">Down</button>
<button value="stop">Stop</button>

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
console.log("Start");
$("button").click(
function(event){
console.log(event);
$.get("/" + event.currentTarget.value)
});
</script>
</body>

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "NodeRaspberryPi",
"dependencies": {
"bluebird": "^3.1.1",
"httpdispatcher": "^0.4.0",
"pi-gpio": "0.0.8"
}
}

0 comments on commit db840b2

Please sign in to comment.