Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
DND - Blog Post: Controlling an Arduino with Node.js
const int outputPin = 13;
void setup()
{
pinMode(outputPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
int incomingByte = Serial.read();
if (incomingByte == 0x01) {
digitalWrite(outputPin, HIGH);
} else if (incomingByte == 0x00) {
digitalWrite(outputPin, LOW);
}
}
}
{SerialPort} = require('serialport')
fs = require 'fs'
port = '/dev/tty.usbserial-A600enDA'
express = require 'express'
serial = null
interval = null
lightOn = false
turnOn = =>
lightOn = true
serial.write new Buffer([0x01])
turnOff = =>
lightOn = false
serial.write new Buffer([0x00])
toggle = =>
if lightOn
turnOff()
else
turnOn()
app = express.createServer()
app.get '/', (req, res) ->
res.sendfile 'index.htm'
app.get '/on', (req, res) ->
clearInterval interval
turnOn()
res.end()
app.get '/off', (req, res) ->
clearInterval interval
turnOff()
res.end()
app.get '/blink', (req, res) ->
clearInterval interval
interval = setInterval toggle, 500
res.end()
console.log "Starting..."
fs.stat port, (err, stats) ->
if err?
console.log "Couldn't stat #{port}"
process.exit()
console.log "Started."
serial = new SerialPort port, baudrate: 9600
app.listen(8080)
<html>
<head>
<title>Online Lightswitch</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
$("#on").click(function() {
$.ajax('/on');
});
$("#off").click(function() {
$.ajax('/off');
});
$("#blink").click(function() {
$.ajax('/blink');
});
});
</script>
<style>
button {
font-size: 20pt;
}
</style>
</head>
<body>
<button id="on">On!</button>
<button id="off">Off!</button>
<button id="blink">Blink!</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment