이전 포스트에서 라즈베리파이와 아두이노를 시리얼로 연결하여 프로그램을 작성하거나 업로드하는 부분까지 보았고, 이어서
라즈베리파이에 Node.js와 Express를 설치하여 아두이노의 LED 센서를 웹화면을 통하여 켜고/끄는 부분까지 진행해 보도록 하겠습니다.
1. 라즈베리 파이에 Node.js 설치하기
1) Node 최신 패키지 다운로드
$ sudo wget http://node-arm.herokuapp.com/node_latest_armhf.deb
2) 패키지 설치
$ sudo dpkg -i node_latest_armhf.deb
※ apt-get install을 통하여 설치할 경우 old 버젼이 설치됨으로 인해 이후 ExpressJS 설치 및 App 생성 시 npm 버젼 문제로 오류로 진행이 안됩니다.
2. ExpressJS 설치
1) NPM을 이용하여 express 설치
$ sudo npm install -g express
$ sudo npm install -g express-generator
2) Express App 생성
$ express --ejs myapp
$ cd myapp && npm install
※ template engine을 ejs로 사용하기 위하여 옵션을 붙였습니다.
3. LED 제어 웹 화면 생성
1) LED 제어용 Routes 추가 : ~/myapp/routes/led.js
http://xxx.xxx.xxx.xxx:3000/led/on : LED켜기
http://xxx.xxx.xxx.xxx:3000/led/ooff : LED끄기
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('led', { title: 'LED Control' });
});
router.get('/:switch', function(req, res, next) {
var onoff = req.params.switch;
if (onoff == 'on') setLED(1);
if (onoff == 'off') setLED(0);
res.render('led', { title: 'LED Control : ' + req.params.switch });
});
module.exports = router;
// LED 제어 function
function setLED(flag) {
var fs = require('fs');
fs.open('/dev/ttyUSB0','a', 666, function(e, fd) {
fs.write(fd, flag ? '1' : '0', null, null, null, function() {
fs.close(fd, function() { });
});
});
}
※ /led/on 이나 /led/off 시 아두이노의 LED를 제어하기 위한 Serial 신호를 전송합니다.
2) LED routes 을 로딩하도록 App.js 수정 : ~/myapp/app.js
var routes = require('./routes/index');
var users = require('./routes/users');
var led = require('./routes/led');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
app.use('/led', led);
※ express 로 프로젝트 생성 시 기본으로 생성되는 app.js에 두줄을 추가하였습니다.
- http://주소:포토/led 의 주소를 위에서 설정한 led.js routes 에서 처리하도록 설정합니다.
3) /led URL을 위한 EJS View를 생성 : ~/myapp/views/led.ejs
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Control 아두이노 LEF <%= title %></p>
<a href="/led/on">LED 켜기</a>
<a href="/led/off">LED 끄기</a>
</body>
</html>
4. 실행
1) myapp 실행
$ DEBUG=myapp:* ./bin/www
2) myapp에 브라우져 접속 : https://ip:port/led/on
LED켜기, LED끄기를 누르면 13번 pin에 연결된 LED가 제어됩니다.
'raspberry pi' 카테고리의 다른 글
라즈베리파이 + 아두이노 + NodeJS (4) | 2015.03.29 |
---|---|
라즈베리파이 + 아두이노 Serial 연결 (0) | 2015.03.29 |
아두이노 중국산 USB2.0 To Serial 드라이버 (0) | 2015.03.17 |
댓글을 달아 주세요
프로젝트 진행중인데 도움좀 부탁드릴게요. 혹시 저 숫자를 입력받아서 그 숫자만큼 led를 깜빡거리게 하려면 fs.write에 flag?1:0 부분에 어떤식으로 넣어야될까요?
router.get('/:switch', function(req, res, next) {
var onoff = req.params.switch;
if (onoff == 'on') setLED(1);
if (onoff == 'off') setLED(0);
res.render('led', { title: 'LED Control : ' + req.params.switch });
});
on/off가 아닌 숫자를 받으시고 해당 숫자를 아두이노로 전송해 주시면 됩니다.
router.get('/:switch', function(req, res, next) {
var count = req.params.switch;
setLED(count);
res.render('led', { title: 'LED Control : ' + req.params.switch });
});
function setLED(count) {
var fs = require('fs');
fs.open('/dev/ttyUSB0','a', 666, function(e, fd) {
fs.write(fd, count, null, null, null, function() {
fs.close(fd, function() { });
});
});
}
이렇게 바꾸면 될것 같은데요
이 포스트 덕분에 라즈베리파이에 다수의 아두이노를 연결하여 웹에 게시 및 컨트롤 하는 방법이 대부분 해결되었네요... 감사합니다.
여기서 더 나아가 LED를 여러개 제어하고 싶으면 무엇을 더 추가해야하나여??