webserver basé sur pub-iva2k project , fix IP adress , htm page in sdcard; for TESTBED card

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // original Written by IVA2K  
00002 //
00003 // Example of HTTPServer with additional features:
00004 // * SNTP Client (Simple NTP)
00005 // * Link status indication (LED4 or RJ45 socket LED on MBED-BoB2-mod)
00006 // * Local file system (create index.htm page on MBED!)
00007 // * SD-based WebServer
00008 // * ////RPC-able class (myrpc, allows remote function call that blinks LED1 N times)
00009 // * Static HTML page
00010 // * Dynamic HTML page
00011 // modification fron LRSD  from the pub-iva2k-ethsntp  project
00012 //  i am not a specialist , stil bugs , but that works 
00013 // Instructions:
00014 // 2  Plug ETH connector into your network(with a switch for direct  pc, needs DHCP to get IP address and Internet connection)
00015 // 3  Plug MBED using USB cable
00016 // 4  need to have Installed MBED serial driver (http://mbed.org/handbook/SerialPC)
00017 // 5  Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct)
00018 // 6  Open terminal on the mbed serial port
00019 // 7  Push MBED reset button
00020 // 8  Terminal will display info message with mac address, followed by IP address 
00021 //    in the following items, i put  192.168.1.77  line 144 : IPv4(192,168,1,77),
00022 // 9  Open browser and enter the following URL:
00023 //    http://192,168,1,77 /
00024 //        !!
00025 // 13 Open browser and enter the following URL:
00026 //    http://192.168.1.77/static.htm
00027 // 14 The browser will show static HTML page
00028 // 15 Open browser and enter the following URL:
00029 //    http://192,168,1,77/dynamic.htm
00030 // 16 The browser will show dynamic HTML page
00031 // 17 Create a simple index.htm page on the MBED
00032 // 18 Open browser and enter the following URL:
00033 //    http://192,168,1,77
00034 // 19 The browser will show index HTM page  not HTML! ( 3 char extension , no directorie)
00035 // 20 Create a simple index.htm page on a micro SD card, plug the card into MBED-BoB2
00036 // 22 The browser will show index HTM page from SD card
00037 // 23 Optionally, create file "sntp.ini" on MBED or SD card. Copy-paste SNTP configuration from the terminal into this file and modify to your needs.
00038 //
00039 
00040 #include "mbed.h"
00041 #include "SDFileSystem.h"
00042 #include "HTTPServer.h"
00043 #include "HTTPRPC.h"
00044 #include "HTTPFS.h"
00045 #include "HTTPStaticPage.h"
00046 #include "HTTPDynamicPage.h"
00047 #include "HTTPLinkStatus.h"
00048 #include "SNTPClient.h"
00049 
00050 #define CLS "\033[2J"
00051 
00052 const char content[] = 
00053 "<HTML>"
00054 "<HEAD>"
00055 "<title>Static Page</title>"
00056 "</HEAD>"
00057 "<BODY>"
00058 "<H1>Hello World</H1>"
00059 "<p>Page generated statically from code.</p>"
00060 "</BODY></HTML>"
00061 ;
00062 
00063 #define MAX_DYNAMIC_CONTENT_LEN     2048
00064 char dynamic_content[MAX_DYNAMIC_CONTENT_LEN];
00065 
00066 const char content_fmt[] = 
00067 "<HTML>"
00068 "<HEAD>"
00069 "<title>Dynamic Page</title>"
00070 "</HEAD>"
00071 "<BODY>"
00072 "<H1>Hello World</H1>"
00073 "<p>Page generated dynamically from code.</p>"
00074 "<p>URL=%s</p>"
00075 "<p>Header Fields=%s</p>"
00076 "</BODY></HTML>"
00077 ;
00078 
00079 DigitalOut led1(LED1, "led1");
00080 DigitalOut led2(LED2, "led2");
00081 DigitalOut led3(LED3, "led3");
00082 DigitalOut led4(LED4, "led4");
00083 DigitalIn sw1(p9, "sw1");
00084 DigitalIn sw2(p10, "sw2");
00085 LocalFileSystem local("local");
00086 SDFileSystem sd(p11, p12, p13, p14, "sd");        // MBED-BoB2
00087 
00088 #include "myrpc.h"
00089 myrpc myrpc1(LED1, "myrpc1");
00090 
00091 extern Ethernet eth;        // eth is defined elsewhere, avoid compiler error.
00092 Serial pc(USBTX, USBRX);
00093 int gDebug=1;
00094 float gWait = 0.005;        // Main loop wait timeout
00095 
00096 HTTPStatus myDynamicPage(HTTPConnection *con, HTTPDynamicPageData *pd) {
00097 #if 0
00098     // Static example. With this, we don't really need HTTPStaticPage
00099     pd->size = 0;    // let it measure our page
00100     pd->page = (char*)content;    // Nothing dynamic about that yet, but we can now get loose here.
00101     pd->page_free = NULL;    // No mem free() needed
00102 #elif 0
00103     // Dynamic example, static buffer
00104     pd->size = sprintf(dynamic_content, content_fmt, con->getURL(), con->getHeaderFields());
00105     pd->page = (char*)dynamic_content;
00106     pd->page_free = NULL;    // No mem free() needed
00107 if(pd->size > sizeof(dynamic_content)) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__);    // Buffer overrun
00108 #else
00109     // Dynamic example, dynamic buffer
00110     int size = sizeof(content_fmt) + 512;    // Just guess how much the data will expand
00111     char *buf = (char *)malloc(size);
00112     if (buf) {
00113         pd->size = sprintf(buf, content_fmt, con->getURL(), con->getHeaderFields());
00114         pd->page = buf;
00115         pd->page_free = &free;    // Use free() when done
00116 if(pd->size > size) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__);    // Buffer overrun
00117 #endif
00118     }
00119     return HTTP_OK;
00120 }
00121 
00122 int main(void) {
00123     pc.baud(115200);
00124     printf("Aum Sri Ganeshay Namh !!\n");
00125     
00126     char mac[6];
00127     int sw1_old=0, sw2_old=0;
00128     bool use_sd = false;
00129 
00130     led1=1;
00131     led2=1;
00132     led3=1;
00133     led4=1;
00134 
00135     // Start RTC
00136     time_t seconds = time(NULL);
00137     if (seconds == (unsigned)-1 || seconds == 0) {
00138       seconds = 1256729737;     // Set RTC time to Wed, 28 Oct 2009 11:35:37
00139       set_time(seconds);
00140       printf("RTC initialized, start time %d seconds\r\n", seconds);
00141     }
00142      HTTPServer http("mbed",              // Brings up the device with static IP address and domain name.
00143                  IPv4(192,168,1,77),  // IPv4 is a helper function which allows to rtype ipaddresses direct
00144                  IPv4(255,255,0,0), // as numbers in C++.
00145                 IPv4(192,168,1,0),   // the device address is set to 192.168.0.44, netmask 255.255.255.0
00146                  IPv4(192,168,1,0),    // default gateway is 192.168.0.1 and dns to 192.168.0.1 as well.
00147                    80);               // And port is on 8080. Default port is 80.  
00148     char *hostname = "mbed";
00149    // HTTPServer http(hostname);    // Use DHCP
00150     http.timeout(10000);    // Sets the timout for a HTTP request.  The timout is the time which is allowed to spent between two incomming TCP packets.  If the time is passed the connection will be closed.
00151    
00152     eth.address(mac);
00153     pc.printf(CLS "\r\n\r\nHTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n%s",
00154         hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
00155         gDebug?"Debug is ON\r\n":""
00156     );
00157 
00158     Base::add_rpc_class<AnalogIn>();
00159     Base::add_rpc_class<AnalogOut>();
00160     Base::add_rpc_class<DigitalIn>();
00161     Base::add_rpc_class<DigitalOut>();
00162     Base::add_rpc_class<PwmOut>();
00163     Base::add_rpc_class<Timer>();
00164     Base::add_rpc_class<SPI>();
00165     Base::add_rpc_class<BusOut>();
00166     Base::add_rpc_class<BusIn>();
00167     Base::add_rpc_class<myrpc>();
00168     led1=0;
00169 
00170     // Check if we can use SD card for the web server
00171     FILE *fp = fopen("/sd/index.htm", "r");
00172     if (fp == NULL) {
00173         if (gDebug) printf("DEBUG: No SD card found or no index.htm file - using LocalFilesystem for WebServer.\r\n");
00174     } else {
00175         use_sd = true;
00176         fclose(fp);
00177         if (gDebug) printf("DEBUG: Found SD card with index.htm file - using SD for WebServer.\r\n");
00178     }
00179 
00180     if (0 != SNTPReadIniFile("/sd/sntp.ini") )
00181         SNTPReadIniFile("/local/sntp.ini");
00182     SNTPWriteIniFile(stdout);
00183 
00184 
00185     http.addHandler(new HTTPLinkStatus("/", 
00186 #if MBED_BOB2
00187       p25, p26,            // MBED-BoB2-mods
00188 #else
00189       LED3, LED4,
00190 #endif
00191       0.1,
00192       /*do_urlfile*/ true, /*do_link_printf*/ true, /*do_log_printf*/ false, 
00193       /*log_file*/ ( (gDebug>1) ? (use_sd ? "/sd/httpd.log" : "/local/httpd.log") : NULL)
00194     ));    // Should be the first handler to get a preview of all requests
00195     http.addHandler(new HTTPRPC());
00196     led2=0;
00197 
00198     // Static/Dynamic pages must be installed before FileSystem on /
00199     http.addHandler(new HTTPStaticPage("/static.htm", content, strlen(content)));
00200     http.addHandler(new HTTPDynamicPage("/dynamic.htm", &myDynamicPage));
00201     http.addHandler(new HTTPFileSystemHandler("/", use_sd ? "/sd/" : "/local/"));
00202     led3=0;
00203 
00204 // FIXME: BUG If eth is not plugged, http.bind() hangs!
00205     http.bind();
00206 
00207     SNTPClientInit();
00208     led4 = 0;
00209 
00210     pc.printf("\r");    // Add linefeed for stupid Hyperterminal
00211     while(1) {
00212         http.poll();    // Have to call this method at least every 250ms to let the http server run.
00213         if (sw1 & !sw1_old) {
00214             printf("SW1\r\n");
00215         }
00216         if (sw2 && !sw2_old) {
00217             printf(CLS "SW2\r\n");
00218         }
00219         sw1_old = sw1;
00220         sw2_old = sw2;
00221         wait(gWait);
00222     }
00223 }
00224 
00225 //END