ofxNetworkを使ってiPhoneとMacでTCP通信

ofxNetworkを使って、iPhoneとMacを連動させてみました。

Mac版v0061のaddonsExampleには、networkTcpClientExample、networkTcpServerExampleというサンプルがあり、Mac同士で通信を行っています。同様に、Macをサーバー、iPhoneをクライアントとすれば、データのやり取りがでるはず。

2つのプロジェクトを開いてみると、ofxNetworkとofxThreadというアドオンを利用しています。しかし、iPhone版のv0061には、これらのアドオンは含まれていないので、Mac版からコピーしてみます。

networkTcpClientExampleのコードを見てみます。

testApp.h
ofxTCPClient tcpClient; // これが通信とかいろいろやってくれるみたいです。便利君。
string msgTx, msgRx; // サーバーとやり取りする文字列

float counter; //再接続までのカウンターなどなど
int connectTime;
int deltaTime;

bool weConnected; //接続フラグ


testApp.cpp

■setup
クライアントのアドレスとポートを指定してセットアップします。アドレスは環境に応じて適宜。
weConnected = tcpClient.setup("127.0.0.1", 11999);


■update
接続されていればmsgTxをサーバーに投げ、そうでなければ一定時間後に接続しようとします。
渡したい値を文字列にし、msgTxに入れてあげれば良いようです。

if(weConnected){
tcpClient.send(msgTx);
//if data has been sent lets update our text
string str = tcpClient.receive();
if( str.length() > 0 ){
msgRx = str;
}
}else{
//if we are not connected lets try and reconnect every 5 seconds
deltaTime = ofGetElapsedTimeMillis() - connectTime;

if( deltaTime > 5000 ){
weConnected = tcpClient.setup("127.0.0.1", 11999);
connectTime = ofGetElapsedTimeMillis();
}
}




せっかくiPhoneを使うので、加速度センサーを使ってみます。加速度は以下のように取得できます。
ofxAccelerometer.getForce().x、ofxAccelerometer.getForce().y、ofxAccelerometer.getForce().z

これを文字列に変換し、カンマで区切ってmsgTxに入れます。

if(weConnected){
msgTx =
ofToString( ofxAccelerometer.getForce().x, 2) + "," +
ofToString( ofxAccelerometer.getForce().y, 2) + "," +
ofToString( ofxAccelerometer.getForce().z, 2);
}




それでは、新規のiPhoneプロジェクトを作り上記を加えます。
testApp.h

#ifndef _TEST_APP
#define _TEST_APP
#pragma once
#include "ofMain.h"
#include "ofxiPhone.h"
#include "ofxNetwork.h"

class testApp : public ofxiPhoneApp {

public:

〜中略〜

ofxTCPClient tcpClient;
string msgTx, msgRx;

ofTrueTypeFont mono;
ofTrueTypeFont monosm;

float counter;
int connectTime;
int deltaTime;

bool weConnected;

int size;
int pos;
bool typed;
};

#endif


testApp.cpp


#include "testApp.h"

#define RECONNECT_TIME 400

//--------------------------------------------------------------
void testApp::setup(){
// register touch events
ofRegisterTouchEvents(this);

// initialize the accelerometer
ofxAccelerometer.setup();

//iPhoneAlerts will be sent to this.
ofxiPhoneAlerts.addListener(this);

// we don't want to be running to fast
ofSetVerticalSync(true);

//load our type
mono.loadFont("type/mono.ttf",9);
monosm.loadFont("type/mono.ttf",8);

//some variables

//have we typed
typed = false;

//our typing position
pos = 0;

//our send and recieve strings
msgTx = "";
msgRx = "";

//are we connected to the server - if this fails we
//will check every few seconds to see if the server exists
weConnected = tcpClient.setup("192.168.1.27", 11999);

connectTime = 0;
deltaTime = 0;

tcpClient.setVerbose(true);
}

//--------------------------------------------------------------
void testApp::update() {
ofBackground(230, 230, 230);

//we are connected - lets send our text and check what we get back
if(weConnected){
tcpClient.send(msgTx);

//if data has been sent lets update our text
string str = tcpClient.receive();
if( str.length() > 0 ){
msgRx = str;
}
}else{
//if we are not connected lets try and reconnect every 5 seconds
deltaTime = ofGetElapsedTimeMillis() - connectTime;

if( deltaTime > 5000 ){
weConnected = tcpClient.setup("192.168.1.27", 11999);
connectTime = ofGetElapsedTimeMillis();
}
}

if(weConnected){
msgTx =
ofToString( ofxAccelerometer.getForce().x, 2) + "," +
ofToString( ofxAccelerometer.getForce().y, 2) + "," +
ofToString( ofxAccelerometer.getForce().z, 2);

typed = true;
}
}

//--------------------------------------------------------------
void testApp::draw() {
ofSetColor(20, 20, 20);
mono.drawString("openFrameworks TCP Send Example", 15, 30);

if(typed){
monosm.drawString("sending:", 15, 55);
monosm.drawString(msgTx, 85, 55);
}
else{
//if(weConnected)monosm.drawString("status: type something to send data to port 11999", 15, 55);
//else monosm.drawString("status: server not found. launch server app and check ports!\n\nreconnecting in "+ofToString( (5000 - deltaTime) / 1000 )+" seconds", 15, 55);
}
monosm.drawString("from server: \n"+msgRx, 15, 270);
}
〜後略〜




Mac側でnetworkTcpServerExampleを起動しておき、上記のアプリをiPhoneで実行すると、接続がうまくいけば加速度センサーの値が表示されます。今は垂直に立てている状態です。


さて次回は、受け取った値を使って、3Dオブジェクトを動かしてみます。

Comments