Simple code to demonstrate how to mount the internal flash and deploy files via http server requests (http://mbed_ip_address/demo.htm) Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed HTTPServer
main.cpp@0:b1e16a7d2c19, 2011-10-27 (annotated)
- Committer:
- botdream
- Date:
- Thu Oct 27 22:53:32 2011 +0000
- Revision:
- 0:b1e16a7d2c19
GeekSessionLab talk.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:b1e16a7d2c19 | 1 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 2 | #include "mbed.h" |
botdream | 0:b1e16a7d2c19 | 3 | #include "EthernetNetIf.h" |
botdream | 0:b1e16a7d2c19 | 4 | #include "HTTPServer.h" |
botdream | 0:b1e16a7d2c19 | 5 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 6 | DigitalOut myled(LED1); |
botdream | 0:b1e16a7d2c19 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
botdream | 0:b1e16a7d2c19 | 8 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 9 | #define internaldebug // send debug messages to USB Serial port (9600,1,N) |
botdream | 0:b1e16a7d2c19 | 10 | //#define dhcpenable // auto-setup IP Address from DHCP router |
botdream | 0:b1e16a7d2c19 | 11 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 12 | // Timer Interrupt - NetPool |
botdream | 0:b1e16a7d2c19 | 13 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 14 | Ticker netpool; |
botdream | 0:b1e16a7d2c19 | 15 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 16 | // Ethernet Object Setup |
botdream | 0:b1e16a7d2c19 | 17 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 18 | #ifdef dhcpenable |
botdream | 0:b1e16a7d2c19 | 19 | EthernetNetIf eth; |
botdream | 0:b1e16a7d2c19 | 20 | #else |
botdream | 0:b1e16a7d2c19 | 21 | EthernetNetIf eth( |
botdream | 0:b1e16a7d2c19 | 22 | IpAddr(192,168,1,100), //IP Address |
botdream | 0:b1e16a7d2c19 | 23 | IpAddr(255,255,255,0), //Network Mask |
botdream | 0:b1e16a7d2c19 | 24 | IpAddr(192,168,1,254), //Gateway |
botdream | 0:b1e16a7d2c19 | 25 | IpAddr(192,168,1,254) //DNS |
botdream | 0:b1e16a7d2c19 | 26 | ); |
botdream | 0:b1e16a7d2c19 | 27 | #endif |
botdream | 0:b1e16a7d2c19 | 28 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 29 | // HTTP Server |
botdream | 0:b1e16a7d2c19 | 30 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 31 | HTTPServer httpserver; |
botdream | 0:b1e16a7d2c19 | 32 | LocalFileSystem fs("webfs"); |
botdream | 0:b1e16a7d2c19 | 33 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 34 | |
botdream | 0:b1e16a7d2c19 | 35 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 36 | // ISR -> Pool Ethernet - will be triggered by netpool ticker |
botdream | 0:b1e16a7d2c19 | 37 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 38 | void netpoolupdate() |
botdream | 0:b1e16a7d2c19 | 39 | { |
botdream | 0:b1e16a7d2c19 | 40 | Net::poll(); |
botdream | 0:b1e16a7d2c19 | 41 | } |
botdream | 0:b1e16a7d2c19 | 42 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 43 | |
botdream | 0:b1e16a7d2c19 | 44 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 45 | // MAIN |
botdream | 0:b1e16a7d2c19 | 46 | //--------------------------------------------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 47 | int main() |
botdream | 0:b1e16a7d2c19 | 48 | { |
botdream | 0:b1e16a7d2c19 | 49 | // Set Serial Port Transfer Rate |
botdream | 0:b1e16a7d2c19 | 50 | pc.baud(115200); |
botdream | 0:b1e16a7d2c19 | 51 | |
botdream | 0:b1e16a7d2c19 | 52 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 53 | // Setting Ethernet |
botdream | 0:b1e16a7d2c19 | 54 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 55 | #ifdef internaldebug |
botdream | 0:b1e16a7d2c19 | 56 | printf("\r\nSetting up Ethernet interface!\r\n"); |
botdream | 0:b1e16a7d2c19 | 57 | #endif |
botdream | 0:b1e16a7d2c19 | 58 | // Create return object for error check |
botdream | 0:b1e16a7d2c19 | 59 | EthernetErr ethErr = eth.setup(); |
botdream | 0:b1e16a7d2c19 | 60 | if(ethErr) |
botdream | 0:b1e16a7d2c19 | 61 | { |
botdream | 0:b1e16a7d2c19 | 62 | #ifdef internaldebug |
botdream | 0:b1e16a7d2c19 | 63 | printf("\r\nError %d in Ethernet setup.\r\n", ethErr); |
botdream | 0:b1e16a7d2c19 | 64 | #endif |
botdream | 0:b1e16a7d2c19 | 65 | return -1; |
botdream | 0:b1e16a7d2c19 | 66 | } |
botdream | 0:b1e16a7d2c19 | 67 | #ifdef internaldebug |
botdream | 0:b1e16a7d2c19 | 68 | printf("\r\nEthernet setup completed with success!\r\n"); |
botdream | 0:b1e16a7d2c19 | 69 | #endif |
botdream | 0:b1e16a7d2c19 | 70 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 71 | |
botdream | 0:b1e16a7d2c19 | 72 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 73 | // adding Handlers |
botdream | 0:b1e16a7d2c19 | 74 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 75 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
botdream | 0:b1e16a7d2c19 | 76 | |
botdream | 0:b1e16a7d2c19 | 77 | httpserver.addHandler<FSHandler>("/"); //Default handler |
botdream | 0:b1e16a7d2c19 | 78 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 79 | |
botdream | 0:b1e16a7d2c19 | 80 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 81 | // bind http server to port 80 (Listen) |
botdream | 0:b1e16a7d2c19 | 82 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 83 | httpserver.bind(80); |
botdream | 0:b1e16a7d2c19 | 84 | #ifdef internaldebug |
botdream | 0:b1e16a7d2c19 | 85 | printf("Listening on port 80!\r\n"); |
botdream | 0:b1e16a7d2c19 | 86 | #endif |
botdream | 0:b1e16a7d2c19 | 87 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 88 | |
botdream | 0:b1e16a7d2c19 | 89 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 90 | // ISR -> attach timer interrupt to update Net::Pool(); |
botdream | 0:b1e16a7d2c19 | 91 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 92 | netpool.attach(&netpoolupdate, 0.1); |
botdream | 0:b1e16a7d2c19 | 93 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 94 | |
botdream | 0:b1e16a7d2c19 | 95 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 96 | // |
botdream | 0:b1e16a7d2c19 | 97 | //-------------------------------------------------------- |
botdream | 0:b1e16a7d2c19 | 98 | // main loop |
botdream | 0:b1e16a7d2c19 | 99 | while(1) |
botdream | 0:b1e16a7d2c19 | 100 | { |
botdream | 0:b1e16a7d2c19 | 101 | myled = 1; |
botdream | 0:b1e16a7d2c19 | 102 | wait(0.5); |
botdream | 0:b1e16a7d2c19 | 103 | myled = 0; |
botdream | 0:b1e16a7d2c19 | 104 | wait(0.5); |
botdream | 0:b1e16a7d2c19 | 105 | } |
botdream | 0:b1e16a7d2c19 | 106 | } |
botdream | 0:b1e16a7d2c19 | 107 | //--------------------------------------------------------------------------------------------- |