DND - Blog Post: Controlling an Arduino with Node.js
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
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); | |
} | |
} | |
} |
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
{SerialPort} = require('serialport') | |
fs = require 'fs' | |
port = '/dev/tty.usbserial-A600enDA' | |
serial = null | |
value = 0x00 | |
toggle = => | |
value = if value == 0x00 then 0x01 else 0x00 | |
serial.write new Buffer([value]) | |
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 | |
setInterval toggle, 1000 |
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
{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) |
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
<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