Take’s diary

Macとマイコンに関すること--ワクワクの製作日記

ESP-WROOM-02 開発ボード (ESP8266) と iPhone が OSCで双方向通信できた!!

この前までは、

 ESP-WROOM-02(以下ESP)からiPhoneへの1方向だけの通信だけでした。今回は双方向通信を実現してみます。

          f:id:TAKEsan:20160224220921j:plain:w200:left

           
 今回の動画です。テザリングiPhoneのスイッチを押すとESPのLEDとLEPTONのシャッターが動く。

 これを使えば、大概のことが実現可能です。iPhoneからESPを自由に操作できるし、ESPから受け取ったデータをOpenframeworksの豊富な映像処理で高速に加工してiPhoneに表示できます。コツはiPhone側のWifiをONにして、インターネット共有もONにしておくことです。これは、EdisonでのiPhoneテザリング実験をしていなかったら思いつきませんでした。
 OSC通信ってのはそのソースコードを見る限り双方向通信可能なのはわかりますけど、大概のExampleは一方通行の通信例だけです。素人では送受信ソースの合体なんてめんどくさいし、後回しにしようって考えが先にたってしまいます。
 今回は「なんかめんどくさい」ことをすることにしました。理由はOSCのソースは簡潔で私にも容易に理解できたから。
結果、予想通り実現できました。 
 f:id:TAKEsan:20160224214654p:plainf:id:TAKEsan:20160224214655p:plain

OSC通信の大雑把な説明

  1. OSCのアドレスと称するものは /から始まる英数字で、自由に自分でいくらでも設定できる。
  2. 送受信側でOSCアドレスが同一であること。/◯◯/◯◯等入れ子にできるようですが、単純なデータ送受信は/◯◯だけでいい。そのあとに続くのが肝心のデータで、int,string,float他のデータを複数送信できる。これに関してはライブラリによって関数の記述方法が違うので、ライブラリの説明を確認すること。
  3. 受信側はシリアル通信のように面倒な認識手段を考える必要なし。
  4. 送信に関し受信側のipと送信用のポート番号が必要。受信側は受信用のポート番号を設定すれば送信側のipは必要なし。注意点は、送受信別々のポート番号が必要なことです。つまりiPhone側の送信ポートとESP側の受診ポート。iPhone側の受信ポートとESP側の送信ポートが同一名称であること。
  5. ポート番号はほとんど自由に自分で設定できる(規則に従うこと)
  6. ESPからiPhoneの送信に関し、複数のESPから1台のiPhoneに各々別のデータを送ることが可能です。もうお分かりですね。OSCアドレスを変えてやればいいんです。

 これらは、今回のソースを見て貰えば多少ソフトをいじったことがある方なら容易に判別できると思います。ライブラリの中身はどうあれソース作成はすごく簡単です。今回の応用で、iPhone複数のESPとの送受信、そして複雑になるとは思いますがESP通しの送受信も可能になると思います。

iPhonのインターネット通信をON=テザリングを有効にすると...。

 今回調べて新たに分かったことは、iPhoneではテザリング通信において、固定ipだということ。これはどのiPhoneも同じようなので公表できます。「172.20.10.1」です。ESP側は、iPhoneテザリングしている限り、このipに設定してしまえば、iPhonのipアドレスを意識しなくていいんです(ただしパスワードは個人設定!!)。これは大きな収穫で、EdisonのようにiPhone,iot機器で各々bonjourlを使わなくても良いのです。ESPからiPhoneの交信を確立して、自分のipアドレスをOSC通信で、iPhoneに送って知らせてやれば、iPhoneはESPにデータを送ることが可能になります。Edisonとのさらに大きな違いは、めんどくさいWifi選択などしなくても、プログラム中ですんなり繋がってしまうことでした。これはっ.....いい。ただしiPhoneの名称を英数字に修正してから。多分何も意識しないと日本語に設定されてしまうのでiPhoneの名称を英数字に直してから実行してください。
 これで、実行する環境は、家の中の無線ランルーターを意識しなくて済みますので、どこへ外出してもiPhoneとESPはお友達のままです。これも画期的です。
 心配なのはテザリング通信してパケットの使用量がどうかです。同一ラン内で外部に出ていないので多分大丈夫かとは思いましたが、一応昨日実行したLEPTONとの通信をAUで確認したら、問題ありませんでした。PhoneとESPはなんかOSCを実現するために生み出されてきたような感じです。

 さらに前回からの進歩は、SPIについて最後のDelayが必要なくなったことです。SSピンのON,OFF切り替えだけでいけるかもという直感だけでしたが、うまくいきました。これで若干(0.1秒)ですがスピードアップしました。
 てな訳で、今回はその全貌の公開をすることにしました。 ESP側はセンサーのデータを送るだけでいいし(少なくとも今回取り上げた4.8K程度のデータなら瞬時)iPhoneからはESPを思うように操作できます。今の所ESPから送れないのは大量の画像でしょうか?ただ、カメラの代わりに、OMRONの非接触赤外線センサーで人の移動(体温)に合わせてサーボを動かすなんて朝飯前にできます。そして同時にiPhone側にセンサーで捉えた温度分布も表示できます。でも、今回のセンサーの表示は、まさにカメラですよね。このくらいの動画なら、iPhone側のOpenframeworksでOpenCVが使えます。OpenCVが使えたら動体の認識はもちろん様々な画像の加工が簡単に実現可能。しかも暗闇でも画像が認識できるのですからワクワクです。
 
 今回のソースに関し、Xcodeの最初の設定は、初めての方には特にGUIに関して多少面倒くさいかもしれませんが、設定してしまえば後は少しの修正だけで、iPhoneからESPを考えがつく限り最大の表現力でしかもサクサクと実行できます(通信方式がUDPなので反応がとても早い)。すごい。Openframeworksのハードルが高いって?そんなことなし。最新のバージョン(9.2以降)では、今まで私がぶつかった問題点が、ほとんど解消されてます。メジャーなオープンソースってすごいです。資金源はどこなんだろうなんて下衆の勘繰りをしちゃいます。日本語の解説を見れば理解できますし、最小のCの知識で素晴らしい画像が作れます。なんといっても芸術家のための開発環境ですから難しいわけがない。Enjoy!!

