Node.jsでIchigoJamを制御する方法を説明します。
Node.jsを使うことで、PCとIchigojamを連携させて、さまざまなWebサービス(IoTプラットフォーム、メール、クラウドストレージなど)を利用したアプリケーションを作成できます。
※操作例は、Windows PCを基本とします。
※PCとIchigoJamの接続は、「Node-REDでIchigoJamを制御する」を参照してください。
○Node.jsのインストール
Node.js V4.4.6(2016年6月現在)をダウンロードして、インストールします。
○serialportのインストール
次のように操作して、serialportモジュールをインストールします。
①作業のフォルダ(例「JS」)を作成します。
②コマンドプロンプトを起動し、作業用のフォルダに移動します。
③「npm install serialport」と入力します。
serialportの詳細は、以下のページを参照してください。
○基本プログラム
COMポートの指定は、ご自身の環境に合わせて変更してください。
・コマンド送信
PCからIchigoJamにコマンドを送信するプログラムは、次のように書きます。
This file contains hidden or 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 serialport = require('serialport'); | |
// シリアルポートの設定 | |
var port = new serialport.SerialPort( | |
'COM31', { // COMポート | |
baudrate: 115200, // ボーレート | |
} | |
); | |
port.on('open', function () { | |
// コマンド送信(改行コード「\r\n」を付加) | |
port.write("LED1\r\n"); | |
}); |
・データ受信
Ichigojamの出力データを受信するプログラムは、次のように書きます。
This file contains hidden or 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 serialport = require('serialport'); | |
// シリアルポートの設定 | |
var port = new serialport.SerialPort( | |
'COM31', { // COMポート | |
baudrate: 115200, // ボーレート | |
parser: serialport.parsers.readline('\n') // 区切り文字 | |
} | |
); | |
port.on('data', function (data) { | |
// 取得データはbufferオブジェクトなので「toString」で文字列に変換 | |
console.log(data.toString()); | |
}) |
○応用例
socket.ioを使って、ローカルのWebサーバーから、IchigojamのLEDを制御する例を紹介します。
・socket.ioインストール
①コマンドプロンプトを起動し、作業用のフォルダに移動します。
②「npm install socket.io」と入力します。
・プログラム
次のJavaScript「ichigo_led.js」とHTMLコード「index.html」を入力し、作業用のフォルダに保存します。
This file contains hidden or 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 serialport = require('serialport'); | |
// シリアルポートの設定 | |
var port = new serialport.SerialPort( | |
'COM31', { // COMポート | |
baudrate: 115200, // ボーレート | |
} | |
); | |
// HTMLファイル(ホームページ)の設定 | |
var html = require('fs').readFileSync('index.html'); | |
// httpサーバーの作成 | |
var http = require('http').createServer(function(req, res) { | |
res.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
res.end(html); | |
}); | |
var io = require('socket.io')(http); | |
http.listen(3000); // httpポート | |
port.on('open', function () { | |
io.on('connection', function(socket) { | |
socket.on('msg', function(data) { | |
console.log(data); | |
// 空白の除去と改行コードの付加 | |
data = data.replace(/^[\s ]+|[\s ]+$/g, "")+"\r\n"; | |
// コマンド送信 | |
port.write(data); | |
}); | |
}); | |
}); |
This file contains hidden or 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"> | |
<title>LED制御</title> | |
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> | |
</head> | |
<body> | |
<script> | |
var socket = io(); | |
function on() { | |
socket.emit('msg', 'LED1'); | |
} | |
function off() { | |
socket.emit('msg', 'LED0'); | |
} | |
</script> | |
<form> | |
<input type="button" value="ON" onclick="on()"/> | |
<input type="button" value="OFF" onclick="off()"/> | |
</form> | |
</body> | |
</html |
・動作の確認
①コマンドプロンプトを起動し、作業用のフォルダに移動します。
②「node ichigo_led.js」と入力します。
③ブラウザを起動し、「http://localhost:3000」にアクセスします。
④[ON]ボタンのクリックでLEDが点灯し、[OFF]ボタンのクリックでLEDが消灯することを確認します。
0 件のコメント:
コメントを投稿