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);
Revision 0:5e068c08724a, committed 2009-12-09
- Comitter:
- iva2k
- Date:
- Wed Dec 09 01:55:06 2009 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 5e068c08724a lwip.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwip.lib Wed Dec 09 01:55:06 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_unsupported/code/lwip/ \ No newline at end of file
diff -r 000000000000 -r 5e068c08724a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 09 01:55:06 2009 +0000 @@ -0,0 +1,112 @@ +// Written by IVA2K +// +// Example of HTTPServer with additional features: +// * 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) +// +// Instructions: +// 1 Plug MBED into BoB2 (or other ETH connector breakout) +// 2 Plug ETH connector into your network (needs DHCP to get IP address) +// 3 Power up MBED using USB cable +// 4 Install MBED serial driver (http://mbed.org/handbook/SerialPC) +// 5 Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct) +// 6 Open terminal on the mbed serial port +// 7 Push MBED reset button +// 8 Terminal will display info message with mac address, followed by IP address (if connection succeeds) +// 9 Open browser and enter the following URL, inserting your MBED's IP address: +// http://10.0.0.321/rpc/myrpc1/blink,10 (instead of 10.0.0.321 use MBED IP address from terminal) +// 10 MBED will blink the LED 10 times +// +// Notes: there are still some bugs in HTTPServer code. +// To help fight some of them, copy a valid favicon.ico (a 16x16 icon) file to MBED. +// + +#include "mbed.h" +#include "HTTPServer.h" +#include "HTTPRPC.h" +#include "HTTPFS.h" +#include "myrpc.h" + +DigitalOut led1(LED1, "led1"); +DigitalOut led2(LED2, "led2"); +DigitalOut led3(LED3, "led3"); +DigitalOut led4(LED4, "led4"); +LocalFileSystem local("local"); +myrpc myrpc1(LED1, "myrpc1"); + +extern Ethernet eth; // eth is defined elsewhere, avoid compiler error. +Serial pc(USBTX, USBRX); +Ticker eth_timer; + +#define LED_ETH_LINK(val) do { led4=val; } while (0) +void eth_link_status() { + static bool first = true; // Avoid duplicate IP report on the very first pass + static int eth_link = -1; // Last state of eth link + int new_link = eth.link(); + if (eth_link != new_link) { + if (new_link) { + // From http://mbed.org/forum/post/909/ + NetServer *net = NetServer::get(); + struct ip_addr ip = net->getIPAddr(); +// struct ip_addr gw = net->getGateway(); +// struct ip_addr nm = net->getNetmask(); +// struct ip_addr dns = net->getDNS1(); + 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); + first = false; + } + else { + printf("IP: <link down>\r\n"); + } + LED_ETH_LINK(new_link); + eth_link = new_link; + } +} + +int main(void) { + char mac[6]; + + led1=1; + led2=1; + led3=1; + led4=1; + + char *hostname = "mbed"; + HTTPServer http(hostname); // Use DHCP + http.timeout(60000); + + eth.address(mac); + pc.printf("\r\n\r\nMBED HTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n", + hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + Base::add_rpc_class<AnalogIn>(); + Base::add_rpc_class<AnalogOut>(); + Base::add_rpc_class<DigitalIn>(); + Base::add_rpc_class<DigitalOut>(); + Base::add_rpc_class<PwmOut>(); + Base::add_rpc_class<Timer>(); + Base::add_rpc_class<SPI>(); + Base::add_rpc_class<BusOut>(); + Base::add_rpc_class<BusIn>(); + Base::add_rpc_class<myrpc>(); + led1=0; + + http.addHandler(new HTTPRPC()); + led2=0; + + http.addHandler(new HTTPFileSystemHandler("/", "/local/")); + led3=0; + +// FIXME: BUG:: If eth is not plugged, http.bind() hangs for a while! + http.bind(); + led4 = 0; + + pc.printf("\r"); // Add linefeed for stupid Hyperterminal + eth_timer.attach(ð_link_status, 0.1); + while(1) { + http.poll(); // Have to call this method at least every 250ms to let the http server run. + wait(0.100); + } +} + +//END \ No newline at end of file
diff -r 000000000000 -r 5e068c08724a mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Dec 09 01:55:06 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0
diff -r 000000000000 -r 5e068c08724a myrpc.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/myrpc.cpp Wed Dec 09 01:55:06 2009 +0000 @@ -0,0 +1,44 @@ +// myrpc.cpp +// +// Based on: +// http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.h +// http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.cpp +// http://mbed.org/forum/topic/234/ + +#include "myrpc.h" +#ifdef MBED_RPC +#include "rpc.h" +#endif + +namespace mbed { + +myrpc::myrpc(PinName pin, const char *name) : Base(name), _pin(pin) {} + +void myrpc::blink(int n) { + for (int i=0; i<n; i++) { + _pin = 1; + wait(0.2); + _pin = 0; + wait(0.2); + } +} + +#ifdef MBED_RPC +const rpc_method *myrpc::get_rpc_methods() { + static const rpc_method rpc_methods[] = { + { "blink", rpc_method_caller<myrpc, int, &myrpc::blink> }, + RPC_METHOD_SUPER(Base) + }; + return rpc_methods; +} +rpc_class *myrpc::get_rpc_class() { + static const rpc_function funcs[] = { + "new", rpc_function_caller<const char*, PinName, const char*, &Base::construct<myrpc,PinName,const char*> >, + RPC_METHOD_END + }; + static rpc_class c = { "myrpc", funcs, NULL }; + return &c; +} +#endif + +} // namespace mbed
diff -r 000000000000 -r 5e068c08724a myrpc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/myrpc.h Wed Dec 09 01:55:06 2009 +0000 @@ -0,0 +1,29 @@ +// myrpc.h +// +// Based on: +// http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.h +// http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.cpp + +#ifndef MYRPC_H +#define MYRPC_H + +#include "mbed.h" + +namespace mbed { + +class myrpc : public Base { +public: + myrpc(PinName pin, const char* name = NULL); + void blink(int n); + +#ifdef MBED_RPC + virtual const struct rpc_method *get_rpc_methods(); + static struct rpc_class *get_rpc_class(); +#endif // MBED_RPC + +protected: + DigitalOut _pin; +}; + +} // namespace mbed +#endif // MYRPC_H \ No newline at end of file