5月12日追記

 今回作ったOpenframeworksのソースをフォルダごとダウンロードできます。(今まで「はてなブログ」でのリンク方法が分からなかった)
    WROOM02.zip 直
 これをダウンロードして解凍したら of〇〇ios_release/apps/myApps へコピーして、最初はXcodeiosシュミレーターからiPhone6を選択してコンパイルして見て下さい。今回の画面が表示されるはずです。うまく実行できたら、自分のiPhoneをつないで転送。たたき台にすればきっと面白いものができると思います。
f:id:TAKEsan:20160512124959p:plain
 ios版V0.9.3では、exampleをそのままコンパイルするとエラーになります。原因はbin/dataに本来なければならないpngファイルがなぜか全て消えているからです。exampleを実行したい場合は、一度projectGeneratorでダミーフォルダを作って、bin/data 以下のpngファイルを全てコピーする必要があります。

以下 ofApp.h

基本的なプログラムの作成方法は、前回の記事を参照
takesan.hatenablog.com

#pragma once

#include "ofxiOS.h"
#include "ofxOsc.h"

//#define HOST "172.20.10.7" 今回は設定の必要なし
#define PORT1 8091
#define PORT 8090

#define NUM_MSG_STRINGS 20

class ofApp : public ofxiOSApp {

	public:
		void setup();
		void update();
		void draw();
		void exit();
		
		void touchDown(ofTouchEventArgs & touch);
		void touchMoved(ofTouchEventArgs & touch);
		void touchUp(ofTouchEventArgs & touch);
		void touchDoubleTap(ofTouchEventArgs & touch);
		void touchCancelled(ofTouchEventArgs & touch);

		void lostFocus();
		void gotFocus();
		void gotMemoryWarning();
		void deviceOrientationChanged(int newOrientation);

		ofxOscReceiver receiver;
		ofxOscSender sender;
    
		int current_msg_string;
		string msg_strings[NUM_MSG_STRINGS];
		float timers[NUM_MSG_STRINGS];

         //OSC Data
        int EPS_swA,EPS_swB,EPS_button;
        float EPS_sli;
};

以下 main.mm

#include "ofApp.h"

int main() {
    
    //  here are the most commonly used iOS window settings.
    //------------------------------------------------------
    ofiOSWindowSettings settings;
    settings.enableRetina = true; // レティーナ用にするにははここを true にする
    settings.enableDepth = false; // enables depth buffer for 3d drawing.
    settings.enableAntiAliasing = false; // enables anti-aliasing which smooths out graphics on the screen.
    settings.numOfAntiAliasingSamples = 0; // number of samples used for anti-aliasing.
    settings.enableHardwareOrientation = false; // enables native view orientation.
    settings.enableHardwareOrientationAnimation = false; // enables native orientation changes to be animated.
    settings.glesVersion = OFXIOS_RENDERER_ES1; // type of renderer to use, ES1, ES2, etc.
    
    ofCreateWindow(settings);
    
	return ofRunApp(new ofApp);
}

以下 ofApp.mm

#include "ofApp.h"
#include "EPSView.h"

