Working sample implementation for the EthernetInterface HTTPServer.

Dependencies:   EthernetInterface HTTPServer mbed-rpc mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HTTPServer.h"
00003 #include "FsHandler.h"
00004 #include "LocalFileSystem.h"
00005 #include "RpcHandler.h"
00006 #include "mbed_rpc.h"
00007 
00008 #define ALTERNATIVE
00009 //#undef ALTERNATIVE
00010 
00011 //  Use LED1 to indicate that the main loop is still executing
00012 DigitalOut myled(LED1);
00013 //  Use the serial connection 'pc' to dump debug information
00014 Serial pc(USBTX, USBRX, "pc");
00015 //  Instantiate a HTTPServer to handle incoming requests
00016 HTTPServer  svr;
00017 //  Instantiate a local file system handler named 'local' which will be used later to access files on the mbed.
00018 LocalFileSystem local("local");
00019 
00020 
00021 #ifdef ALTERNATIVE
00022 //  Create the EthernetInterface. This is optional, please see the documentation of HTTP Server's start method.
00023 EthernetInterface eth;
00024 #endif
00025 
00026 
00027 int main() {
00028 
00029     pc.baud(460800);
00030     RPC::add_rpc_class<RpcDigitalOut>();
00031     HTTPFsRequestHandler::mount("/local/", "/");
00032     svr.addHandler<HTTPFsRequestHandler>("/");
00033     svr.addHandler<HTTPRpcRequestHandler>("/RPC");
00034     
00035 #ifdef ALTERNATIVE
00036     //  Initialize the EthernetInterface and initiate a connection using DHCP.
00037     eth.init();
00038     eth.connect();
00039     
00040     if (!svr.start(80, &eth)) {
00041 #else
00042     if (!svr.start()) {
00043 #endif
00044         error("Server not starting !");
00045         exit(0);
00046     }
00047     
00048     while(1) {
00049         svr.poll();
00050         myled = 1;
00051         wait(0.2);
00052         myled = 0;
00053         wait(0.2);
00054     }
00055 }