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);

Dependencies:   mbed lwip

Committer:
iva2k
Date:
Wed Dec 09 01:55:06 2009 +0000
Revision:
0:5e068c08724a

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iva2k 0:5e068c08724a 1 // Written by IVA2K
iva2k 0:5e068c08724a 2 //
iva2k 0:5e068c08724a 3 // Example of HTTPServer with additional features:
iva2k 0:5e068c08724a 4 // * Link status indication (LED4)
iva2k 0:5e068c08724a 5 // * Local file system (create index.htm page on MBED!)
iva2k 0:5e068c08724a 6 // * RPC-able class (myrpc, allows remote function call that blinks LED1 N times)
iva2k 0:5e068c08724a 7 //
iva2k 0:5e068c08724a 8 // Instructions:
iva2k 0:5e068c08724a 9 // 1 Plug MBED into BoB2 (or other ETH connector breakout)
iva2k 0:5e068c08724a 10 // 2 Plug ETH connector into your network (needs DHCP to get IP address)
iva2k 0:5e068c08724a 11 // 3 Power up MBED using USB cable
iva2k 0:5e068c08724a 12 // 4 Install MBED serial driver (http://mbed.org/handbook/SerialPC)
iva2k 0:5e068c08724a 13 // 5 Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct)
iva2k 0:5e068c08724a 14 // 6 Open terminal on the mbed serial port
iva2k 0:5e068c08724a 15 // 7 Push MBED reset button
iva2k 0:5e068c08724a 16 // 8 Terminal will display info message with mac address, followed by IP address (if connection succeeds)
iva2k 0:5e068c08724a 17 // 9 Open browser and enter the following URL, inserting your MBED's IP address:
iva2k 0:5e068c08724a 18 // http://10.0.0.321/rpc/myrpc1/blink,10 (instead of 10.0.0.321 use MBED IP address from terminal)
iva2k 0:5e068c08724a 19 // 10 MBED will blink the LED 10 times
iva2k 0:5e068c08724a 20 //
iva2k 0:5e068c08724a 21 // Notes: there are still some bugs in HTTPServer code.
iva2k 0:5e068c08724a 22 // To help fight some of them, copy a valid favicon.ico (a 16x16 icon) file to MBED.
iva2k 0:5e068c08724a 23 //
iva2k 0:5e068c08724a 24
iva2k 0:5e068c08724a 25 #include "mbed.h"
iva2k 0:5e068c08724a 26 #include "HTTPServer.h"
iva2k 0:5e068c08724a 27 #include "HTTPRPC.h"
iva2k 0:5e068c08724a 28 #include "HTTPFS.h"
iva2k 0:5e068c08724a 29 #include "myrpc.h"
iva2k 0:5e068c08724a 30
iva2k 0:5e068c08724a 31 DigitalOut led1(LED1, "led1");
iva2k 0:5e068c08724a 32 DigitalOut led2(LED2, "led2");
iva2k 0:5e068c08724a 33 DigitalOut led3(LED3, "led3");
iva2k 0:5e068c08724a 34 DigitalOut led4(LED4, "led4");
iva2k 0:5e068c08724a 35 LocalFileSystem local("local");
iva2k 0:5e068c08724a 36 myrpc myrpc1(LED1, "myrpc1");
iva2k 0:5e068c08724a 37
iva2k 0:5e068c08724a 38 extern Ethernet eth; // eth is defined elsewhere, avoid compiler error.
iva2k 0:5e068c08724a 39 Serial pc(USBTX, USBRX);
iva2k 0:5e068c08724a 40 Ticker eth_timer;
iva2k 0:5e068c08724a 41
iva2k 0:5e068c08724a 42 #define LED_ETH_LINK(val) do { led4=val; } while (0)
iva2k 0:5e068c08724a 43 void eth_link_status() {
iva2k 0:5e068c08724a 44 static bool first = true; // Avoid duplicate IP report on the very first pass
iva2k 0:5e068c08724a 45 static int eth_link = -1; // Last state of eth link
iva2k 0:5e068c08724a 46 int new_link = eth.link();
iva2k 0:5e068c08724a 47 if (eth_link != new_link) {
iva2k 0:5e068c08724a 48 if (new_link) {
iva2k 0:5e068c08724a 49 // From http://mbed.org/forum/post/909/
iva2k 0:5e068c08724a 50 NetServer *net = NetServer::get();
iva2k 0:5e068c08724a 51 struct ip_addr ip = net->getIPAddr();
iva2k 0:5e068c08724a 52 // struct ip_addr gw = net->getGateway();
iva2k 0:5e068c08724a 53 // struct ip_addr nm = net->getNetmask();
iva2k 0:5e068c08724a 54 // struct ip_addr dns = net->getDNS1();
iva2k 0:5e068c08724a 55 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);
iva2k 0:5e068c08724a 56 first = false;
iva2k 0:5e068c08724a 57 }
iva2k 0:5e068c08724a 58 else {
iva2k 0:5e068c08724a 59 printf("IP: <link down>\r\n");
iva2k 0:5e068c08724a 60 }
iva2k 0:5e068c08724a 61 LED_ETH_LINK(new_link);
iva2k 0:5e068c08724a 62 eth_link = new_link;
iva2k 0:5e068c08724a 63 }
iva2k 0:5e068c08724a 64 }
iva2k 0:5e068c08724a 65
iva2k 0:5e068c08724a 66 int main(void) {
iva2k 0:5e068c08724a 67 char mac[6];
iva2k 0:5e068c08724a 68
iva2k 0:5e068c08724a 69 led1=1;
iva2k 0:5e068c08724a 70 led2=1;
iva2k 0:5e068c08724a 71 led3=1;
iva2k 0:5e068c08724a 72 led4=1;
iva2k 0:5e068c08724a 73
iva2k 0:5e068c08724a 74 char *hostname = "mbed";
iva2k 0:5e068c08724a 75 HTTPServer http(hostname); // Use DHCP
iva2k 0:5e068c08724a 76 http.timeout(60000);
iva2k 0:5e068c08724a 77
iva2k 0:5e068c08724a 78 eth.address(mac);
iva2k 0:5e068c08724a 79 pc.printf("\r\n\r\nMBED HTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n",
iva2k 0:5e068c08724a 80 hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
iva2k 0:5e068c08724a 81
iva2k 0:5e068c08724a 82 Base::add_rpc_class<AnalogIn>();
iva2k 0:5e068c08724a 83 Base::add_rpc_class<AnalogOut>();
iva2k 0:5e068c08724a 84 Base::add_rpc_class<DigitalIn>();
iva2k 0:5e068c08724a 85 Base::add_rpc_class<DigitalOut>();
iva2k 0:5e068c08724a 86 Base::add_rpc_class<PwmOut>();
iva2k 0:5e068c08724a 87 Base::add_rpc_class<Timer>();
iva2k 0:5e068c08724a 88 Base::add_rpc_class<SPI>();
iva2k 0:5e068c08724a 89 Base::add_rpc_class<BusOut>();
iva2k 0:5e068c08724a 90 Base::add_rpc_class<BusIn>();
iva2k 0:5e068c08724a 91 Base::add_rpc_class<myrpc>();
iva2k 0:5e068c08724a 92 led1=0;
iva2k 0:5e068c08724a 93
iva2k 0:5e068c08724a 94 http.addHandler(new HTTPRPC());
iva2k 0:5e068c08724a 95 led2=0;
iva2k 0:5e068c08724a 96
iva2k 0:5e068c08724a 97 http.addHandler(new HTTPFileSystemHandler("/", "/local/"));
iva2k 0:5e068c08724a 98 led3=0;
iva2k 0:5e068c08724a 99
iva2k 0:5e068c08724a 100 // FIXME: BUG:: If eth is not plugged, http.bind() hangs for a while!
iva2k 0:5e068c08724a 101 http.bind();
iva2k 0:5e068c08724a 102 led4 = 0;
iva2k 0:5e068c08724a 103
iva2k 0:5e068c08724a 104 pc.printf("\r"); // Add linefeed for stupid Hyperterminal
iva2k 0:5e068c08724a 105 eth_timer.attach(&eth_link_status, 0.1);
iva2k 0:5e068c08724a 106 while(1) {
iva2k 0:5e068c08724a 107 http.poll(); // Have to call this method at least every 250ms to let the http server run.
iva2k 0:5e068c08724a 108 wait(0.100);
iva2k 0:5e068c08724a 109 }
iva2k 0:5e068c08724a 110 }
iva2k 0:5e068c08724a 111
iva2k 0:5e068c08724a 112 //END