const int colormap_rainbow[] = {1, 3, 74, 0, 3, 74, 0, 3, 75, 0, 3, 75, 0, 3, 76, 0, 3, 76, 0, 3, 77, 0, 3, 79, 0, 3, 82, 0, 5, 85, 0, 7, 88, 0, 10, 91, 0, 14, 94, 0, 19, 98, 0, 22, 100, 0, 25, 103, 0, 28, 106, 0, 32, 109, 0, 35, 112, 0, 38, 116, 0, 40, 119, 0, 42, 123, 0, 45, 128, 0, 49, 133, 0, 50, 134, 0, 51, 136, 0, 52, 137, 0, 53, 139, 0, 54, 142, 0, 55, 144, 0, 56, 145, 0, 58, 149, 0, 61, 154, 0, 63, 156, 0, 65, 159, 0, 66, 161, 0, 68, 164, 0, 69, 167, 0, 71, 170, 0, 73, 174, 0, 75, 179, 0, 76, 181, 0, 78, 184, 0, 79, 187, 0, 80, 188, 0, 81, 190, 0, 84, 194, 0, 87, 198, 0, 88, 200, 0, 90, 203, 0, 92, 205, 0, 94, 207, 0, 94, 208, 0, 95, 209, 0, 96, 210, 0, 97, 211, 0, 99, 214, 0, 102, 217, 0, 103, 218, 0, 104, 219, 0, 105, 220, 0, 107, 221, 0, 109, 223, 0, 111, 223, 0, 113, 223, 0, 115, 222, 0, 117, 221, 0, 118, 220, 1, 120, 219, 1, 122, 217, 2, 124, 216, 2, 126, 214, 3, 129, 212, 3, 131, 207, 4, 132, 205, 4, 133, 202, 4, 134, 197, 5, 136, 192, 6, 138, 185, 7, 141, 178, 8, 142, 172, 10, 144, 166, 10, 144, 162, 11, 145, 158, 12, 146, 153, 13, 147, 149, 15, 149, 140, 17, 151, 132, 22, 153, 120, 25, 154, 115, 28, 156, 109, 34, 158, 101, 40, 160, 94, 45, 162, 86, 51, 164, 79, 59, 167, 69, 67, 171, 60, 72, 173, 54, 78, 175, 48, 83, 177, 43, 89, 179, 39, 93, 181, 35, 98, 183, 31, 105, 185, 26, 109, 187, 23, 113, 188, 21, 118, 189, 19, 123, 191, 17, 128, 193, 14, 134, 195, 12, 138, 196, 10, 142, 197, 8, 146, 198, 6, 151, 200, 5, 155, 201, 4, 160, 203, 3, 164, 204, 2, 169, 205, 2, 173, 206, 1, 175, 207, 1, 178, 207, 1, 184, 208, 0, 190, 210, 0, 193, 211, 0, 196, 212, 0, 199, 212, 0, 202, 213, 1, 207, 214, 2, 212, 215, 3, 215, 214, 3, 218, 214, 3, 220, 213, 3, 222, 213, 4, 224, 212, 4, 225, 212, 5, 226, 212, 5, 229, 211, 5, 232, 211, 6, 232, 211, 6, 233, 211, 6, 234, 210, 6, 235, 210, 7, 236, 209, 7, 237, 208, 8, 239, 206, 8, 241, 204, 9, 242, 203, 9, 244, 202, 10, 244, 201, 10, 245, 200, 10, 245, 199, 11, 246, 198, 11, 247, 197, 12, 248, 194, 13, 249, 191, 14, 250, 189, 14, 251, 187, 15, 251, 185, 16, 252, 183, 17, 252, 178, 18, 253, 174, 19, 253, 171, 19, 254, 168, 20, 254, 165, 21, 254, 164, 21, 255, 163, 22, 255, 161, 22, 255, 159, 23, 255, 157, 23, 255, 155, 24, 255, 149, 25, 255, 143, 27, 255, 139, 28, 255, 135, 30, 255, 131, 31, 255, 127, 32, 255, 118, 34, 255, 110, 36, 255, 104, 37, 255, 101, 38, 255, 99, 39, 255, 93, 40, 255, 88, 42, 254, 82, 43, 254, 77, 45, 254, 69, 47, 254, 62, 49, 253, 57, 50, 253, 53, 52, 252, 49, 53, 252, 45, 55, 251, 39, 57, 251, 33, 59, 251, 32, 60, 251, 31, 60, 251, 30, 61, 251, 29, 61, 251, 28, 62, 250, 27, 63, 250, 27, 65, 249, 26, 66, 249, 26, 68, 248, 25, 70, 248, 24, 73, 247, 24, 75, 247, 25, 77, 247, 25, 79, 247, 26, 81, 247, 32, 83, 247, 35, 85, 247, 38, 86, 247, 42, 88, 247, 46, 90, 247, 50, 92, 248, 55, 94, 248, 59, 96, 248, 64, 98, 248, 72, 101, 249, 81, 104, 249, 87, 106, 250, 93, 108, 250, 95, 109, 250, 98, 110, 250, 100, 111, 251, 101, 112, 251, 102, 113, 251, 109, 117, 252, 116, 121, 252, 121, 123, 253, 126, 126, 253, 130, 128, 254, 135, 131, 254, 139, 133, 254, 144, 136, 254, 151, 140, 255, 158, 144, 255, 163, 146, 255, 168, 149, 255, 173, 152, 255, 176, 153, 255, 178, 155, 255, 184, 160, 255, 191, 165, 255, 195, 168, 255, 199, 172, 255, 203, 175, 255, 207, 179, 255, 211, 182, 255, 216, 185, 255, 218, 190, 255, 220, 196, 255, 222, 200, 255, 225, 202, 255, 227, 204, 255, 230, 206, 255, 233, 208};
const int colormap_ironblack[] = {255, 255, 255, 253, 253, 253, 251, 251, 251, 249, 249, 249, 247, 247, 247, 245, 245, 245, 243, 243, 243, 241, 241, 241, 239, 239, 239, 237, 237, 237, 235, 235, 235, 233, 233, 233, 231, 231, 231, 229, 229, 229, 227, 227, 227, 225, 225, 225, 223, 223, 223, 221, 221, 221, 219, 219, 219, 217, 217, 217, 215, 215, 215, 213, 213, 213, 211, 211, 211, 209, 209, 209, 207, 207, 207, 205, 205, 205, 203, 203, 203, 201, 201, 201, 199, 199, 199, 197, 197, 197, 195, 195, 195, 193, 193, 193, 191, 191, 191, 189, 189, 189, 187, 187, 187, 185, 185, 185, 183, 183, 183, 181, 181, 181, 179, 179, 179, 177, 177, 177, 175, 175, 175, 173, 173, 173, 171, 171, 171, 169, 169, 169, 167, 167, 167, 165, 165, 165, 163, 163, 163, 161, 161, 161, 159, 159, 159, 157, 157, 157, 155, 155, 155, 153, 153, 153, 151, 151, 151, 149, 149, 149, 147, 147, 147, 145, 145, 145, 143, 143, 143, 141, 141, 141, 139, 139, 139, 137, 137, 137, 135, 135, 135, 133, 133, 133, 131, 131, 131, 129, 129, 129, 126, 126, 126, 124, 124, 124, 122, 122, 122, 120, 120, 120, 118, 118, 118, 116, 116, 116, 114, 114, 114, 112, 112, 112, 110, 110, 110, 108, 108, 108, 106, 106, 106, 104, 104, 104, 102, 102, 102, 100, 100, 100, 98, 98, 98, 96, 96, 96, 94, 94, 94, 92, 92, 92, 90, 90, 90, 88, 88, 88, 86, 86, 86, 84, 84, 84, 82, 82, 82, 80, 80, 80, 78, 78, 78, 76, 76, 76, 74, 74, 74, 72, 72, 72, 70, 70, 70, 68, 68, 68, 66, 66, 66, 64, 64, 64, 62, 62, 62, 60, 60, 60, 58, 58, 58, 56, 56, 56, 54, 54, 54, 52, 52, 52, 50, 50, 50, 48, 48, 48, 46, 46, 46, 44, 44, 44, 42, 42, 42, 40, 40, 40, 38, 38, 38, 36, 36, 36, 34, 34, 34, 32, 32, 32, 30, 30, 30, 28, 28, 28, 26, 26, 26, 24, 24, 24, 22, 22, 22, 20, 20, 20, 18, 18, 18, 16, 16, 16, 14, 14, 14, 12, 12, 12, 10, 10, 10, 8, 8, 8, 6, 6, 6, 4, 4, 4, 2, 2, 2, 0, 0, 0, 0, 0, 9, 2, 0, 16, 4, 0, 24, 6, 0, 31, 8, 0, 38, 10, 0, 45, 12, 0, 53, 14, 0, 60, 17, 0, 67, 19, 0, 74, 21, 0, 82, 23, 0, 89, 25, 0, 96, 27, 0, 103, 29, 0, 111, 31, 0, 118, 36, 0, 120, 41, 0, 121, 46, 0, 122, 51, 0, 123, 56, 0, 124, 61, 0, 125, 66, 0, 126, 71, 0, 127, 76, 1, 128, 81, 1, 129, 86, 1, 130, 91, 1, 131, 96, 1, 132, 101, 1, 133, 106, 1, 134, 111, 1, 135, 116, 1, 136, 121, 1, 136, 125, 2, 137, 130, 2, 137, 135, 3, 137, 139, 3, 138, 144, 3, 138, 149, 4, 138, 153, 4, 139, 158, 5, 139, 163, 5, 139, 167, 5, 140, 172, 6, 140, 177, 6, 140, 181, 7, 141, 186, 7, 141, 189, 10, 137, 191, 13, 132, 194, 16, 127, 196, 19, 121, 198, 22, 116, 200, 25, 111, 203, 28, 106, 205, 31, 101, 207, 34, 95, 209, 37, 90, 212, 40, 85, 214, 43, 80, 216, 46, 75, 218, 49, 69, 221, 52, 64, 223, 55, 59, 224, 57, 49, 225, 60, 47, 226, 64, 44, 227, 67, 42, 228, 71, 39, 229, 74, 37, 230, 78, 34, 231, 81, 32, 231, 85, 29, 232, 88, 27, 233, 92, 24, 234, 95, 22, 235, 99, 19, 236, 102, 17, 237, 106, 14, 238, 109, 12, 239, 112, 12, 240, 116, 12, 240, 119, 12, 241, 123, 12, 241, 127, 12, 242, 130, 12, 242, 134, 12, 243, 138, 12, 243, 141, 13, 244, 145, 13, 244, 149, 13, 245, 152, 13, 245, 156, 13, 246, 160, 13, 246, 163, 13, 247, 167, 13, 247, 171, 13, 248, 175, 14, 248, 178, 15, 249, 182, 16, 249, 185, 18, 250, 189, 19, 250, 192, 20, 251, 196, 21, 251, 199, 22, 252, 203, 23, 252, 206, 24, 253, 210, 25, 253, 213, 27, 254, 217, 28, 254, 220, 29, 255, 224, 30, 255, 227, 39, 255, 229, 53, 255, 231, 67, 255, 233, 81, 255, 234, 95, 255, 236, 109, 255, 238, 123, 255, 240, 137, 255, 242, 151, 255, 244, 165, 255, 246, 179, 255, 248, 193, 255, 249, 207, 255, 251, 221, 255, 253, 235, 255, 255, 24};

