Dependencies: EthernetNetIf mbed HTTPServer
HTTPServerExample.cpp@0:c88421a1adc9, 2012-04-10 (annotated)
- Committer:
- nenad
- Date:
- Tue Apr 10 14:53:06 2012 +0000
- Revision:
- 0:c88421a1adc9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nenad | 0:c88421a1adc9 | 1 | #include "mbed.h" |
nenad | 0:c88421a1adc9 | 2 | #include "EthernetNetIf.h" |
nenad | 0:c88421a1adc9 | 3 | #include "HTTPServer.h" |
nenad | 0:c88421a1adc9 | 4 | |
nenad | 0:c88421a1adc9 | 5 | DigitalOut led1(LED1, "led1"); |
nenad | 0:c88421a1adc9 | 6 | DigitalOut led2(LED2, "led2"); |
nenad | 0:c88421a1adc9 | 7 | DigitalOut led3(LED3, "led3"); |
nenad | 0:c88421a1adc9 | 8 | DigitalOut led4(LED4, "led4"); |
nenad | 0:c88421a1adc9 | 9 | |
nenad | 0:c88421a1adc9 | 10 | LocalFileSystem fs("webfs"); |
nenad | 0:c88421a1adc9 | 11 | |
nenad | 0:c88421a1adc9 | 12 | EthernetNetIf eth; |
nenad | 0:c88421a1adc9 | 13 | /*EthernetNetIf eth( |
nenad | 0:c88421a1adc9 | 14 | IpAddr(192,168,0,111), //IP Address |
nenad | 0:c88421a1adc9 | 15 | IpAddr(255,255,255,0), //Network Mask |
nenad | 0:c88421a1adc9 | 16 | IpAddr(192,168,0,1), //Gateway |
nenad | 0:c88421a1adc9 | 17 | IpAddr(192,168,0,1) //DNS |
nenad | 0:c88421a1adc9 | 18 | );*/ |
nenad | 0:c88421a1adc9 | 19 | HTTPServer svr; |
nenad | 0:c88421a1adc9 | 20 | |
nenad | 0:c88421a1adc9 | 21 | int main() { |
nenad | 0:c88421a1adc9 | 22 | Base::add_rpc_class<DigitalOut>(); |
nenad | 0:c88421a1adc9 | 23 | |
nenad | 0:c88421a1adc9 | 24 | printf("Setting up...\n"); |
nenad | 0:c88421a1adc9 | 25 | EthernetErr ethErr = eth.setup(); |
nenad | 0:c88421a1adc9 | 26 | if(ethErr) |
nenad | 0:c88421a1adc9 | 27 | { |
nenad | 0:c88421a1adc9 | 28 | printf("Error %d in setup.\n", ethErr); |
nenad | 0:c88421a1adc9 | 29 | return -1; |
nenad | 0:c88421a1adc9 | 30 | } |
nenad | 0:c88421a1adc9 | 31 | printf("Setup OK\n"); |
nenad | 0:c88421a1adc9 | 32 | |
nenad | 0:c88421a1adc9 | 33 | FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path |
nenad | 0:c88421a1adc9 | 34 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
nenad | 0:c88421a1adc9 | 35 | |
nenad | 0:c88421a1adc9 | 36 | svr.addHandler<SimpleHandler>("/hello"); |
nenad | 0:c88421a1adc9 | 37 | svr.addHandler<RPCHandler>("/rpc"); |
nenad | 0:c88421a1adc9 | 38 | svr.addHandler<FSHandler>("/files"); |
nenad | 0:c88421a1adc9 | 39 | svr.addHandler<FSHandler>("/"); //Default handler |
nenad | 0:c88421a1adc9 | 40 | //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
nenad | 0:c88421a1adc9 | 41 | |
nenad | 0:c88421a1adc9 | 42 | svr.bind(80); |
nenad | 0:c88421a1adc9 | 43 | |
nenad | 0:c88421a1adc9 | 44 | printf("Listening...\n"); |
nenad | 0:c88421a1adc9 | 45 | |
nenad | 0:c88421a1adc9 | 46 | Timer tm; |
nenad | 0:c88421a1adc9 | 47 | tm.start(); |
nenad | 0:c88421a1adc9 | 48 | //Listen indefinitely |
nenad | 0:c88421a1adc9 | 49 | while(true) |
nenad | 0:c88421a1adc9 | 50 | { |
nenad | 0:c88421a1adc9 | 51 | Net::poll(); |
nenad | 0:c88421a1adc9 | 52 | if(tm.read()>.5) |
nenad | 0:c88421a1adc9 | 53 | { |
nenad | 0:c88421a1adc9 | 54 | led1=!led1; //Show that we are alive |
nenad | 0:c88421a1adc9 | 55 | tm.start(); |
nenad | 0:c88421a1adc9 | 56 | } |
nenad | 0:c88421a1adc9 | 57 | } |
nenad | 0:c88421a1adc9 | 58 | |
nenad | 0:c88421a1adc9 | 59 | return 0; |
nenad | 0:c88421a1adc9 | 60 | |
nenad | 0:c88421a1adc9 | 61 | } |