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 NTPClient_NetServices mbed HTTPServer
HTTPServerExample.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "HTTPServer.h" 00004 #include "SDHCFileSystem.h" 00005 #include "NTPClient.h" 00006 00007 DigitalOut led1(p29, "led1"); 00008 DigitalOut led2(LED2, "led2"); 00009 DigitalOut led3(LED3, "led3"); 00010 DigitalOut led4(LED4, "led4"); 00011 00012 // LocalFileSystem fs("webfs"); 00013 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs 00014 00015 EthernetNetIf eth 00016 ( 00017 IpAddr(192,168,1,150), //IP Address 00018 IpAddr(255,255,255,0), //Network Mask 00019 IpAddr(192,168,1,1), //Gateway 00020 IpAddr(192,168,1,1) //DNS 00021 ); 00022 00023 HTTPServer svr; 00024 NTPClient ntp; 00025 00026 int main() { 00027 Base::add_rpc_class<DigitalOut>(); 00028 00029 FILE *fp = fopen("/sd/myfile.txt", "w"); // to initialize SD 00030 fprintf(fp, "\n\rHello World!\n\r"); 00031 fclose(fp); 00032 00033 printf("Setting up...\n"); 00034 EthernetErr ethErr = eth.setup(); 00035 if(ethErr) 00036 { 00037 printf("Error %d in setup.\n", ethErr); 00038 return -1; 00039 } 00040 printf("Setup OK\n"); 00041 00042 time_t seconds = time(NULL); 00043 printf("Current time is (UTC): %s\n", ctime(&seconds)); 00044 00045 Host server(IpAddr(), 123, "ntp.ien.it"); 00046 ntp.setTime(server); 00047 00048 seconds = time(NULL); 00049 printf("\nTime is now (UTC): %s\n", ctime(&seconds)); 00050 00051 FSHandler::mount("/sd", "/files"); //Mount /webfs path on /files web path 00052 FSHandler::mount("/sd", "/"); //Mount /webfs path on web root path 00053 00054 svr.addHandler<SimpleHandler>("/hello"); 00055 svr.addHandler<RPCHandler>("/rpc"); 00056 svr.addHandler<FSHandler>("/files"); 00057 svr.addHandler<FSHandler>("/"); //Default handler 00058 //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm 00059 00060 svr.bind(8080); 00061 00062 printf("Listening...\n"); 00063 00064 Timer tm; 00065 tm.start(); 00066 //Listen indefinitely 00067 while(true) 00068 { 00069 Net::poll(); 00070 if(tm.read()>1) 00071 { 00072 led1=!led1; //Show that we are alive 00073 tm.start(); 00074 char buffer[32]; 00075 seconds = time(NULL)+3600; 00076 strftime(buffer, 32, "%a %d %b %Y - %H:%M:%S \n", localtime(&seconds)); 00077 printf(" %s", buffer); 00078 } 00079 } 00080 00081 return 0; 00082 00083 }
Generated on Thu Jul 28 2022 01:19:24 by
1.7.2