目的
TesselでWebSocketをしゃべってみます。Socket.ioを使いたかったんですが、まだ動かないみたいで、別のモジュールを使うみたいなんですが十分動きます。
https://forums.tessel.io/t/attempting-to-use-socket-io-client/40
前提
- Tesselを持っている事
- Macを利用している事
手順
動作環境を準備をする
サンプルソースを準備します。
$ git clone https://github.com/Frijol/tessel-websocket
$ cd tessel-websocket
nodejs-websocketをインストールしておきます。
$ npm install nodejs-websocket -s
Wifi繋がってますか?
$ tessel wifi -l
繋がってなければ、つなげます。
$ tessel wifi -n <networkname> -p <password>
取得したIPはtessel wifi -lで分かります。
動作させる
Tesselがサーバーです。
$ tessel run server.js
client.jsのほうのipAddressをTesselのIPアドレスに書き換えます。
// The IP address of your Tessel. Find it with `tessel wifi -l` and set it here
var ipAddress = '192.168.210.74'
var ws = require('nodejs-websocket');
var port = 8000;
そしてコンソールから接続してみます。メッセージをタイプするとサーバー側でWebSocketでメッセージを受取り表示します。
$ node client.js
Connected to server! You may start typing into the console.
hello
hey
サーバー側の表示はこんな感じです。
$ tessel run server.js
TESSEL! Connected to TM-00-04-f000da30-00544f4a-62782586.
INFO Bundling directory /Users/takayukii/GoogleDrive/workspaces/tessel-websocket
INFO Deploying bundle (85.50 KB)...
INFO Running script...
Listening on port 8000
Accepted new connection...
Got data: hello
Got data: hey
Tesselをクライアントにする
個人的な利用用途を考えるとTesselはサーバーではなくクライアントだと思うので、Tesselをクライアントにしてみます。ore-client.jsを作成します。1秒間隔でhelloとつぶやくだけのクライアントです。ipAdressにはMacなど自分のIPアドレスを指定します。
// The IP address of your Tessel. Find it with `tessel wifi -l` and set it here
var ipAddress = '192.168.210.45'
var ws = require('nodejs-websocket');
var port = 8000;
// Set the binary fragmentation to 1 byte so it instantly sends anything we write to it
ws.setBinaryFragmentation(1);
// When we get a connection
var connection = ws.connect('ws://' + ipAddress + ':' + port, function() {
setInterval(function (){
var now = new Date();
var str = 'hello at ' + now;
connection.sendBinary(new Buffer(str));
}, 1000);
});
さきほどTesselで使ったserver.jsを今度はMacで起動します。全く同じソースが使えると感動します。
$ node server.js
先ほど作ったore-client.jsをTesselで動かします。
$ tessel run ore-client.js
サーバー側の表示はこんな感じになります。
$ node server.js
Listening on port 8000
Accepted new connection...
Got data: hello at Wed Nov 26 2014 21:30:47 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:48 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:49 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:50 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:51 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:52 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:53 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:54 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:55 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:56 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:57 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:58 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:30:59 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:31:00 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:31:01 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:31:02 GMT+0000 (GMT)
Got data: hello at Wed Nov 26 2014 21:31:03 GMT+0000 (GMT)
次にやってみたい事
温度・湿度センサーとBLEモジュールを買う事にしました。