nodejs로 디바이스 통신하기

17

Upload: taeyoung-kim

Post on 13-Jan-2015

731 views

Category:

Technology


8 download

DESCRIPTION

Node.js 로 디바이스 통신하기

TRANSCRIPT

Page 2: NodeJs로 디바이스 통신하기
Page 3: NodeJs로 디바이스 통신하기
Page 4: NodeJs로 디바이스 통신하기
Page 5: NodeJs로 디바이스 통신하기
Page 6: NodeJs로 디바이스 통신하기

아두이노(Arduino)는 오픈소스를 기반으로 한 단일 보드마이크로 컨트롤러이다.AVR을 기반으로 한 보드로 이루어져 있고 최근에는Cortex-M3를 이용한 제품(Arduino Due)도 있다.소프트웨어 개발을 위한 통합 환경(IDE)이 있다. 아두이노는 다수의 스위치나 센서로부터 값을 받아들여, LED나 모터와 같은 외부 전자 장치들을 통제함으로써환경과 상호작용이 가능한 물건을 만들어낼 수 있다. 또한 플래시, 프로세싱, Max/MSP와 같은 소프트웨어를 연동할 수 있다.[3]

아두이노의 가장 큰 장점은 마이크로컨트롤러를 쉽게 동작시킬 수 있다는 것이다. 일반적으로 AVR 프로그래밍이WinAVR로 컴파일하여, ISP장치를 통해 업로드를 해야하는 등 번거로운 과정을 거쳐야하는데 비해, 아두이노는 컴파일된 펌웨어를 USB를 통해 업로드를 쉽게 할 수 있다. 또한 아두이노는 다른 모듈에 비해 비교적 저렴하고, 윈도를 비롯해 맥 OS X, 리눅스와 같은 여러 OS를 모두 지원한다. 아두이노 보드의 회로도가 CCL에 따라 공개되어 있으므로, 누구나 직접 보드를 직접 만들고 수정할 수 있다.

Page 7: NodeJs로 디바이스 통신하기

• 통신 방식 : RS-232 Serial via USB

• 패킷 : [전체][빨강][노랑][초록]\n\r• 0 : 그대로

• 1 : 끄기

• 2 : 켜기

https://github.com/theRichu/node_arduinio_example/blob/master/arduino/device.ino

Page 8: NodeJs로 디바이스 통신하기
Page 9: NodeJs로 디바이스 통신하기

$ npm update$ npm install –g express$ npm install -g express-generator$ express server$ cd server$ npm install$ npm start• http://localhost:3000

app.get('/', function (request, response) {

fs.readFile('index.html', function (error, data) {

response.send(data.toString());

});

});

https://github.com/theRichu/node_arduinio_example/blob/master/node_arduino_example/public/index.html

Page 10: NodeJs로 디바이스 통신하기

$ npm install serialport

var serialport = require("serialport");var SerialPort = serialport.SerialPort;

var sp = new SerialPort("/dev/ttyACM1", {baudrate: 9600,parser: serialport.parsers.readline("\n")

});

var onoff = 0;var state_r = 0;var state_y = 0;var state_g = 0;

sp.on("data", function (data) {onoff = data[0];state_r = data[1];state_y = data[2];state_g = data[3];

io.sockets.emit('status', { "onoff": onoff, "red": state_r,"yellow": state_y,"green": state_g });});

Page 11: NodeJs로 디바이스 통신하기

$ npm install socket.io

var server = http.createServer(app);var io = require('socket.io').listen(server);

io.sockets.on('connection', function (socket) {

socket.on('status', function (data) {

sp.write("0000\n\r");

});

socket.on('on', function (data) {

sp.write("2000\n\r");

});

socket.on('off', function (data) {

sp.write("1000\n\r");

});

socket.on('red', function (data) {

if(data['status']=="toggle"){

if(state_r=='1') sp.write("0100\n\r");

else sp.write("0200\n\r");

}else if(data['status']=="on"){

sp.write("0200\n\r");

}else if(data['status']=="off"){

sp.write("0100\n\r");

}

});

socket.emit('status', { "red": state_r,"yellow": state_y,"green": state_g });

});

Page 12: NodeJs로 디바이스 통신하기
Page 13: NodeJs로 디바이스 통신하기

https://github.com/theRichu/node_arduinio_example/blob/master/node_arduino_example/public/index.html

<div data-role="page"><div data-role="header">

<h1>OctoberSky Week1

</h1></div><ul data-role="content"><div class="containing-element">

<select name="flip-min" id="flip-min" data-role="slider"><option value="off">Off</option><option value="on">On</option></select>

</div><div data-role="controlgroup" data-type="horizontal">

<a href="#" id="red_toggle" data-role="button"> red </a><a href="#" id="yellow_toggle" data-role="button"> yellow </a><a href="#" id="green_toggle" data-role="button"> green </a>

</div></ul>

</div>

Page 14: NodeJs로 디바이스 통신하기

https://github.com/theRichu/node_arduinio_example/blob/master/node_arduino_example/public/index.html

var socket = io.connect('http://localhost:52273');

socket.emit('status');

socket.on('status', function (data) {

if(data['onoff']=="1"){$('#flip-min').val("on");}

else{$('#flip-min').val("off");}

$('#flip-min').slider('refresh');

if(data['red']=="1"){$('#red_toggle').attr("style", "background: red;");}

else{$('#red_toggle').attr("style", "background: gray;");}

$( "#flip-min" ).on( "change", function(event, ui) {

if($("#flip-min").val()=="on"){socket.emit('on');}

else{socket.emit('off');}

});

$("#red_toggle").click(function(){

socket.emit('red', {status: 'toggle'});

});

});

Page 15: NodeJs로 디바이스 통신하기
Page 16: NodeJs로 디바이스 통신하기