MiMicSDK Websocket module sample program.

Dependencies:   NyFileSystems libMiMic mbed-rtos mbed

Fork of UPnPBasicDevice by Ryo Iizuka

This is Websocket server.

WebSocketのサンプルです。 MiMicSDKのWebsocketはhttpdの1モジュールとして動作するので、ファイルサーバ等と一緒に動かすことが出来ます。

ファームウェアを書き込んだ後にブラウザから "http://192.168.128.39/" にアクセスすると、AIN1のデータをリアルタイムに表示するページが開きます。

Committer:
nyatla
Date:
Fri Sep 27 12:52:34 2013 +0000
Revision:
24:83a1d2bc8709
Parent:
23:6340bfc0bfe3
Child:
25:56d0d879bee7
update comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 4:0a280ed0a848 1 /**
nyatla 20:4b0b449ddb12 2 * @file
nyatla 24:83a1d2bc8709 3 * Websocket server sample.<br/>
nyatla 24:83a1d2bc8709 4 * This program is websocket server template.
nyatla 20:4b0b449ddb12 5 *
nyatla 4:0a280ed0a848 6 */
nyatla 5:6a2a1644ea2c 7 #include "mbed.h"
nyatla 19:66d729b94d16 8 #include "rtos.h"
nyatla 5:6a2a1644ea2c 9 #include "SDFileSystem.h"
nyatla 0:ec1e45489427 10 #include "mimic.h"
nyatla 10:80c05810f911 11 #include "utils/PlatformInfo.h"
nyatla 19:66d729b94d16 12 #include "fsdata.h"
nyatla 19:66d729b94d16 13
nyatla 0:ec1e45489427 14
nyatla 23:6340bfc0bfe3 15 //local filesystem
nyatla 23:6340bfc0bfe3 16 LocalFileSystem2 lf("local");
nyatla 23:6340bfc0bfe3 17
nyatla 23:6340bfc0bfe3 18 NetConfig cfg; //create network configulation
nyatla 19:66d729b94d16 19 Net* net;
nyatla 19:66d729b94d16 20
nyatla 0:ec1e45489427 21 /**
nyatla 20:4b0b449ddb12 22 * Httpd for UPnPService and presentation.
nyatla 0:ec1e45489427 23 */
nyatla 23:6340bfc0bfe3 24 class WebSocketHttpd:public MiMic::Httpd
nyatla 0:ec1e45489427 25 {
nyatla 0:ec1e45489427 26 private:
nyatla 23:6340bfc0bfe3 27 ModLocalFileSystem modlocal;
nyatla 23:6340bfc0bfe3 28 ModWebSocket modwebsocket;
nyatla 20:4b0b449ddb12 29 ModRomFiles modromfs; //ROM file module
nyatla 0:ec1e45489427 30 public:
nyatla 23:6340bfc0bfe3 31 WebSocketHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort())
nyatla 0:ec1e45489427 32 {
nyatla 20:4b0b449ddb12 33 //prepare fs data (presentation.html,icon,image.)
nyatla 20:4b0b449ddb12 34 this->modromfs.setParam("rom",FSDATA,3);
nyatla 23:6340bfc0bfe3 35 this->modlocal.setParam("local");
nyatla 23:6340bfc0bfe3 36 //bind websocket module.
nyatla 23:6340bfc0bfe3 37 this->modwebsocket.setParam("ws");
nyatla 0:ec1e45489427 38 }
nyatla 0:ec1e45489427 39 virtual void onRequest(HttpdConnection& i_connection)
nyatla 0:ec1e45489427 40 {
nyatla 20:4b0b449ddb12 41 //try to ModRomFS module. for icon,images.
nyatla 19:66d729b94d16 42 if(this->modromfs.execute(i_connection)){
nyatla 19:66d729b94d16 43 return;
nyatla 23:6340bfc0bfe3 44 }
nyatla 23:6340bfc0bfe3 45 //try to ModLocalFileSystem
nyatla 23:6340bfc0bfe3 46 if(this->modlocal.execute(i_connection)){
nyatla 23:6340bfc0bfe3 47 return;
nyatla 23:6340bfc0bfe3 48 }
nyatla 23:6340bfc0bfe3 49 //try to Websocket service.
nyatla 23:6340bfc0bfe3 50 if(this->modwebsocket.execute(i_connection)){
nyatla 23:6340bfc0bfe3 51 //send AIN1 value every 10ms.
nyatla 23:6340bfc0bfe3 52 AnalogIn ain(p20);
nyatla 23:6340bfc0bfe3 53 do{
nyatla 23:6340bfc0bfe3 54 Thread::wait(20);
nyatla 23:6340bfc0bfe3 55 }while(this->modwebsocket.writeF("%d,",(int)(ain*4096)));
nyatla 23:6340bfc0bfe3 56 this->modwebsocket.close();
nyatla 19:66d729b94d16 57 return;
nyatla 19:66d729b94d16 58 }
nyatla 20:4b0b449ddb12 59 //Otherwise, Send the redirect response to /rom/index.html
nyatla 20:4b0b449ddb12 60 i_connection.sendHeader(302,
nyatla 20:4b0b449ddb12 61 "text/html",
nyatla 20:4b0b449ddb12 62 "Status: 302:Moved Temporarily\r\n"
nyatla 20:4b0b449ddb12 63 "Location: /rom/index.html\r\n");
nyatla 0:ec1e45489427 64 }
nyatla 0:ec1e45489427 65 };
nyatla 0:ec1e45489427 66
nyatla 12:218b57d9a6d4 67
nyatla 0:ec1e45489427 68 int main()
nyatla 0:ec1e45489427 69 {
nyatla 19:66d729b94d16 70 net=new Net();//Net constructor must be created after started RTOS
nyatla 23:6340bfc0bfe3 71 // manual setting
nyatla 23:6340bfc0bfe3 72 cfg.setIpAddr(192,168,128,39);
nyatla 23:6340bfc0bfe3 73 cfg.setNetMask(255,255,255,0);
nyatla 23:6340bfc0bfe3 74 cfg.setGateway(192,168,128,254);
nyatla 12:218b57d9a6d4 75
nyatla 23:6340bfc0bfe3 76 WebSocketHttpd httpd(cfg); //create a httpd instance.
nyatla 19:66d729b94d16 77 net->start(cfg);
nyatla 0:ec1e45489427 78 httpd.loop(); //start httpd loop.
nyatla 0:ec1e45489427 79 return 0;
nyatla 0:ec1e45489427 80 }