Just a \"collage\" of some existing examples to test the web capabilities of the mBed
Dependencies: EthernetNetIf NTPClient_NetServices mbed HTTPServer
HTTPServerExample.cpp@0:bf8b2e1459d0, 2011-02-17 (annotated)
- Committer:
- guiott
- Date:
- Thu Feb 17 13:13:13 2011 +0000
- Revision:
- 0:bf8b2e1459d0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guiott | 0:bf8b2e1459d0 | 1 | #include "mbed.h" |
guiott | 0:bf8b2e1459d0 | 2 | #include "EthernetNetIf.h" |
guiott | 0:bf8b2e1459d0 | 3 | #include "HTTPServer.h" |
guiott | 0:bf8b2e1459d0 | 4 | #include "SDHCFileSystem.h" |
guiott | 0:bf8b2e1459d0 | 5 | #include "NTPClient.h" |
guiott | 0:bf8b2e1459d0 | 6 | |
guiott | 0:bf8b2e1459d0 | 7 | DigitalOut led1(p29, "led1"); |
guiott | 0:bf8b2e1459d0 | 8 | DigitalOut led2(LED2, "led2"); |
guiott | 0:bf8b2e1459d0 | 9 | DigitalOut led3(LED3, "led3"); |
guiott | 0:bf8b2e1459d0 | 10 | DigitalOut led4(LED4, "led4"); |
guiott | 0:bf8b2e1459d0 | 11 | |
guiott | 0:bf8b2e1459d0 | 12 | // LocalFileSystem fs("webfs"); |
guiott | 0:bf8b2e1459d0 | 13 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs |
guiott | 0:bf8b2e1459d0 | 14 | |
guiott | 0:bf8b2e1459d0 | 15 | EthernetNetIf eth |
guiott | 0:bf8b2e1459d0 | 16 | ( |
guiott | 0:bf8b2e1459d0 | 17 | IpAddr(192,168,1,150), //IP Address |
guiott | 0:bf8b2e1459d0 | 18 | IpAddr(255,255,255,0), //Network Mask |
guiott | 0:bf8b2e1459d0 | 19 | IpAddr(192,168,1,1), //Gateway |
guiott | 0:bf8b2e1459d0 | 20 | IpAddr(192,168,1,1) //DNS |
guiott | 0:bf8b2e1459d0 | 21 | ); |
guiott | 0:bf8b2e1459d0 | 22 | |
guiott | 0:bf8b2e1459d0 | 23 | HTTPServer svr; |
guiott | 0:bf8b2e1459d0 | 24 | NTPClient ntp; |
guiott | 0:bf8b2e1459d0 | 25 | |
guiott | 0:bf8b2e1459d0 | 26 | int main() { |
guiott | 0:bf8b2e1459d0 | 27 | Base::add_rpc_class<DigitalOut>(); |
guiott | 0:bf8b2e1459d0 | 28 | |
guiott | 0:bf8b2e1459d0 | 29 | FILE *fp = fopen("/sd/myfile.txt", "w"); // to initialize SD |
guiott | 0:bf8b2e1459d0 | 30 | fprintf(fp, "\n\rHello World!\n\r"); |
guiott | 0:bf8b2e1459d0 | 31 | fclose(fp); |
guiott | 0:bf8b2e1459d0 | 32 | |
guiott | 0:bf8b2e1459d0 | 33 | printf("Setting up...\n"); |
guiott | 0:bf8b2e1459d0 | 34 | EthernetErr ethErr = eth.setup(); |
guiott | 0:bf8b2e1459d0 | 35 | if(ethErr) |
guiott | 0:bf8b2e1459d0 | 36 | { |
guiott | 0:bf8b2e1459d0 | 37 | printf("Error %d in setup.\n", ethErr); |
guiott | 0:bf8b2e1459d0 | 38 | return -1; |
guiott | 0:bf8b2e1459d0 | 39 | } |
guiott | 0:bf8b2e1459d0 | 40 | printf("Setup OK\n"); |
guiott | 0:bf8b2e1459d0 | 41 | |
guiott | 0:bf8b2e1459d0 | 42 | time_t seconds = time(NULL); |
guiott | 0:bf8b2e1459d0 | 43 | printf("Current time is (UTC): %s\n", ctime(&seconds)); |
guiott | 0:bf8b2e1459d0 | 44 | |
guiott | 0:bf8b2e1459d0 | 45 | Host server(IpAddr(), 123, "ntp.ien.it"); |
guiott | 0:bf8b2e1459d0 | 46 | ntp.setTime(server); |
guiott | 0:bf8b2e1459d0 | 47 | |
guiott | 0:bf8b2e1459d0 | 48 | seconds = time(NULL); |
guiott | 0:bf8b2e1459d0 | 49 | printf("\nTime is now (UTC): %s\n", ctime(&seconds)); |
guiott | 0:bf8b2e1459d0 | 50 | |
guiott | 0:bf8b2e1459d0 | 51 | FSHandler::mount("/sd", "/files"); //Mount /webfs path on /files web path |
guiott | 0:bf8b2e1459d0 | 52 | FSHandler::mount("/sd", "/"); //Mount /webfs path on web root path |
guiott | 0:bf8b2e1459d0 | 53 | |
guiott | 0:bf8b2e1459d0 | 54 | svr.addHandler<SimpleHandler>("/hello"); |
guiott | 0:bf8b2e1459d0 | 55 | svr.addHandler<RPCHandler>("/rpc"); |
guiott | 0:bf8b2e1459d0 | 56 | svr.addHandler<FSHandler>("/files"); |
guiott | 0:bf8b2e1459d0 | 57 | svr.addHandler<FSHandler>("/"); //Default handler |
guiott | 0:bf8b2e1459d0 | 58 | //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
guiott | 0:bf8b2e1459d0 | 59 | |
guiott | 0:bf8b2e1459d0 | 60 | svr.bind(8080); |
guiott | 0:bf8b2e1459d0 | 61 | |
guiott | 0:bf8b2e1459d0 | 62 | printf("Listening...\n"); |
guiott | 0:bf8b2e1459d0 | 63 | |
guiott | 0:bf8b2e1459d0 | 64 | Timer tm; |
guiott | 0:bf8b2e1459d0 | 65 | tm.start(); |
guiott | 0:bf8b2e1459d0 | 66 | //Listen indefinitely |
guiott | 0:bf8b2e1459d0 | 67 | while(true) |
guiott | 0:bf8b2e1459d0 | 68 | { |
guiott | 0:bf8b2e1459d0 | 69 | Net::poll(); |
guiott | 0:bf8b2e1459d0 | 70 | if(tm.read()>1) |
guiott | 0:bf8b2e1459d0 | 71 | { |
guiott | 0:bf8b2e1459d0 | 72 | led1=!led1; //Show that we are alive |
guiott | 0:bf8b2e1459d0 | 73 | tm.start(); |
guiott | 0:bf8b2e1459d0 | 74 | char buffer[32]; |
guiott | 0:bf8b2e1459d0 | 75 | seconds = time(NULL)+3600; |
guiott | 0:bf8b2e1459d0 | 76 | strftime(buffer, 32, "%a %d %b %Y - %H:%M:%S \n", localtime(&seconds)); |
guiott | 0:bf8b2e1459d0 | 77 | printf(" %s", buffer); |
guiott | 0:bf8b2e1459d0 | 78 | } |
guiott | 0:bf8b2e1459d0 | 79 | } |
guiott | 0:bf8b2e1459d0 | 80 | |
guiott | 0:bf8b2e1459d0 | 81 | return 0; |
guiott | 0:bf8b2e1459d0 | 82 | |
guiott | 0:bf8b2e1459d0 | 83 | } |