Ilya I / Mbed 2 deprecated iva2k_NetHttpServerTcpSockets

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HttpServerTcpSockets.cpp Source File

HttpServerTcpSockets.cpp

00001 #define MY_DEBUG_LEVEL 2
00002 #define MBED_BOB2 1
00003 #define USE_SD 0
00004 
00005 #include "mbed.h"
00006 #include "EthernetNetIf.h"
00007 #include "HTTPServer.h"
00008 #include "StreamServer.h"
00009 
00010 #if USE_SD
00011 #include "SDFileSystem.h"
00012 #endif
00013 
00014 DigitalOut led1(LED1, "led1");
00015 DigitalOut led2(LED2, "led2");
00016 DigitalOut led3(LED3, "led3");
00017 DigitalOut led4(LED4, "led4");
00018 
00019 //LocalFileSystem fs("webfs");
00020 LocalFileSystem local("webfs");
00021 #if USE_SD
00022 SDFileSystem sd(p5, p6, p7, p8, "sd");        // MBED-BoB2
00023 #endif
00024 
00025 EthernetNetIf eth;
00026 //#if 0 // Fixed address
00027 //EthernetNetIf eth(
00028 //  IpAddr(192,168,0,101), //IP Address
00029 //  IpAddr(255,255,255,0), //Network Mask
00030 //  IpAddr(192,168,0,1), //Gateway
00031 //  IpAddr(192,168,0,1)  //DNS
00032 //);
00033 //#endif 
00034 HTTPServer svr;
00035 StreamServer stream;
00036 Serial pc(USBTX, USBRX);
00037 
00038 int gDebug=MY_DEBUG_LEVEL;
00039 float gWait = 0.005;        // Main loop wait timeout
00040 
00041 int main() {
00042   bool use_sd = false;
00043   unsigned int cnt = 0;
00044 
00045 pc.baud(115200);
00046   Base::add_rpc_class<DigitalOut>();
00047 
00048   printf("\r\nSetting up...\r\n");
00049   EthernetErr ethErr = eth.setup();
00050   if(ethErr)
00051   {
00052     printf("Error %d in setup.\n", ethErr);
00053     return -1;
00054   }
00055   printf("\r\nSetup OK\r\n");
00056   
00057 #if USE_SD
00058   // Check if we can use SD card for the web server
00059   FILE *fp = fopen("/sd/index.htm", "r");
00060   if (fp == NULL) {
00061     if (gDebug) printf("DEBUG: No SD card found or no index.htm file - using LocalFilesystem for WebServer.\r\n");
00062   } else {
00063     use_sd = true;
00064     fclose(fp);
00065     if (gDebug) printf("DEBUG: Found SD card with index.htm file - using SD for WebServer.\r\n");
00066   }
00067 #endif
00068 
00069   svr.addHandler<SimpleHandler>("/hello");
00070   svr.addHandler<RPCHandler>("/rpc");
00071   svr.addHandler<FSHandler>(""); //Default handler
00072   //FIXME: can't use this:    , use_sd ? "/sd/" : "/local/");
00073   //Example : Access to mbed.htm : http://a.b.c.d/webfs/mbed.htm
00074   
00075   svr.bind(80);
00076   
00077   printf("\r\nHTTP Listening...\r\n");
00078 
00079 //#### STREAM SOCKETS
00080 stream.bind(123);
00081     
00082   Timer tm;
00083   tm.start();
00084   //Listen indefinitely
00085   while(true)
00086   {
00087     Net::poll();
00088     if(tm.read()>.5)
00089     {
00090       led1=!led1; //Show that we are alive
00091       if ((cnt % 4) == 0) {
00092         char buf[100];
00093         int len = sprintf(buf, ".tick.%d======================================================================\r\n", cnt);
00094         stream.sendToAll(buf, len);
00095         printf("%s - %d clients\r\n", buf, stream.countClients());
00096 //        printf(".tick.%d - %d clients\r\n", cnt, stream.countClients());
00097       }
00098       tm.start();
00099       cnt++;
00100     }
00101 //    wait(gWait);
00102   }
00103 
00104   return 0;
00105 
00106 }