HTTPServer example with additional functions: * Link status indication (LED4); * Local file system (create index.htm page on MBED!); * RPC-able class (myrpc, allows remote function call that blinks LED1 N times);
main.cpp
00001 // Written by IVA2K 00002 // 00003 // Example of HTTPServer with additional features: 00004 // * Link status indication (LED4) 00005 // * Local file system (create index.htm page on MBED!) 00006 // * RPC-able class (myrpc, allows remote function call that blinks LED1 N times) 00007 // 00008 // Instructions: 00009 // 1 Plug MBED into BoB2 (or other ETH connector breakout) 00010 // 2 Plug ETH connector into your network (needs DHCP to get IP address) 00011 // 3 Power up MBED using USB cable 00012 // 4 Install MBED serial driver (http://mbed.org/handbook/SerialPC) 00013 // 5 Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct) 00014 // 6 Open terminal on the mbed serial port 00015 // 7 Push MBED reset button 00016 // 8 Terminal will display info message with mac address, followed by IP address (if connection succeeds) 00017 // 9 Open browser and enter the following URL, inserting your MBED's IP address: 00018 // http://10.0.0.321/rpc/myrpc1/blink,10 (instead of 10.0.0.321 use MBED IP address from terminal) 00019 // 10 MBED will blink the LED 10 times 00020 // 00021 // Notes: there are still some bugs in HTTPServer code. 00022 // To help fight some of them, copy a valid favicon.ico (a 16x16 icon) file to MBED. 00023 // 00024 00025 #include "mbed.h" 00026 #include "HTTPServer.h" 00027 #include "HTTPRPC.h" 00028 #include "HTTPFS.h" 00029 #include "myrpc.h" 00030 00031 DigitalOut led1(LED1, "led1"); 00032 DigitalOut led2(LED2, "led2"); 00033 DigitalOut led3(LED3, "led3"); 00034 DigitalOut led4(LED4, "led4"); 00035 LocalFileSystem local("local"); 00036 myrpc myrpc1(LED1, "myrpc1"); 00037 00038 extern Ethernet eth; // eth is defined elsewhere, avoid compiler error. 00039 Serial pc(USBTX, USBRX); 00040 Ticker eth_timer; 00041 00042 #define LED_ETH_LINK(val) do { led4=val; } while (0) 00043 void eth_link_status() { 00044 static bool first = true; // Avoid duplicate IP report on the very first pass 00045 static int eth_link = -1; // Last state of eth link 00046 int new_link = eth.link(); 00047 if (eth_link != new_link) { 00048 if (new_link) { 00049 // From http://mbed.org/forum/post/909/ 00050 NetServer *net = NetServer::get(); 00051 struct ip_addr ip = net->getIPAddr(); 00052 // struct ip_addr gw = net->getGateway(); 00053 // struct ip_addr nm = net->getNetmask(); 00054 // struct ip_addr dns = net->getDNS1(); 00055 if (!first) printf("IP: %hhu.%hhu.%hhu.%hhu\r\n", (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF); 00056 first = false; 00057 } 00058 else { 00059 printf("IP: <link down>\r\n"); 00060 } 00061 LED_ETH_LINK(new_link); 00062 eth_link = new_link; 00063 } 00064 } 00065 00066 int main(void) { 00067 char mac[6]; 00068 00069 led1=1; 00070 led2=1; 00071 led3=1; 00072 led4=1; 00073 00074 char *hostname = "mbed"; 00075 HTTPServer http(hostname); // Use DHCP 00076 http.timeout(60000); 00077 00078 eth.address(mac); 00079 pc.printf("\r\n\r\nMBED HTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n", 00080 hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 00081 00082 Base::add_rpc_class<AnalogIn>(); 00083 Base::add_rpc_class<AnalogOut>(); 00084 Base::add_rpc_class<DigitalIn>(); 00085 Base::add_rpc_class<DigitalOut>(); 00086 Base::add_rpc_class<PwmOut>(); 00087 Base::add_rpc_class<Timer>(); 00088 Base::add_rpc_class<SPI>(); 00089 Base::add_rpc_class<BusOut>(); 00090 Base::add_rpc_class<BusIn>(); 00091 Base::add_rpc_class<myrpc>(); 00092 led1=0; 00093 00094 http.addHandler(new HTTPRPC()); 00095 led2=0; 00096 00097 http.addHandler(new HTTPFileSystemHandler("/", "/local/")); 00098 led3=0; 00099 00100 // FIXME: BUG:: If eth is not plugged, http.bind() hangs for a while! 00101 http.bind(); 00102 led4 = 0; 00103 00104 pc.printf("\r"); // Add linefeed for stupid Hyperterminal 00105 eth_timer.attach(ð_link_status, 0.1); 00106 while(1) { 00107 http.poll(); // Have to call this method at least every 250ms to let the http server run. 00108 wait(0.100); 00109 } 00110 } 00111 00112 //END
Generated on Tue Jul 19 2022 21:00:22 by 1.7.2