uint8_t mat[61][81];
int mathigh[119][159];
float min1,max1,tem1;
string linex;
ofFile datax;
ofTrueTypeFont Font1,Font2;
const int *colormap;
int color=0;
int EPS=0;
EPSView *gui;
string EPS_ip;

//--------------------------------------------------------------
void ofApp::setup(){

    // iOS GUI 設定
    // 田所先生のWEB上のやり方http://yoppa.org/blog/2995.htmlだとXcode自体が少し昔のものなのでうまくいかなかった。他の方も
    // http://www.creativeapplications.net/tutorials/integrating-native-uikit-to-your-existing-openframeworks-ios-project/
    // でさらに詳しく説明されているが、これでも上手くいかない。また、OFに添付されたいるiOSのサンプル(iPhoneGuiExample)は動くが設定の経緯が分からない。
    // 結局  GUI フォルダを 作って 3つのファイルを自動で作成させ 自動で作成される xxxx.m の拡張子を .mm に変えるまでは田所先生の説明、
    //  すなわちnewfile->iOS->Source->Cocoa touch Class->Next->Class名変更、サブクラスはそのまま、Also creat XLB fileにチェック
    //  さらに SWIFT から Objectiv C に変更
    // 次に各ファイルの内容とボタン作成の操作を上記ブログを参考にして変更するとうまくいった。慣れればすごく簡単。以下2行ofApp.mm内での設定
    //  EPSViewはGUIのファイル名。テキトーに修正して使う。
    gui = [[EPSView alloc] initWithNibName:@"EPSView" bundle:nil];
    [ofxiOSGetGLParentView() addSubview:gui.view];
    
	cout << "listening for osc messages on port " << PORT << "\n";
	receiver.setup( PORT );

	current_msg_string = 0;

	Font1.load("cooperBlack.ttf", 18, true, true, true);
    Font2.load("cooperBlack.ttf", 48, true, true, true);
	ofBackground( 0,0,0);
    
    //*******GUI関連*******
    colormap = colormap_rainbow;
    EPS_button=0;
    EPS=0;
    EPS_swA=1;
    EPS_swB=0;
    EPS_sli=1;
    EPS_ip="Not Connect!!!";
}
//--------------------------------------------------------------
void ofApp::update(){
    
    //********iPhone GUI********
    if (EPS_button==1) {
        if (color==0) {
            colormap = colormap_rainbow;
            color=1;
        }
        else if (color==1) {
            colormap = colormap_ironblack;
            color=0;
        }
        EPS_button=0;
    }
    //********OSC Send********
    if (EPS==1) {
    if( EPS_swA==1 ){
        ofxOscMessage mm;
        mm.setAddress( "/led" );
        mm.addIntArg(EPS_swA);
        sender.sendMessage( mm );
        EPS_swA=0;
    }
    if( EPS_swB==0){
        ofxOscMessage mm;
        mm.setAddress( "/led1" );
        mm.addIntArg(EPS_swB );
        sender.sendMessage( mm );
        EPS_swB=1;
    }
    }
	//********OSC Receve********
	for( int i=0; i<NUM_MSG_STRINGS; i++ ){
		if( timers[i] < ofGetElapsedTimef() )
			msg_strings[i] = "";
	}
	while( receiver.hasWaitingMessages() ){
		// get the next message
		ofxOscMessage m;
		receiver.getNextMessage(m);
        if(m.getAddress() == "/img_8"){
            linex = m.getArgAsString(0);
            if(linex[0]==0x3c) linex[0]=0;
            for(int yy=0;yy<82;yy++){
                
                if(yy != 81) mat[linex[0]][yy]=uint8_t(linex[yy+1]);
                else if(yy != 0) mat[linex[0]][yy-1]=uint8_t(linex[yy]);
            }
        }
        else if(m.getAddress()=="/ip")
        {
            if(EPS==0){
            EPS_ip = m.getArgAsString(0);
            sender.setup( EPS_ip , PORT1 );
            EPS=1;
            }
        }
        else if(m.getAddress() == "/minmax" ){
            min1=m.getArgAsFloat(0);
            max1=m.getArgAsFloat(1);
            tem1=m.getArgAsFloat(2);
        }
	}	
}

