기존에 아듀이노나 하드웨어로 장치를 제어하기 위해서는 포텐셔미터 혹은 토글 스위치 같은 하드웨어를 장착하거나
원격 통신장치를 사용해서 통신 프로토콜을 맞추는 작업을 해야했다면, 웹 브라우저는 PC혹은 스마트폰 등 어떠한 플랫폼에서든 접근 가능하고 어디에서나 접속되므로 Embedded장치 제어의 한계를 크게 넓혀주게 된다.
인텔 에디슨은 내장된 WIFI 모듈을 사용하여 원격으로 접속이 가능하다.
그럼 Wiif를 통해서 어떻게 프로그램이 상호 인터페이스 할 수 있는가?
임베디드 세계에서 장치는 장치고 사용자는 유저이다. 하지만 웹의 영역에서 장치는 서비스를 제공하는 서버이고 사용자는 서버에 접속하는 고객 혹은 클라이언트다.
클라이언트는 웹 브라우저라는 HTML스크립트로 동작하는 웹페이지를 통해 접속하고 명령을 내린다.
서버는 웹페이지에서 발생한 이벤트를 통해 입력을 받아서 프로그램 명령을 수행한다.
그리고 다시 이벤트를 발생시켜 웹페이지에 정보를 업데이트한다.
그래서 IOT 프로그램을 하려면 장치의 프로그램 뿐 아니라 HTML, 자바 스크립트를 같이 작업해주어야 한다.
그리고 이 장치 프로그래밍과 스크립트 사이를 연결해주는 통신방식이 바로 Socket 이다.
소켓은 말 그대로 접속단자를 의미한다.
장치 프로그램과 웹 프로그램이 서로 소켓을 열면 통신이 되는 것이다.
그럼 어떻게 구현하는지 알아보자.
빈 프로젝트를 하나 생성하여 main.js를 만든다.
main.js
main.js에서는 아래와 같이 간단히 소켓을 초기화 하고 http서버를 생성한다.
var mraa = require('mraa'); //require mraa
var app = require('express')(); //Express Library
var server = require('http').Server(app); //Create HTTP instance
var io = require('socket.io')(server); //Socket.IO Library
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console
클라이언트가 접속했을 때 index.html 파일을 전송할 수 있도록 다음을 추가한다.
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html'); //serve the static html file
});
이제 IO포트를 열어서 LED를 켜고 끌 수 있도록 한다. 여기서는 D12 핀에 LED가 연결된 것으로 가정한다. 변수 초기화도 여기서 시켜준다.
var LED2 = new mraa.Gpio(12);
LED2.dir(mraa.DIR_OUT);
var LED2State = 1;
다음은 소켓에서 특정 이벤트가 발생했을 때 호출될 함수를 선언한다. 여기서는 LED2ButtonClick 이라고 정했다. 그럼 HTML에서 LED2ButtonClick 이벤트를 호출하면 이 함수가 실행되는 것이다.
src/gz all http://repo.opkg.net/edison/repo/all
src/gz edison http://repo.opkg.net/edison/repo/edison
src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32
Update opkg:
opkg update
If the update is successful, the output should look like this:
Downloading http://repo.opkg.net/edison/repo/all/Packages.gz.
Inflating http://repo.opkg.net/edison/repo/all/Packages.gz.
Updated list of available packages in /var/lib/opkg/all.
Downloading http://repo.opkg.net/edison/repo/edison/Packages.gz.
Inflating http://repo.opkg.net/edison/repo/edison/Packages.gz.
Updated list of available packages in /var/lib/opkg/edison.
Downloading http://repo.opkg.net/edison/repo/core2-32/Packages.gz.
Inflating http://repo.opkg.net/edison/repo/core2-32/Packages.gz.
Updated list of available packages in /var/lib/opkg/core2-32.
Downloading http://iotdk.intel.com/repos/2.0/intelgalactic/Packages.
Updated list of available packages in /var/lib/opkg/iotkit.
Cloning this repository onto Edison
To install git:
opkg install git
Installing git (2.0.1-r0) on root.
Downloading http://repo.opkg.net/edison/repo/core2-32/git_2.0.1-r0_core2-32.ipk.
Configuring git.
다음을 실행하여 Repository를 클로닝(복제) 한다. git clone https://github.com/drejkim/edi-cam.
Also, verify that the video device node has been created by typing ls -l /dev/video0:
root@myedison:~# ls -l /dev/video0
crw-rw---- 1 root video 81, 0 Nov 10 15:57 /dev/video0
I
Installing ffmpeg
To install ffmpeg:
edi-cam 으로 이동
cd /edi-cam
bin. 폴더로 이동한다.
Type ./install_ffmpeg.sh to run the shell script.
Creating ~/bin directory if it doesn't exist...
Removing old versions of ffmpeg...
Downloading ffmpeg...
--2016-02-11 08:12:53-- http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-32bit-static.tar.xz
Resolving johnvansickle.com... 199.79.62.21
Connecting to johnvansickle.com|199.79.62.21|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14387580 (14M) [application/x-tar]
Saving to: '/home/root/bin/ffmpeg-release-32bit-static.tar.xz'
100%[======================================>] 14,387,580 922KB/s in 30s
2016-02-11 08:13:27 (472 KB/s) - '/home/root/bin/ffmpeg-release-32bit-static.tar.xz' saved [14387580/14387580]
Unpacking...
Cleaning up...
If the download doesn't work, the release link may have changed. Check here, copy the address of the latest release, and replace in the shell script.
Installing Node.js packages
Navigate to web/server.
Install the Node.js packages by typing npm install.
Running the demo
Updating the WebSocket address
Modify wsUrl in web/client/index.html. The section of the code looks like this:
// CHANGE THIS TO THE APPROPRIATE WS ADDRESS
var wsUrl = 'ws://myedison.local:8084/';
Replace myedison with the name of your Edison.
Running the Node.js server
Navigate to web/server.
Run the server by typing node server.js.
The Node.js server should now be running. The console will look something like this:
WebSocket server listening on port 8084
HTTP server listening on port 8080
Listening for video stream on port 8082
Stream Connected: 127.0.0.1:52995 size: 320x240
Viewing the video stream
Open a browser window and navigate to http://myedison.local:8080, where myedison is the name of your Edison. You should now see the video stream from your webcam!