まずは、Raspberry Piのカメラ モジュール
①「date-utils」モジュールのインストール
ファイル名を日付から生成するため、まずは日付処理を単純にするための「date-utils」モジュールをインストールします。
1.ターミナルを起動し、作業用フォルダに移動します。
2.「npm install date-utils」と入力します。
②コードの入力
次のJavaScriptコードを入力し、「camae.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
require('date-utils'); | |
var dt = new Date(); | |
var filename = dt.toFormat("YYYYMMDDHH24MISS") + ".jpg"; | |
var option = [ '-o' , filename]; | |
var spawn = require('child_process').spawn; | |
var raspistill = spawn('raspistill', option); | |
raspistill.on('close', function(code, signal){ | |
console.log('chile process terminated'); | |
}); |
処理内容は、「child_process.spawn」で、「raspistill」コマンドを実行しているだけです。
③動作の確認
1.ターミナルを起動し、作業用フォルダに移動します。
2.「sudo node camae.js」と入力します。
■カメラをリモート制御する
応用的な事例として、PubNubを使ってRaspberry Piのカメラモジュールをリモート制御してみます。
①コードの入力
1.次のJavaScriptコードを入力し、「pi-camae.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
require('date-utils'); | |
var publishKey = '<your Publish Key>'; | |
var subscribeKey = '<your Subscribe Key>'; | |
var channel = "shutter"; | |
var pubnub = require("pubnub")({ | |
ssl : true, | |
publish_key : publishKey, | |
subscribe_key : subscribeKey | |
}); | |
pubnub.subscribe({ | |
channel : channel, | |
callback : function(message){ | |
if(message.action === 'on'){ | |
var dt = new Date(); | |
var filename = dt.toFormat("YYYYMMDDHH24MISS") + ".jpg"; | |
var option = [ '-o' , filename]; | |
var spawn = require('child_process').spawn; | |
var raspistill = spawn('raspistill', option); | |
raspistill.on('close', function(code, signal){ | |
console.log('chile process terminated'); | |
}); | |
} | |
} | |
}); |
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 channel = "shutter" // channel名の設定 | |
// PubNub初期化 | |
var pubnub = PUBNUB.init({ | |
publish_key : publishKey, | |
subscribe_key : subscribeKey | |
}); | |
// データの送信 | |
function publish(action) { | |
var value = {'action': action}; | |
pubnub.publish({ | |
channel : channel, | |
message : value, | |
callback: function(message){ | |
console.log(message); | |
} | |
}); | |
} | |
// [Shutter]ボタンのクリックで文字列「on」を送信 | |
$('#on-button').click(function(){ | |
publish('on'); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<button id="on-button">Shutter</button> | |
</body> | |
</html> |
<your Publish key>にはPublish Keyを、<your Pubscribe key>にはSubscribe Keyを入力します。

⑤動作の確認
1.ターミナルを起動し、作業用フォルダに移動します。
2.「sudo node pi-camera.js」と入力します。
3.ブラウザで「camera.html」を開き、[Shutter]ボタンのクリックで、撮影されることを確認します。