Dependencies:   EthernetNetIf mbed HTTPServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPServerExample.cpp Source File

HTTPServerExample.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPServer.h"
00004 
00005 DigitalOut led1(LED1, "led1");
00006 DigitalOut led2(LED2, "led2");
00007 DigitalOut led3(LED3, "led3");
00008 DigitalOut led4(LED4, "led4");
00009 
00010 LocalFileSystem fs("webfs");
00011 
00012 EthernetNetIf eth;  
00013 HTTPServer svr;
00014 
00015 int main() {
00016   Base::add_rpc_class<DigitalOut>();
00017 
00018   printf("Setting up...\n");
00019   EthernetErr ethErr = eth.setup();
00020   if(ethErr)
00021   {
00022     printf("Error %d in setup.\n", ethErr);
00023     return -1;
00024   }
00025   printf("Setup OK\n");
00026   
00027   FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
00028   FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
00029   
00030   svr.addHandler<SimpleHandler>("/hello");
00031   svr.addHandler<RPCHandler>("/rpc");
00032   svr.addHandler<FSHandler>("/files");
00033   svr.addHandler<FSHandler>("/"); //Default handler
00034   //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
00035   
00036   svr.bind(80);
00037   
00038   printf("Listening...\n");
00039     
00040   Timer tm;
00041   tm.start();
00042   //Listen indefinitely
00043   while(true)
00044   {
00045     Net::poll();
00046     if(tm.read()>.5)
00047     {
00048       led1=!led1; //Show that we are alive
00049       tm.start();
00050     }
00051   }
00052   
00053   return 0;
00054 
00055 }