MiMicSDK Websocket module sample program.
Dependencies: NyFileSystems libMiMic mbed-rtos mbed
Fork of UPnPBasicDevice by
This is Websocket server.
WebSocketのサンプルです。 MiMicSDKのWebsocketはhttpdの1モジュールとして動作するので、ファイルサーバ等と一緒に動かすことが出来ます。
ファームウェアを書き込んだ後にブラウザから "http://192.168.128.39/" にアクセスすると、AIN1のデータをリアルタイムに表示するページが開きます。
Diff: main.cpp
- Revision:
- 23:6340bfc0bfe3
- Parent:
- 21:7dbe2100b419
- Child:
- 24:83a1d2bc8709
--- a/main.cpp Sat Aug 10 02:53:46 2013 +0000 +++ b/main.cpp Fri Sep 27 12:48:30 2013 +0000 @@ -16,32 +16,48 @@ #include "fsdata.h" +//local filesystem +LocalFileSystem2 lf("local"); + +NetConfig cfg; //create network configulation Net* net; /** * Httpd for UPnPService and presentation. */ -class UPnPBasicDeviceHttpd:public MiMic::Httpd +class WebSocketHttpd:public MiMic::Httpd { private: - ModUPnPDevice modupnp; + ModLocalFileSystem modlocal; + ModWebSocket modwebsocket; ModRomFiles modromfs; //ROM file module public: - UPnPBasicDeviceHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort()) + WebSocketHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort()) { //prepare fs data (presentation.html,icon,image.) this->modromfs.setParam("rom",FSDATA,3); - //bind upnp service to module. - this->modupnp.setParam(*net); + this->modlocal.setParam("local"); + //bind websocket module. + this->modwebsocket.setParam("ws"); } virtual void onRequest(HttpdConnection& i_connection) { //try to ModRomFS module. for icon,images. if(this->modromfs.execute(i_connection)){ return; - } - //try to UPnP service. for descriptions. - if(this->modupnp.execute(i_connection)){ + } + //try to ModLocalFileSystem + if(this->modlocal.execute(i_connection)){ + return; + } + //try to Websocket service. + if(this->modwebsocket.execute(i_connection)){ + //send AIN1 value every 10ms. + AnalogIn ain(p20); + do{ + Thread::wait(20); + }while(this->modwebsocket.writeF("%d,",(int)(ain*4096))); + this->modwebsocket.close(); return; } //Otherwise, Send the redirect response to /rom/index.html @@ -52,28 +68,16 @@ } }; -NetConfig cfg; //create network configulation int main() { net=new Net();//Net constructor must be created after started RTOS - //Prepare configulation. - cfg.setUPnPIcon(64,64,8,"image/png","/rom/icon.png");//set upnp icon address - cfg.setUPnPUdn(0xe29f7103,0x4ba2,0x01e0,0); //set application timebase-uuid time and sequence field. - cfg.setFriendlyName("UPnPBasicDevice(LPC176x)"); //set friendly name - cfg.setUPnPPresentationURL("/rom/index.html"); //set presentationURL - cfg.setZeroconf(true);//AutoIP enable - - /* - DHCP client has a bug which can not obtain IP address from a DHCP server. - In that case, please turn off setZeroconf and give an IP address manually as temporary solution. - cfg.setIpAddr(1,2,3,4); - cfg.setNetMask(1,2,3,4); - cfg.setGateway(1,2,3,4); - */ - + // manual setting + cfg.setIpAddr(192,168,128,39); + cfg.setNetMask(255,255,255,0); + cfg.setGateway(192,168,128,254); - UPnPBasicDeviceHttpd httpd(cfg); //create a httpd instance. + WebSocketHttpd httpd(cfg); //create a httpd instance. net->start(cfg); httpd.loop(); //start httpd loop. return 0;