//--------------------------------------------------------------
void ofApp::draw(){

    string buf;

    ofSetColor(255,210,0);
    Font2.drawString("Thermalcam!!", 70, 170-30);
    Font1.drawString(EPS_ip, 15, 50);

   	buf = "Min=" + ofToString(min1,2);
    ofSetColor(100,100,255);
    Font1.drawString(buf, 15, 360-50);
    
    buf = "Max=" + ofToString(max1,2);
    ofSetColor(255,0,0);
    Font1.drawString(buf, 460, 360-50);

    buf = "Temp=" + ofToString(tem1,2);
    ofSetColor(255,255,255);
    Font1.drawString(buf, 225, 310-50);
    
    ofSetColor(255,110,0);
    Font1.drawString("by Takesan", 460, 1070+30);
    
    ofSetColor(120,120,255);
    ofDrawRectangle(178,48+290-50,260,29);
    
    for(int i=0;i<256;i++) {
        ofSetColor( colormap[3*i], colormap[3*i+1], colormap[3*i+2]);
        ofDrawLine(i+180, 340-50, i+180, 365-50);
    }
    for (int i=0;i<60;i++){
        for (int j=0;j<80;j++) {
            mathigh[i*2][j*2]=mat[i][j];
        }
    }

    for (int i=0;i<119;i=i+2){
        for (int j=1;j<159;j=j+2) {
            mathigh[i][j]=(mathigh[i][j-1]+mathigh[i][j+1])/2;
        }
    }
    for (int i=1;i<119;i=i+2){
        for (int j=0;j<159;j++) {
            mathigh[i][j]=(mathigh[i-1][j]+mathigh[i+1][j])/2;
        }
    }
    for (int i=0;i<119;i++){
        for (int j=0;j<159;j++) {
            ofSetColor( colormap[3*mathigh[i][j]]*EPS_sli, colormap[3*mathigh[i][j]+1], colormap[3*mathigh[i][j]+2]);
            ofDrawCircle(j*4+5, i*4+400-30, 2);
        }
    }
}

