serveur WEB sur carte SD pour TestBed , lit des tensions les met dans un fichier htm et l\'envoie . on peut agir sur les ES .
main.cpp@0:83cf97c10b63, 2011-09-05 (annotated)
- Committer:
- geiineuville
- Date:
- Mon Sep 05 14:36:10 2011 +0000
- Revision:
- 0:83cf97c10b63
webserver v 0 v 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geiineuville | 0:83cf97c10b63 | 1 | // original Written by IVA2K modified LRSD |
geiineuville | 0:83cf97c10b63 | 2 | // adapted to TestBed card for Electronik laden |
geiineuville | 0:83cf97c10b63 | 3 | // Example of HTTPServer |
geiineuville | 0:83cf97c10b63 | 4 | // * SNTP Client (Simple NTP) |
geiineuville | 0:83cf97c10b63 | 5 | // * Link status indication (LED4 or RJ45 socket LED on MBED-BoB2-mod) |
geiineuville | 0:83cf97c10b63 | 6 | // * Local file system |
geiineuville | 0:83cf97c10b63 | 7 | // * SD-based WebServer , sd card 128meg only **.htm 8.3 |
geiineuville | 0:83cf97c10b63 | 8 | // * Static HTM page |
geiineuville | 0:83cf97c10b63 | 9 | // * Dynamic HTM page a tester |
geiineuville | 0:83cf97c10b63 | 10 | // LRSD evolution from pub-iva2k-ethsntp project |
geiineuville | 0:83cf97c10b63 | 11 | // not so good but works. |
geiineuville | 0:83cf97c10b63 | 12 | // Instructions: |
geiineuville | 0:83cf97c10b63 | 13 | // 1 Plug ETH connector into your network (with a switch beetwen pc) |
geiineuville | 0:83cf97c10b63 | 14 | // 3 Plug MBED tu pc using USB cable |
geiineuville | 0:83cf97c10b63 | 15 | // 4 need to have Installed MBED serial driver (http://mbed.org/handbook/SerialPC) |
geiineuville | 0:83cf97c10b63 | 16 | // 5 Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct) |
geiineuville | 0:83cf97c10b63 | 17 | // 6 Open terminal on the mbed serial port |
geiineuville | 0:83cf97c10b63 | 18 | // 7 Push MBED reset button |
geiineuville | 0:83cf97c10b63 | 19 | // 8 Terminal will display info message with mac address, followed by IP address |
geiineuville | 0:83cf97c10b63 | 20 | // in the following items, i put 192.168.1.77 |
geiineuville | 0:83cf97c10b63 | 21 | // 9 Open browser and enter the following URL: http://192,168,1,77 !! ( sd card site) |
geiineuville | 0:83cf97c10b63 | 22 | // 13 Open browser and enter the following URL: http://192.168.1.77/static.htm |
geiineuville | 0:83cf97c10b63 | 23 | // 14 The browser will show static HTML page writen in defhtm.h |
geiineuville | 0:83cf97c10b63 | 24 | // 16 if you clic on Led ( in browser) led5 from testbed light on ou off |
geiineuville | 0:83cf97c10b63 | 25 | // led change , and the browser is redirected to static.htm |
geiineuville | 0:83cf97c10b63 | 26 | |
geiineuville | 0:83cf97c10b63 | 27 | int aip4,aip3,aip2,aip1; |
geiineuville | 0:83cf97c10b63 | 28 | int pon4,pon3,pon2,pon1; |
geiineuville | 0:83cf97c10b63 | 29 | int gDebug=1; |
geiineuville | 0:83cf97c10b63 | 30 | float ana = 1.0; |
geiineuville | 0:83cf97c10b63 | 31 | float gWait = 0.05; // Main loop wait timeout |
geiineuville | 0:83cf97c10b63 | 32 | |
geiineuville | 0:83cf97c10b63 | 33 | |
geiineuville | 0:83cf97c10b63 | 34 | #include "mbed.h" |
geiineuville | 0:83cf97c10b63 | 35 | DigitalOut led5(p6, "led5"); // avant led.h |
geiineuville | 0:83cf97c10b63 | 36 | #include "NewTextLCD.h" |
geiineuville | 0:83cf97c10b63 | 37 | #include "SDFileSystem.h" |
geiineuville | 0:83cf97c10b63 | 38 | #include "HTTPRPC.h" |
geiineuville | 0:83cf97c10b63 | 39 | #include "HTTPServer.h" |
geiineuville | 0:83cf97c10b63 | 40 | #include "HTTPFS.h" |
geiineuville | 0:83cf97c10b63 | 41 | #include "HTTPStaticPage.h" |
geiineuville | 0:83cf97c10b63 | 42 | #include "HTTPled.h" // |
geiineuville | 0:83cf97c10b63 | 43 | #include "HTTPDynamicPage.h" |
geiineuville | 0:83cf97c10b63 | 44 | #include "HTTPLinkStatus.h" |
geiineuville | 0:83cf97c10b63 | 45 | #include "SNTPClient.h" |
geiineuville | 0:83cf97c10b63 | 46 | #define CLS "\033[2J" |
geiineuville | 0:83cf97c10b63 | 47 | |
geiineuville | 0:83cf97c10b63 | 48 | |
geiineuville | 0:83cf97c10b63 | 49 | #define MAX_DYNAMIC_CONTENT_LEN 2048 // a exploiter mieux |
geiineuville | 0:83cf97c10b63 | 50 | char dynamic_content[MAX_DYNAMIC_CONTENT_LEN]; |
geiineuville | 0:83cf97c10b63 | 51 | const char content_fmt[] = |
geiineuville | 0:83cf97c10b63 | 52 | "<HTML>" |
geiineuville | 0:83cf97c10b63 | 53 | "<HEAD>" |
geiineuville | 0:83cf97c10b63 | 54 | "<title>Dynamic Page</title>" |
geiineuville | 0:83cf97c10b63 | 55 | "</HEAD>" |
geiineuville | 0:83cf97c10b63 | 56 | "<BODY>" // |
geiineuville | 0:83cf97c10b63 | 57 | "<H1>Hello l'univers </H1>" // "<H1>Hello World</H1>" |
geiineuville | 0:83cf97c10b63 | 58 | "<p>Page generated dynamically from code.</p>" |
geiineuville | 0:83cf97c10b63 | 59 | "<p>URL=%s</p>" |
geiineuville | 0:83cf97c10b63 | 60 | "<p>Header Fields=%s</p>" |
geiineuville | 0:83cf97c10b63 | 61 | "</BODY></HTML>" |
geiineuville | 0:83cf97c10b63 | 62 | ; |
geiineuville | 0:83cf97c10b63 | 63 | // E/S sur la carte mbed 005.1 |
geiineuville | 0:83cf97c10b63 | 64 | DigitalOut led1(LED1, "led1"); // mbed |
geiineuville | 0:83cf97c10b63 | 65 | DigitalOut led2(LED2, "led2"); |
geiineuville | 0:83cf97c10b63 | 66 | DigitalOut led3(LED3, "led3"); |
geiineuville | 0:83cf97c10b63 | 67 | DigitalOut led4(LED4, "led4"); |
geiineuville | 0:83cf97c10b63 | 68 | // E/S hors carte mbed sur la carte TestBed |
geiineuville | 0:83cf97c10b63 | 69 | DigitalIn sw1(p8, "sw1"); // testbed bt1 ( bt2 on sd card !!) |
geiineuville | 0:83cf97c10b63 | 70 | //DigitalOut led5(p6, "led5"); |
geiineuville | 0:83cf97c10b63 | 71 | DigitalOut led6(p5, "led6"); |
geiineuville | 0:83cf97c10b63 | 72 | AnalogIn Potm(p15); //potentiometre carte testbed |
geiineuville | 0:83cf97c10b63 | 73 | TextLCD lcd(p26, p25, p24, p23, p22, p20, p19, TextLCD::LCD20x4); // testbed LCD |
geiineuville | 0:83cf97c10b63 | 74 | LocalFileSystem local("local"); |
geiineuville | 0:83cf97c10b63 | 75 | SDFileSystem sd(p11, p12, p13, p14, "sd"); //test bed sd card |
geiineuville | 0:83cf97c10b63 | 76 | |
geiineuville | 0:83cf97c10b63 | 77 | #include "myrpc.h" // ici on ecrit les pages web dans des tableaux |
geiineuville | 0:83cf97c10b63 | 78 | myrpc le(p6, "le"); // fonction apelable par l'adresse |
geiineuville | 0:83cf97c10b63 | 79 | |
geiineuville | 0:83cf97c10b63 | 80 | extern Ethernet eth; // eth is defined elsewhere, avoid compiler error. |
geiineuville | 0:83cf97c10b63 | 81 | Serial pc(USBTX, USBRX); // usb sur Mbed l'autre sur testbed ?? |
geiineuville | 0:83cf97c10b63 | 82 | |
geiineuville | 0:83cf97c10b63 | 83 | |
geiineuville | 0:83cf97c10b63 | 84 | HTTPStatus myDynamicPage(HTTPConnection *con, HTTPDynamicPageData *pd) { |
geiineuville | 0:83cf97c10b63 | 85 | #if 0 |
geiineuville | 0:83cf97c10b63 | 86 | // Static example. With this, we don't really need HTTPStaticPage |
geiineuville | 0:83cf97c10b63 | 87 | pd->size = 0; // let it measure our page |
geiineuville | 0:83cf97c10b63 | 88 | pd->page = (char*)content; // Nothing dynamic about that yet, but we can now get loose here. |
geiineuville | 0:83cf97c10b63 | 89 | pd->page_free = NULL; // No mem free() needed |
geiineuville | 0:83cf97c10b63 | 90 | #elif 0 |
geiineuville | 0:83cf97c10b63 | 91 | // Dynamic example, static buffer |
geiineuville | 0:83cf97c10b63 | 92 | pd->size = sprintf(dynamic_content, content_fmt, con->getURL(), con->getHeaderFields()); |
geiineuville | 0:83cf97c10b63 | 93 | pd->page = (char*)dynamic_content; |
geiineuville | 0:83cf97c10b63 | 94 | pd->page_free = NULL; // No mem free() needed |
geiineuville | 0:83cf97c10b63 | 95 | if(pd->size > sizeof(dynamic_content)) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__); // Buffer overrun |
geiineuville | 0:83cf97c10b63 | 96 | #else |
geiineuville | 0:83cf97c10b63 | 97 | // Dynamic example, dynamic buffer |
geiineuville | 0:83cf97c10b63 | 98 | int size = sizeof(content_fmt) + 512; // Just guess how much the data will expand |
geiineuville | 0:83cf97c10b63 | 99 | char *buf = (char *)malloc(size); |
geiineuville | 0:83cf97c10b63 | 100 | if (buf) { |
geiineuville | 0:83cf97c10b63 | 101 | pd->size = sprintf(buf, content_fmt, con->getURL(), con->getHeaderFields()); |
geiineuville | 0:83cf97c10b63 | 102 | pd->page = buf; |
geiineuville | 0:83cf97c10b63 | 103 | pd->page_free = &free; // Use free() when done |
geiineuville | 0:83cf97c10b63 | 104 | if(pd->size > size) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__); // Buffer overrun |
geiineuville | 0:83cf97c10b63 | 105 | #endif |
geiineuville | 0:83cf97c10b63 | 106 | } |
geiineuville | 0:83cf97c10b63 | 107 | return HTTP_OK; |
geiineuville | 0:83cf97c10b63 | 108 | } |
geiineuville | 0:83cf97c10b63 | 109 | //*******************************************MAIN*************************** |
geiineuville | 0:83cf97c10b63 | 110 | //**************************************************************************** |
geiineuville | 0:83cf97c10b63 | 111 | int main(void) |
geiineuville | 0:83cf97c10b63 | 112 | { |
geiineuville | 0:83cf97c10b63 | 113 | lcd.cls(); |
geiineuville | 0:83cf97c10b63 | 114 | pc.baud(115200); |
geiineuville | 0:83cf97c10b63 | 115 | lcd.printf("LRSD Webserverv2 \n"); |
geiineuville | 0:83cf97c10b63 | 116 | printf("LRSD debout !!\n"); |
geiineuville | 0:83cf97c10b63 | 117 | // initialisation de static.htm en ram local |
geiineuville | 0:83cf97c10b63 | 118 | #include "defhtm.h" |
geiineuville | 0:83cf97c10b63 | 119 | |
geiineuville | 0:83cf97c10b63 | 120 | |
geiineuville | 0:83cf97c10b63 | 121 | char mac[6]; |
geiineuville | 0:83cf97c10b63 | 122 | bool use_sd = false; |
geiineuville | 0:83cf97c10b63 | 123 | char res[10]= "1234000"; |
geiineuville | 0:83cf97c10b63 | 124 | int pt = 0; // pointeur dans tab 128 c'est sur le * |
geiineuville | 0:83cf97c10b63 | 125 | led1=1; led2=1; led3=1; led4=1; led5 = 0; //led off |
geiineuville | 0:83cf97c10b63 | 126 | |
geiineuville | 0:83cf97c10b63 | 127 | // Start RTC |
geiineuville | 0:83cf97c10b63 | 128 | time_t seconds = time(NULL); |
geiineuville | 0:83cf97c10b63 | 129 | if (seconds == (unsigned)-1 || seconds == 0) { |
geiineuville | 0:83cf97c10b63 | 130 | seconds = 1256729737; // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
geiineuville | 0:83cf97c10b63 | 131 | set_time(seconds); |
geiineuville | 0:83cf97c10b63 | 132 | printf("RTC initialized, start time %d seconds\r\n", seconds); |
geiineuville | 0:83cf97c10b63 | 133 | } |
geiineuville | 0:83cf97c10b63 | 134 | |
geiineuville | 0:83cf97c10b63 | 135 | led6 = sw1; |
geiineuville | 0:83cf97c10b63 | 136 | if ( sw1 == 0 ) |
geiineuville | 0:83cf97c10b63 | 137 | { aip4 = 172; aip3 = 20 ; aip2 = 140 ; aip1 = 49; //BT1 on > adresse de mon PC |
geiineuville | 0:83cf97c10b63 | 138 | pon4= 172; pon3 = 20 ; pon2 = 0 ; pon1 = 254;} |
geiineuville | 0:83cf97c10b63 | 139 | else |
geiineuville | 0:83cf97c10b63 | 140 | { aip4 = 192; aip3 = 168 ; aip2 = 1 ; aip1 = 77; // si on ne touche pas BT1 adresse box |
geiineuville | 0:83cf97c10b63 | 141 | pon4 = 192; pon3 = 168 ; pon2 = 1 ; pon1 = 0; } // si on ne touche pas BT1 adresse box |
geiineuville | 0:83cf97c10b63 | 142 | lcd.locate(0,1); |
geiineuville | 0:83cf97c10b63 | 143 | lcd.printf("IP:%hhu.%hhu.%hhu.%hhu\n", |
geiineuville | 0:83cf97c10b63 | 144 | aip4, aip3, aip2, aip1); |
geiineuville | 0:83cf97c10b63 | 145 | HTTPServer http("mbed", // static IP address and domain name. |
geiineuville | 0:83cf97c10b63 | 146 | IPv4(aip4,aip3,aip2,aip1), // IPv4 is a helper function which allows to rtype ipaddresses direct |
geiineuville | 0:83cf97c10b63 | 147 | IPv4(255,255,0,0), |
geiineuville | 0:83cf97c10b63 | 148 | IPv4(pon4,pon3,pon2,pon1), |
geiineuville | 0:83cf97c10b63 | 149 | IPv4(pon4,pon3,pon2,pon1), |
geiineuville | 0:83cf97c10b63 | 150 | 80); // port 80 souvent |
geiineuville | 0:83cf97c10b63 | 151 | char *hostname = "mbed"; |
geiineuville | 0:83cf97c10b63 | 152 | // HTTPServer http(hostname); // Use DHCP |
geiineuville | 0:83cf97c10b63 | 153 | 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. |
geiineuville | 0:83cf97c10b63 | 154 | |
geiineuville | 0:83cf97c10b63 | 155 | eth.address(mac); |
geiineuville | 0:83cf97c10b63 | 156 | pc.printf(CLS "\r\n\r\nHTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n%s", |
geiineuville | 0:83cf97c10b63 | 157 | hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], |
geiineuville | 0:83cf97c10b63 | 158 | gDebug?"Debug is ON\r\n":"" |
geiineuville | 0:83cf97c10b63 | 159 | ); |
geiineuville | 0:83cf97c10b63 | 160 | |
geiineuville | 0:83cf97c10b63 | 161 | Base::add_rpc_class<myrpc>(); |
geiineuville | 0:83cf97c10b63 | 162 | led1=0; |
geiineuville | 0:83cf97c10b63 | 163 | // Check if we can use SD card for the web server |
geiineuville | 0:83cf97c10b63 | 164 | FILE *fp = fopen("/sd/index.htm", "r"); |
geiineuville | 0:83cf97c10b63 | 165 | if (fp == NULL) { |
geiineuville | 0:83cf97c10b63 | 166 | if (gDebug) printf("DEBUG: No SD card found or no index.htm file - using LocalFilesystem for WebServer.\r\n"); |
geiineuville | 0:83cf97c10b63 | 167 | } else { |
geiineuville | 0:83cf97c10b63 | 168 | use_sd = true; |
geiineuville | 0:83cf97c10b63 | 169 | fclose(fp); |
geiineuville | 0:83cf97c10b63 | 170 | if (gDebug) printf("DEBUG: Found SD card with index.htm file - using SD for WebServer.\r\n"); |
geiineuville | 0:83cf97c10b63 | 171 | } |
geiineuville | 0:83cf97c10b63 | 172 | |
geiineuville | 0:83cf97c10b63 | 173 | if (0 != SNTPReadIniFile("/sd/sntp.ini") ) |
geiineuville | 0:83cf97c10b63 | 174 | SNTPReadIniFile("/local/sntp.ini"); |
geiineuville | 0:83cf97c10b63 | 175 | SNTPWriteIniFile(stdout); |
geiineuville | 0:83cf97c10b63 | 176 | |
geiineuville | 0:83cf97c10b63 | 177 | http.addHandler(new HTTPLinkStatus("/", |
geiineuville | 0:83cf97c10b63 | 178 | #if MBED_BOB2 |
geiineuville | 0:83cf97c10b63 | 179 | p25, p26, // MBED-BoB2-mods |
geiineuville | 0:83cf97c10b63 | 180 | #else |
geiineuville | 0:83cf97c10b63 | 181 | LED3, LED4, |
geiineuville | 0:83cf97c10b63 | 182 | #endif |
geiineuville | 0:83cf97c10b63 | 183 | 0.1, // ici ??? |
geiineuville | 0:83cf97c10b63 | 184 | /*do_urlfile*/ true, /*do_link_printf*/ true, /*do_log_printf*/ false, |
geiineuville | 0:83cf97c10b63 | 185 | /*log_file*/ ( (gDebug>1) ? (use_sd ? "/sd/httpd.log" : "/local/httpd.log") : NULL) |
geiineuville | 0:83cf97c10b63 | 186 | )); // Should be the first handler to get a preview of all requests |
geiineuville | 0:83cf97c10b63 | 187 | // fin du ici ?? |
geiineuville | 0:83cf97c10b63 | 188 | |
geiineuville | 0:83cf97c10b63 | 189 | http.addHandler(new HTTPRPC());// on y trouve d'autres choses a faire |
geiineuville | 0:83cf97c10b63 | 190 | led2=0; // |
geiineuville | 0:83cf97c10b63 | 191 | |
geiineuville | 0:83cf97c10b63 | 192 | // Static/Dynamic pages must be installed before FileSystem on / |
geiineuville | 0:83cf97c10b63 | 193 | http.addHandler(new HTTPStaticPage("/static.htm", tab, strlen(tab))); // ma page statique en ram |
geiineuville | 0:83cf97c10b63 | 194 | // http.addHandler(new HTTPDynamicPage("/dynamic.htm", &myDynamicPage)); avant |
geiineuville | 0:83cf97c10b63 | 195 | http.addHandler(new HTTPled("/led.htm", led , strlen(tab))); /// test |
geiineuville | 0:83cf97c10b63 | 196 | http.addHandler(new HTTPFileSystemHandler("/", use_sd ? "/sd/" : "/local/")); |
geiineuville | 0:83cf97c10b63 | 197 | led3=0; |
geiineuville | 0:83cf97c10b63 | 198 | |
geiineuville | 0:83cf97c10b63 | 199 | // FIXME: BUG If eth is not plugged, http.bind() hangs! |
geiineuville | 0:83cf97c10b63 | 200 | http.bind(); |
geiineuville | 0:83cf97c10b63 | 201 | SNTPClientInit(); |
geiineuville | 0:83cf97c10b63 | 202 | led4 = 0; |
geiineuville | 0:83cf97c10b63 | 203 | // pc.printf("\r"); // Add linefeed for stupid Hyperterminal |
geiineuville | 0:83cf97c10b63 | 204 | |
geiineuville | 0:83cf97c10b63 | 205 | while(1) { //BOUCLE POLL |
geiineuville | 0:83cf97c10b63 | 206 | http.poll(); // Have to call this method at least every 250ms to let the http server run. |
geiineuville | 0:83cf97c10b63 | 207 | wait(gWait); |
geiineuville | 0:83cf97c10b63 | 208 | |
geiineuville | 0:83cf97c10b63 | 209 | ana = Potm.read(); // travail en temps reel like ( gWait) |
geiineuville | 0:83cf97c10b63 | 210 | lcd.locate(0,2); //ligne 2 lcd |
geiineuville | 0:83cf97c10b63 | 211 | ana = 3.2*ana; // mise au niveau 3v2 |
geiineuville | 0:83cf97c10b63 | 212 | lcd.printf("Value: %1.2f", ana); |
geiineuville | 0:83cf97c10b63 | 213 | sprintf(res, "%f", ana); // met ana en chaine dans res[] |
geiineuville | 0:83cf97c10b63 | 214 | pt = 127 ; //point dans l'affichage a faire |
geiineuville | 0:83cf97c10b63 | 215 | tab[pt+0] = res[0]; // met 5 char de la chaine a dans static.htm |
geiineuville | 0:83cf97c10b63 | 216 | tab[pt+1] = res[1]; |
geiineuville | 0:83cf97c10b63 | 217 | tab[pt+2] = res[2]; |
geiineuville | 0:83cf97c10b63 | 218 | tab[pt+3] = res[3]; |
geiineuville | 0:83cf97c10b63 | 219 | tab[pt+4] = res[4]; |
geiineuville | 0:83cf97c10b63 | 220 | |
geiineuville | 0:83cf97c10b63 | 221 | |
geiineuville | 0:83cf97c10b63 | 222 | } |
geiineuville | 0:83cf97c10b63 | 223 | } |
geiineuville | 0:83cf97c10b63 | 224 | |
geiineuville | 0:83cf97c10b63 | 225 | //END |