
①Raspberry Pi 開発環境の準備
「Raspberry Pi カメラのリモート制御&画像転送-PubNub編」の「Raspberry Pi 開発環境の準備」の手順で、 開発環境を準備します。
②プログラムの作成
1.次のJavaScriptコードを入力し、「live.js」と名前を付けて、作業用フォルダに保存します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var publishKey = '<your Publish Key >'; | |
var subscribeKey = '<your Subscribe Key>'; | |
var channel1 = "shutter"; | |
var channel2 = "pic"; | |
var base64Img = require('base64-img'); | |
var filename = "picam.jpg"; | |
var raspistillOption = [ "-w", "320", "-h", "240", "-o", filename, "-t", "999999999", "-tl", "1000"]; | |
var jpegoptimOption = ["--strip-all", "--max=30", filename]; | |
var exec = require('child_process').exec; | |
var spawn = require('child_process').spawn; | |
var fs = require('fs'); | |
var pid; | |
var pubnub = require("pubnub")({ | |
ssl : true, | |
publish_key : publishKey, | |
subscribe_key : subscribeKey | |
}); | |
pubnub.subscribe({ | |
channel : channel1, | |
callback : function(message){ | |
console.log('>', message); | |
var raspistill = spawn('raspistill', raspistillOption); | |
getpid(); | |
if(message.action === 'start'){ | |
fs.watchFile(filename, function(current, previous) { | |
var jpegoptim = spawn('jpegoptim', jpegoptimOption); | |
jpegoptim.on('close', function(code, signal){ | |
base64Img.base64(filename,function(err, data){ | |
if (!err){ | |
publish(data); | |
} | |
}); | |
}); | |
}); | |
} else { | |
exec('kill -9 '+ pid); | |
} | |
} | |
}); | |
function publish(data){ | |
pubnub.publish({ | |
channel : channel2, | |
message : data, | |
callback: function(m){ | |
console.log(m); | |
}, | |
error: function(m){ | |
console.log(m); | |
} | |
}); | |
} | |
function getpid(){ | |
exec('ps aux | grep [r]aspistill', function(err, stdout, stderr) { | |
if (stdout) { | |
try { | |
pid = stdout.match(/\s+(\d{4,6})+\s+/gi)[0].trim(); | |
if (pid) { | |
console.log('Raspistill running. PID:' + pid); | |
} | |
} catch (e) { | |
console.log('Raspistill PID not found'); | |
} | |
} else { | |
console.log('Raspistill PID not found'); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<script src="https://cdn.pubnub.com/pubnub.min.js"></script> | |
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> | |
<script> | |
$(function(){ | |
var publishKey = '<your Publish Key>'; | |
var subscribeKey = 'your Subscribe Key'; | |
var channel1 = "shutter" // channel名の設定 | |
var channel2 = "pic" | |
// PubNub初期化 | |
var pubnub = PUBNUB.init({ | |
publish_key : publishKey, | |
subscribe_key : subscribeKey | |
}); | |
pubnub.subscribe({ | |
channel : channel2, | |
callback : function(data) { | |
console.log(data); | |
var img = new Image(); | |
img.src = data; | |
img.onload = function(){ | |
$('#main').html(img); | |
} | |
} | |
}); | |
// データの送信 | |
function publish(action) { | |
var value = {'action': action}; | |
pubnub.publish({ | |
channel : channel1, | |
message : value, | |
callback: function(message){console.log(message);} | |
}); | |
} | |
// [START]ボタンのクリックで文字列「start」を送信 | |
$('#start-button').click(function(){ | |
publish('start'); | |
}); | |
// [STOP]ボタンのクリックで文字列「stop」を送信 | |
$('#stop-button').click(function(){ | |
publish('stop'); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<button id="start-button">START</button> | |
<button id="stop-button">STOP</button> | |
<div id="main"></div> | |
</body> | |
</html> |
③プログラムの実行と動作確認
1.ターミナルを起動し、作業用フォルダに移動します。
2.「sudo node live.js」と入力します。
3.ブラウザで「live.html」を開いて、[START]ボタンをクリックします。
4.撮影が実行(カメラモジュールのLEDが点灯)され、写真がブラウザ上にライブで表示されることを確認します。
5.[STOP]ボタンのクリックで、ライブ表示が停止することを確認します。
0 件のコメント:
コメントを投稿