//--------------------------------------------------------------
void ofApp::exit(){
}
//--------------------------------------------------------------
void ofApp::touchDown(ofTouchEventArgs &touch){
    if (color==0) {
         colormap = colormap_rainbow;
        color=1;
    }
    else if (color==1) {
        colormap = colormap_ironblack;
        color=0;

    }
}
//--------------------------------------------------------------
void ofApp::touchMoved(ofTouchEventArgs &touch){
}
//--------------------------------------------------------------
void ofApp::touchUp(ofTouchEventArgs &touch){
}
//--------------------------------------------------------------
void ofApp::touchDoubleTap(ofTouchEventArgs &touch){
}
//--------------------------------------------------------------
void ofApp::touchCancelled(ofTouchEventArgs & touch){
}
//--------------------------------------------------------------
void ofApp::lostFocus(){
}
//--------------------------------------------------------------
void ofApp::gotFocus(){
}
//--------------------------------------------------------------
void ofApp::gotMemoryWarning(){
}
//--------------------------------------------------------------
void ofApp::deviceOrientationChanged(int newOrientation){
}

ここからGUI関連

 作成方法はofApp.mmの最初の方のコメント参照。
 EPSView.xlb(ストーリーボード)は以下の画像を参照してください。今回は、スイッチとスライダー、ボタンを作ります。
f:id:TAKEsan:20160224222000p:plain

以下 EPSView.mm

//
//  EPSView.h
//
// とにかく◯◯◯◯◯◯.xlb作成前にコピーしとく。
//  この表記方法ムッとくる。
//
//

#import <UIKit/UIKit.h>

@interface EPSView : UIViewController
@property(retain, nonatomic) IBOutlet UISlider *ESPSlider;
@property(retain, nonatomic) IBOutlet UISwitch *EPSSwitchA;
@property(retain, nonatomic) IBOutlet UISwitch *EPSSwitchB;
@property(retain, nonatomic) IBOutlet UIButton *EPSButton;


@end

以下 EPSView.mm

//
//  EPSView.m
//  とにかく◯◯◯◯◯◯.xlb作成前にコピーしとく。GUIを◯◯◯◯◯◯.xlbで作ったら、選択するだけで済む。
// 以下はスライダー、スイッチ、ボタン Openframeworksへの値の渡し方は以下参照
//  ◯◯◯◯◯◯.xlbでスライダー、スイッチ、ボタンを作成したら、そのGUIを右クリックしてFile's Ownerヘドラックすると以下の関数が
//  表示されるので選択するだけ。
//  ◯◯◯◯◯◯.xlb 操作中、Viewの W=320、H=570として描画すること。でないとOFの画面と合わない。これはSaveするとなぜかリセットされる(多分バグ0
// さらにViewのBackgrand をディフォルトにしておくこと。何もしないと白に設定されてるので。OFでつくった画像が真っ白けになる。最初から白なんてこれもバグ。
//  Created by Takesan on 2016/02/23.
// 
//

#import "EPSView.h"    //ファイル名を変更してれば修正必要
#include "ofxiOSExtras.h"
#include "ofApp.h"

@interface EPSView ()

@end

@implementation EPSView   //ファイル名を変更してれば修正必要

ofApp *myApp;


