Notice
Recent Posts
Recent Comments
Today
Total
04-29 20:19
Archives
관리 메뉴

Jeongchul Kim

Intel Edison board Eclipse NodeJS Express Control LED 본문

사물인터넷

Intel Edison board Eclipse NodeJS Express Control LED

김 정출 2016. 3. 9. 11:41


Intel Edison board Eclipse NodeJS Express Control LED


express.js 소스코드 수정

var http = require("http");

var express = require("express");

var socketio = require("socket.io");

var mraa = require('mraa'); //require mraa

var led = new mraa.Gpio(11);

var btn = new mraa.Gpio(8);

var client = null;

var app = express();

app.use( express.static(__dirname+'/public'));

var server = http.createServer(app);

var io = socketio(server);

led.dir(mraa.DIR_OUT);

btn.dir(mraa.DIR_IN);

app.get('/cgi/cmd.do', function(req,res) {

console.log("cmd!!!");

res.send("<h1>CMD</h1>");

});

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

client = socket;

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

console.log(data);

if(data.value === "on"){

led.write(1);

}else if(data.value === "off") {

led.write(0);

}

});

});

periodicActivity();

var last;

function periodicActivity() {

var val =  btn.read(); //read the digital value of the pin

if(last != val) {

 console.log('btn: ' + val); //write the read value out to the console

 if(val === 1) {

 if(client != null) client.emit('up',{});

 }else {

 if(client != null) client.emit('down',{});

 }

 last = val;

}

setTimeout(periodicActivity,300); //call the indicated function after 1 second (1000 milliseconds)

}

server.listen(9090,function() {

console.log("server running on 9090 ...");

console.log("with Express");

}); //port:9090 !

periodicActivity(); //call the periodicActivity function



index.html 소스코드 수정


<!Doctype html>

<html>

<head>

<style rel="stylesheet" type="text/css">

.btn{

background-image : url('button.jpg');

width:160px; height:157px;

}

.btn-unpressed{

background-image : url('button.jpg');

background-position : 0 0;

width:160px; height:157px;

}

.btn-pressed {

background-image : url('button.jpg');

background-position : 160px 0;

width:160px; height:157px;

}

</style>

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

<script type= "text/javascript">

window.onload = function() {

var socket = io.connect();

var btn = document.querySelector('#btn');

var div = document.querySelector('#div');

var led_on = document.querySelector('#led_on');

var led_off = document.querySelector('#led_off');

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

   console.log(data);

   div.className = "btn btn-pressed";

   socket.emit('led',{value:'on'}); <!-- emit 서버로 전송 -->

});

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

   console.log(data);

   div.className = "btn btn-unpressed";

   socket.emit('led',{value:'off'});

});

led_on.onclick = function() {

socket.emit('led',{value:'on'}); <!-- emit 서버로 전송 -->

}

led_off.onclick = function() {

socket.emit('led',{value:'off'});

}

btn.onmousedown = function() {

console.log("down");

div.className = "btn btn-pressed";

}

btn.onmouseup = function() {

console.log("up");

div.className = "btn btn-unpressed";

}

}

</script>

</head>

<body>

<h1>Welcome Express Server </h1>

<p> THis is index page </p>

<button id="btn">btn</button>

<input type="radio" name="led" id="led_on"/><label for="led_on">On</label>

<input type="radio" name="led" id="led_off"/><label for="led_off">Off</label>

<div id="div" class="btn btn-unpressed"></div>

</body>

</html>


라디오 버튼이 On으로 선택되면 LED가 켜지고, off로 선택되면 LED가 꺼진다.

버튼을 누르면 이미지 버튼이 눌렸다 떨어졌다 하게 되며,

보드에서 스위치를 누르면 웹 상의 이미지 또한 눌렸다 변경된다.








Comments