Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf mbed HTTPServer
main.cpp
00001 //--------------------------------------------------------------------------------------------- 00002 #include "mbed.h" 00003 #include "EthernetNetIf.h" 00004 #include "HTTPServer.h" 00005 //--------------------------------------------------------------------------------------------- 00006 DigitalOut myled(LED1); 00007 Serial pc(USBTX, USBRX); // tx, rx 00008 //--------------------------------------------------------------------------------------------- 00009 #define internaldebug // send debug messages to USB Serial port (9600,1,N) 00010 //#define dhcpenable // auto-setup IP Address from DHCP router 00011 //--------------------------------------------------------------------------------------------- 00012 // Timer Interrupt - NetPool 00013 //--------------------------------------------------------------------------------------------- 00014 Ticker netpool; 00015 //--------------------------------------------------------------------------------------------- 00016 // Ethernet Object Setup 00017 //--------------------------------------------------------------------------------------------- 00018 #ifdef dhcpenable 00019 EthernetNetIf eth; 00020 #else 00021 EthernetNetIf eth( 00022 IpAddr(192,168,1,100), //IP Address 00023 IpAddr(255,255,255,0), //Network Mask 00024 IpAddr(192,168,1,254), //Gateway 00025 IpAddr(192,168,1,254) //DNS 00026 ); 00027 #endif 00028 //--------------------------------------------------------------------------------------------- 00029 // HTTP Server 00030 //--------------------------------------------------------------------------------------------- 00031 HTTPServer httpserver; 00032 LocalFileSystem fs("webfs"); 00033 //--------------------------------------------------------------------------------------------- 00034 00035 //--------------------------------------------------------------------------------------------- 00036 // ISR -> Pool Ethernet - will be triggered by netpool ticker 00037 //--------------------------------------------------------------------------------------------- 00038 void netpoolupdate() 00039 { 00040 Net::poll(); 00041 } 00042 //--------------------------------------------------------------------------------------------- 00043 00044 //--------------------------------------------------------------------------------------------- 00045 // MAIN 00046 //--------------------------------------------------------------------------------------------- 00047 int main() 00048 { 00049 // Set Serial Port Transfer Rate 00050 pc.baud(115200); 00051 00052 //-------------------------------------------------------- 00053 // Setting Ethernet 00054 //-------------------------------------------------------- 00055 #ifdef internaldebug 00056 printf("\r\nSetting up Ethernet interface!\r\n"); 00057 #endif 00058 // Create return object for error check 00059 EthernetErr ethErr = eth.setup(); 00060 if(ethErr) 00061 { 00062 #ifdef internaldebug 00063 printf("\r\nError %d in Ethernet setup.\r\n", ethErr); 00064 #endif 00065 return -1; 00066 } 00067 #ifdef internaldebug 00068 printf("\r\nEthernet setup completed with success!\r\n"); 00069 #endif 00070 //-------------------------------------------------------- 00071 00072 //-------------------------------------------------------- 00073 // adding Handlers 00074 //-------------------------------------------------------- 00075 FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path 00076 00077 httpserver.addHandler<FSHandler>("/"); //Default handler 00078 //-------------------------------------------------------- 00079 00080 //-------------------------------------------------------- 00081 // bind http server to port 80 (Listen) 00082 //-------------------------------------------------------- 00083 httpserver.bind(80); 00084 #ifdef internaldebug 00085 printf("Listening on port 80!\r\n"); 00086 #endif 00087 //-------------------------------------------------------- 00088 00089 //-------------------------------------------------------- 00090 // ISR -> attach timer interrupt to update Net::Pool(); 00091 //-------------------------------------------------------- 00092 netpool.attach(&netpoolupdate, 0.1); 00093 //-------------------------------------------------------- 00094 00095 //-------------------------------------------------------- 00096 // 00097 //-------------------------------------------------------- 00098 // main loop 00099 while(1) 00100 { 00101 myled = 1; 00102 wait(0.5); 00103 myled = 0; 00104 wait(0.5); 00105 } 00106 } 00107 //---------------------------------------------------------------------------------------------
Generated on Wed Jul 13 2022 20:37:16 by
1.7.2