- (void)viewDidLoad {
    [super viewDidLoad];
    myApp = (ofApp*)ofGetAppPtr();

    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(IBAction)ESPSlide:(id)sender {
    UISlider *sliderObj = sender;
    myApp->EPS_sli = [sliderObj value];
}

-(IBAction)EPSSwitchA:(id)sender{
    // スイッチのON,OFFを知りたい時は以下のコメント実行
    //UISwitch * switchObj = sender;
    //myApp->EPS_swA =[switchObj isOn];
    myApp->EPS_swA = 1;

}
-(IBAction)EPSSwitchB:(id)sender{
   // スイッチのON,OFFを知りたい時は以下のコメント実行
   // UISwitch * switchObj = sender;
   //myApp->EPS_swA =[switchObj isOn];
    myApp->EPS_swB = 0;
}
-(IBAction)EPSButton:(id)sender{
//ボタンに関しては押されるとイベントが発生してここに来るのでOFの変数に何か返してやればOK
//ここではmyApp->EPS_button =1 でもいい。なーんだ簡単(かなり悩んだ)。
    UIButton * btnObj = sender;
    myApp->EPS_button = btnObj.isTouchInside;
}

@end

以下 ESP-WROOM-02 Arduino ソース(最終)

extern "C"{
  #include <spi.h>
  #include <spi_register.h>
}
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>

#include <OSCBundle.h>
#include <OSCData.h>

OSCErrorCode error;

#define ADDRESS  (0x2A)
#define AGC (0x01)
#define SYS (0x02)
#define VID (0x03)
#define OEM (0x08)
#define GET (0x00)
#define SET (0x01)
#define RUN (0x02)
#define VOSPI_FRAME_SIZE (164)
#define COMMANDID_REG (0x04)
#define DATALEN_REG (0x06)
#define DATA0 (0x08)

char ssid[] = "YYYY-iPhone";          // your iPhone Name SSID (name)
char pass[] = "XXXXXXXXX";          // your iPhone テザリングパスワード 

 WiFiUDP Udp;                                // A UDP instance to let us send and receive packets over UDP

const IPAddress outIp(172,20,10,1);         //iPhonr テザリング固定 ip どの機種でも同じ。よって変更なし。このままでOK
const unsigned int outPort = 8090;          // remote port to receive OSC このままでOK
const unsigned int localPort = 8091;        // local port to listen for OSC   このままでOK

char zz[82];
unsigned int min = 65536;
unsigned int max = 0;
float diff;
char ip_ESP[16];

void setup()
{
    pinMode(15, OUTPUT);   //  SSピン
    pinMode(16, OUTPUT);   //  LED
    Wire.begin();
    Serial.begin(115200);
  
    spi_init(HSPI);
    spi_clock(HSPI,1.7,2);  //spi_clock(HSPI,5,2);spi_clock(HSPI,1.6666,2)
  
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    //ローカルipを文字列に変換。時間がかかった。
    sprintf(ip_ESP, "%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
    Serial.println(ip_ESP);
    Serial.println("Starting UDP");
    Udp.begin(localPort);
    Serial.print("Local port: ");
    Serial.println(Udp.localPort());

}

static uint16_t lepton_image[63][82]; 

void read_lepton_frame(void)
{
  int i;
  uint16_t data = 0x000f;
  delay(50); //delay(185);
  while ((data & 0x000f == 0x000f))
  { 
    data = spi_rx16(HSPI);
    lepton_image[0][0] =data;
    for (i = 0; i < 81; i++)
    {    
      lepton_image[0][i+1]=spi_rx16(HSPI); 
     //digitalWrite(15, HIGH);    
    }  
  }
  for (int frame_number = 1; frame_number < 60; frame_number++){ 
    for (i = 0; i < 82; i++)
    {
        lepton_image[frame_number][i] = spi_rx16(HSPI);
     }
   }
}

void lepton_sync(void)
{
  int i;
  uint16_t data = 0x000f;
  uint16_t aaa;

  while (data & 0x000f == 0x000f)
{
    data = spi_rx16(HSPI);
    for (i = 0; i < 81; i++)
    {
      aaa= spi_rx16(HSPI);
    }  
  }
}

void print_lepton_frame(void)
{
  int i;
  for (int frame_number = 0; frame_number < 60; frame_number++){ 
    for (i = 0; i < (VOSPI_FRAME_SIZE / 2); i++)
    {
        Serial.print(lepton_image[frame_number][i] ,HEX);
        Serial.print(",");
    }
        Serial.println(" ");
  }
        Serial.println(" "); 
}

void lepton_command(unsigned int moduleID, unsigned int commandID, unsigned int command)
{
  byte error;
  Wire.beginTransmission(ADDRESS);

  // Command Register is a 16-bit register located at Register Address 0x0004
  Wire.write(0x00);
  Wire.write(0x04);

  if (moduleID == 0x08) //OEM module ID
  {
    Wire.write(0x48);
  }
  else
  {
    Wire.write(moduleID & 0x0f);
  }
  Wire.write( ((commandID << 2 ) & 0xfc) | (command & 0x3));

  error = Wire.endTransmission();    // stop transmitting
  if (error != 0)
  {
    Serial.print("error=");
    Serial.println(error);
  }
}

void set_reg(unsigned int reg)
{
  byte error;
  Wire.beginTransmission(ADDRESS); // transmit to device #4
  Wire.write(reg >> 8 & 0xff);
  Wire.write(reg & 0xff);            // sends one byte

  error = Wire.endTransmission();    // stop transmitting
  if (error != 0)
  {
    Serial.print("error=");
    Serial.println(error);
  }
}

int read_reg(unsigned int reg)
{
  int reading = 0;
  set_reg(reg);
  Wire.requestFrom(ADDRESS, 2);
  reading = Wire.read();  // receive high byte (overwrites previous reading)
  reading = reading << 8;    // shift high byte to be high 8 bits
  reading |= Wire.read(); // receive low byte as lower 8 bits
  return reading;
}

int read_data()
{
  int i;
  int data;
  int payload_length;

  while (read_reg(0x2) & 0x01)
  {
    Serial.println("busy");
  }
  payload_length = read_reg(0x6);
  Wire.requestFrom(ADDRESS, payload_length);
  //set_reg(0x08);
  for (i = 0; i < (payload_length / 2); i++)
  {
    data = Wire.read() << 8;
    data |= Wire.read();
  }
  return data;
}

void led(OSCMessage &msg) {
int  ledState = msg.getInt(0);
  //LEDを点灯させる。
  digitalWrite(16, HIGH); 
  delay(500);
  digitalWrite(16, LOW);
}
void led1(OSCMessage &msg) {
int  ledState = msg.getInt(0);
  //LEPTONのシャッターをぱちくりさせるだけ。
  lepton_command(SYS, 0x42 >> 2 , RUN);
  read_data();
}

void loop()
{
  int i,p;
  int reading = 0;
  String debugString;
  unsigned int col;
  float value_min,value_max,temp;
  long do_ffc = 0;

  Serial.println("SYS Telemetry Enable State");
  lepton_command(SYS, 0x19>>2 ,GET);
  read_data(); 
  delay(500);
 while(1){
        //*****************Send OSC data*****************
        //iPhoneが切れている場合があるので常時ip。
        OSCMessage msg("/ip");
        msg.add(ip_ESP);
        Udp.beginPacket(outIp, outPort);
        msg.send(Udp);
        Udp.endPacket();
        msg.empty();   
  //**********Osc Receive**********
  OSCBundle bundle;
  int size = Udp.parsePacket();
  if (size > 0) {
    while (size--) {
      bundle.fill(Udp.read());
    }
    if (!bundle.hasError()) {
      //ここでアドレスと実行関数を記述。羅列でいいみたい。アドレスが一致しないと読み飛ばす。
      bundle.dispatch("/led", led);
      bundle.dispatch("/led1", led1);
    } else {
      error = bundle.getError();
      Serial.print("error: ");
      Serial.println(error);
    }
  }    
  min = 65536;
  max = 0;
  read_lepton_frame();
     for (int frame_number = 0; frame_number < 60; frame_number++){     
       for (i = 0; i < 82; i++)
       {
          p=lepton_image[frame_number][i] ;
          if(i >= 2){ 
              if(p < min) min = p;
              if(p > max) max = p;
          }
          if(i==0) lepton_image[frame_number][i]=(p & 0x00ff);
       }
     }     
     diff = max - min;
     diff = diff / 256.0f;
     if(diff < 0.56f) diff = 0.56f; // 0.66
        lepton_command(SYS, 0x10 >> 2 , GET); //ondo : 0x14 = chip   0x10 = aux
        float aux=read_data() ;
        float fpatemp = aux/ 100.0f;
        temp=fpatemp-273.15 ;
        float fpatemp_f = fpatemp * 1.8f - 459.67f;
        value_min = ((0.05872 * (float)min - 472.22999f + fpatemp_f));
        value_min= (value_min - 32.0f) / 1.8f;
        value_max = ((0.05872 * (float)max - 472.22999f + fpatemp_f));
        value_max = (value_max - 32.0f) / 1.8f;
        Serial.println(value_min);
        Serial.println(value_max);
        //*****************Send osc data*****************
        OSCMessage msg1("/minmax");
        msg1.add(value_min).add(value_max).add(temp);
        Udp.beginPacket(outIp, outPort);
        msg1.send(Udp);
        Udp.endPacket();
        msg1.empty();   
        //***********************************     
        delay(2); 
    for (int frame_number = 0; frame_number < 60; frame_number++){ 
       for (int i = 2; i < 82; i++)
       {
            col=lepton_image[frame_number][i] ;
            col = col - min;
            col = col / diff;
            if(col <= 0) col = 1;  //=0
            if(col > 255) col = 255;
            zz[i-1]=col;
       }
     zz[0]=frame_number; 
     if(frame_number==0) zz[0]=0x3c; //=0 nara 0x3c tosuru.
     zz[82]='\0';   //String End
      //*****************Send osc data*****
      OSCMessage msg2("/img_8");
      msg2.add(zz );
      Udp.beginPacket(outIp, outPort);
      msg2.send(Udp);
      Udp.endPacket();
      msg2.empty();   
      //***********************************
      delay(1);       // <----絶対必要
    }
    digitalWrite(15, HIGH);  // <----絶対必要
   }
}

今までセンサーが特殊すぎたので、次回はもっと手に入りやすいセンサーやサーボを使った物を作ろうと思います。