2016년 2월 25일 목요일

웹 브라우저에서 Raspberry Pi 조작하기

스터디의 이번 과제는, 웹브라우저에서 Raspberry Pi의 주소로 접근하여 텍스트필드에 문자열을 입력하면, 그 문자열을 PiFaceCAD에 출력하는 것이었다. 서버는 node.js를 사용하여 구현하기로 했다.

node.js의 웹프레임워크인 express 모듈을 사용했고, PiFaceCAD를 다루는 Python 코드를 호출하기 위해 child_process 모듈을 사용했다.

server.js

var express = require('express');
var app = express();

app.use(express.static('public'));

app.get('/', function (req, res) {
   res.sendFile( __dirname + "/" + "form.html" );
})

app.get('/lcd_write', function (req, res) {
   text = "\"" + req.query.text + "\""
   var exec = require('child_process').exec;
   function puts(error, stdout, stderr) { console.log(stdout) }
   cmd = "python3 -c \"import sys, pifacecad; pifacecad.PiFaceCAD().lcd.write(sys.argv[1])\""
   exec(cmd + " " + text, puts);
   console.log(text);
   res.end(text);
})

var server = app.listen(8000, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("Example app listening at http://%s:%s", host, port)

})

form.html

<html>
<body>
<form action="/lcd_write" method="GET">
Input text here: <input type="text" name="text">  <br>
<input type="submit" value="Submit">
</form>
</body>
</html>


2016년 2월 22일 월요일

Raspberry Pi에 node.js 설치

Raspberry Pi(Raspbian Wheezy)에 node.js를 설치해서 테스트해보았다. 강좌(http://pyrasis.com/nodejs/nodejs-HOWTO)를 따라서 콘솔 메시지로 Hello World까지는 출력을 했지만, express 모듈을 설치하고 사용하는 것이 그리 순탄치 않다.

npm ERR! Error: failed to fetch from registry: express

위와 같은 메시지가 나타나며 패키지 설치에 실패한다든지, 세그멘테이션 오류가 발생한다든지 하는 문제가 생긴다.

다음의 명령으로 패키지를 제거한 뒤에,

sudo apt-get purge nodejs npm

다음의 명령으로 4.2.4 버전을 다시 설치했다.

wget http://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-armv6l.tar.gz
cd /usr/local
sudo tar xzvf ~/node-v4.2.4-linux-armv6l.tar.gz --strip=1


참고

2016년 2월 20일 토요일

Edx.org 수강을 위한 최소 사양

Edx.org의 Body101x 강좌를 듣기 시작했다.

지난 번에 수리를 했던, 사무실에 둔 IBM A22e 2655 노트북은 펜티엄3 CPU에 현재 256MB 램을 갖고 있는데, 동영상 스트리밍으로 강좌를 보기에는 어려움이 있었다.

Lubuntu 14.04를 설치한 HP tx1000을 집에서 시험해보니, 파이어폭스 브라우저에서 동영상 강좌를 보고 문제를 푸는 데에 지장이 없다. AMD (Turion 64 X2 Mobile Technology) CPU에 RAM은 512MB만 남겨둔 상태. 다음에 사무실에 들릴 때 이걸로 바꿔야겠다.


tx1000