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 .

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // original Written by IVA2K  modified LRSD  
00002 //  adapted to TestBed  card for  Electronik laden
00003 // Example of HTTPServer 
00004 // * SNTP Client (Simple NTP)
00005 // * Link status indication (LED4 or RJ45 socket LED on MBED-BoB2-mod)
00006 // * Local file system 
00007 // * SD-based WebServer ,  sd card 128meg only **.htm  8.3
00008 // * Static HTM page
00009 // * Dynamic HTM page  a tester 
00010 // LRSD  evolution from    pub-iva2k-ethsntp  project
00011 //  not so good but works. 
00012 // Instructions:
00013 // 1  Plug ETH connector into your network (with a switch beetwen   pc)
00014 // 3  Plug MBED tu pc  using USB cable
00015 // 4  need to have Installed MBED serial driver (http://mbed.org/handbook/SerialPC)
00016 // 5     Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct)
00017 // 6  Open terminal on the mbed serial port
00018 // 7  Push MBED reset button
00019 // 8  Terminal will display info message with mac address, followed by IP address 
00020 //    in the following items, i put  192.168.1.77 
00021 // 9  Open browser and enter the following URL:    http://192,168,1,77   !! ( sd card site)
00022 // 13 Open browser and enter the following URL:    http://192.168.1.77/static.htm
00023 // 14 The browser will show static HTML page  writen in defhtm.h   
00024 // 16   if you clic on Led ( in browser) led5 from testbed light on ou off
00025 //     led change , and the browser is redirected to static.htm
00026 
00027 int aip4,aip3,aip2,aip1;  
00028 int pon4,pon3,pon2,pon1;  
00029 int gDebug=1;
00030 float ana = 1.0;
00031 float gWait = 0.05;        // Main loop wait timeout
00032 
00033 
00034 #include "mbed.h"
00035 DigitalOut led5(p6, "led5");  // avant led.h
00036 #include "NewTextLCD.h"
00037 #include "SDFileSystem.h"
00038 #include "HTTPRPC.h"
00039 #include "HTTPServer.h"
00040 #include "HTTPFS.h"
00041 #include "HTTPStaticPage.h"
00042 #include "HTTPled.h"   // 
00043 #include "HTTPDynamicPage.h"
00044 #include "HTTPLinkStatus.h"
00045 #include "SNTPClient.h"
00046 #define CLS "\033[2J"
00047 
00048 
00049 #define MAX_DYNAMIC_CONTENT_LEN     2048         // a exploiter mieux
00050 char dynamic_content[MAX_DYNAMIC_CONTENT_LEN];
00051    const char content_fmt[] = 
00052     "<HTML>"
00053     "<HEAD>"
00054     "<title>Dynamic Page</title>"
00055     "</HEAD>"
00056     "<BODY>"                                 //
00057     "<H1>Hello l'univers </H1>"              // "<H1>Hello World</H1>"               
00058     "<p>Page generated dynamically from code.</p>"
00059     "<p>URL=%s</p>"
00060     "<p>Header Fields=%s</p>"
00061     "</BODY></HTML>"
00062     ;
00063     // E/S sur  la carte mbed 005.1
00064 DigitalOut led1(LED1, "led1");  // mbed
00065 DigitalOut led2(LED2, "led2");
00066 DigitalOut led3(LED3, "led3");
00067 DigitalOut led4(LED4, "led4");
00068     // E/S  hors carte mbed sur la carte TestBed
00069 DigitalIn sw1(p8, "sw1");       // testbed bt1  ( bt2 on  sd card !!)
00070 //DigitalOut led5(p6, "led5");
00071 DigitalOut led6(p5, "led6");
00072 AnalogIn Potm(p15);             //potentiometre carte testbed 
00073 TextLCD lcd(p26, p25, p24, p23, p22, p20, p19, TextLCD::LCD20x4); // testbed LCD
00074 LocalFileSystem local("local");
00075 SDFileSystem sd(p11, p12, p13, p14, "sd");        //test bed sd card 
00076 
00077 #include "myrpc.h"  // ici on ecrit les pages web dans des tableaux 
00078 myrpc le(p6, "le"); // fonction apelable par l'adresse 
00079 
00080 extern Ethernet eth;        // eth is defined elsewhere, avoid compiler error.
00081 Serial pc(USBTX, USBRX);    // usb sur Mbed l'autre sur testbed ??
00082 
00083 
00084 HTTPStatus myDynamicPage(HTTPConnection *con, HTTPDynamicPageData *pd) {
00085 #if 0
00086     // Static example. With this, we don't really need HTTPStaticPage
00087     pd->size = 0;    // let it measure our page
00088     pd->page = (char*)content; // Nothing dynamic about that yet, but we can now get loose here.
00089     pd->page_free = NULL;      // No mem free() needed
00090 #elif 0
00091     // Dynamic example, static buffer
00092     pd->size = sprintf(dynamic_content, content_fmt, con->getURL(), con->getHeaderFields());
00093     pd->page = (char*)dynamic_content;
00094     pd->page_free = NULL;    // No mem free() needed
00095 if(pd->size > sizeof(dynamic_content)) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__);    // Buffer overrun
00096 #else
00097     // Dynamic example, dynamic buffer
00098     int size = sizeof(content_fmt) + 512;    // Just guess how much the data will expand
00099     char *buf = (char *)malloc(size);
00100     if (buf) {
00101         pd->size = sprintf(buf, content_fmt, con->getURL(), con->getHeaderFields());
00102         pd->page = buf;
00103         pd->page_free = &free;    // Use free() when done
00104 if(pd->size > size) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__);    // Buffer overrun
00105 #endif
00106     }
00107     return HTTP_OK;
00108 }
00109 //*******************************************MAIN***************************
00110 //****************************************************************************   
00111     int main(void)
00112      {
00113     lcd.cls();
00114     pc.baud(115200);
00115     lcd.printf("LRSD Webserverv2 \n");
00116     printf("LRSD  debout  !!\n");
00117     // initialisation de   static.htm en ram  local 
00118     #include "defhtm.h"
00119     
00120   
00121     char mac[6];
00122     bool use_sd = false;
00123     char res[10]= "1234000";
00124     int  pt = 0;  // pointeur dans tab 128 c'est sur le *
00125     led1=1; led2=1; led3=1;  led4=1; led5 = 0;  //led off
00126 
00127     // Start RTC
00128     time_t seconds = time(NULL);
00129     if (seconds == (unsigned)-1 || seconds == 0) {
00130       seconds = 1256729737;     // Set RTC time to Wed, 28 Oct 2009 11:35:37
00131       set_time(seconds);
00132       printf("RTC initialized, start time %d seconds\r\n", seconds);
00133     }
00134     
00135     led6 = sw1;
00136     if ( sw1 == 0 )
00137       { aip4 = 172; aip3 = 20 ; aip2 = 140 ; aip1 = 49; //BT1 on > adresse de  mon  PC
00138         pon4=  172; pon3 = 20 ; pon2 = 0   ; pon1 = 254;}
00139     else
00140       { aip4 = 192; aip3 = 168 ; aip2 = 1 ; aip1 = 77;      // si on ne touche pas BT1 adresse  box
00141         pon4 = 192; pon3 = 168 ; pon2 = 1 ; pon1 = 0;  }     // si on ne touche pas BT1 adresse  box
00142     lcd.locate(0,1); 
00143     lcd.printf("IP:%hhu.%hhu.%hhu.%hhu\n",                  
00144                   aip4, aip3, aip2, aip1); 
00145      HTTPServer http("mbed",                  //  static IP address and domain name.
00146                  IPv4(aip4,aip3,aip2,aip1),   // IPv4 is a helper function which allows to rtype ipaddresses direct
00147                  IPv4(255,255,0,0),        
00148                  IPv4(pon4,pon3,pon2,pon1),        
00149                  IPv4(pon4,pon3,pon2,pon1),   
00150                    80);             // port 80  souvent 
00151     char *hostname = "mbed";
00152    // HTTPServer http(hostname);    // Use DHCP
00153     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.
00154    
00155     eth.address(mac);
00156     pc.printf(CLS "\r\n\r\nHTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n%s",
00157         hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
00158         gDebug?"Debug is ON\r\n":""
00159     );
00160 
00161     Base::add_rpc_class<myrpc>();
00162     led1=0;
00163     // Check if we can use SD card for the web server
00164     FILE *fp = fopen("/sd/index.htm", "r");
00165     if (fp == NULL) {
00166         if (gDebug) printf("DEBUG: No SD card found or no index.htm file - using LocalFilesystem for WebServer.\r\n");
00167     } else {
00168         use_sd = true;
00169         fclose(fp);
00170         if (gDebug) printf("DEBUG: Found SD card with index.htm file - using SD for WebServer.\r\n");
00171     }
00172 
00173     if (0 != SNTPReadIniFile("/sd/sntp.ini") )
00174         SNTPReadIniFile("/local/sntp.ini");
00175     SNTPWriteIniFile(stdout);
00176 
00177     http.addHandler(new HTTPLinkStatus("/", 
00178 #if MBED_BOB2
00179       p25, p26,            // MBED-BoB2-mods
00180 #else
00181       LED3, LED4,
00182 #endif
00183       0.1,   // ici ???
00184       /*do_urlfile*/ true, /*do_link_printf*/ true, /*do_log_printf*/ false, 
00185       /*log_file*/ ( (gDebug>1) ? (use_sd ? "/sd/httpd.log" : "/local/httpd.log") : NULL)
00186     ));    // Should be the first handler to get a preview of all requests
00187     //  fin du ici ?? 
00188     
00189     http.addHandler(new HTTPRPC());// on y trouve d'autres choses a faire
00190     led2=0;   // 
00191 
00192     // Static/Dynamic pages must be installed before FileSystem on /
00193     http.addHandler(new HTTPStaticPage("/static.htm", tab, strlen(tab)));   // ma page statique en ram
00194  // http.addHandler(new HTTPDynamicPage("/dynamic.htm", &myDynamicPage));   avant
00195     http.addHandler(new HTTPled("/led.htm", led , strlen(tab)));  /// test
00196     http.addHandler(new HTTPFileSystemHandler("/", use_sd ? "/sd/" : "/local/"));
00197     led3=0;
00198 
00199 // FIXME: BUG If eth is not plugged, http.bind() hangs!
00200     http.bind();
00201     SNTPClientInit();
00202     led4 = 0;
00203    // pc.printf("\r");    // Add linefeed for stupid Hyperterminal
00204     
00205     while(1) {  //BOUCLE POLL
00206         http.poll();    // Have to call this method at least every 250ms to let the http server run.      
00207         wait(gWait);
00208         
00209        ana = Potm.read();   //  travail en temps reel like ( gWait)
00210        lcd.locate(0,2); //ligne 2 lcd 
00211        ana = 3.2*ana;   // mise au niveau 3v2
00212               lcd.printf("Value: %1.2f", ana);
00213               sprintf(res, "%f", ana);  // met ana en chaine dans res[]
00214          pt = 127 ; //point dans l'affichage a faire
00215         tab[pt+0] = res[0]; // met 5 char de la chaine a dans static.htm
00216         tab[pt+1] = res[1];
00217         tab[pt+2] = res[2];
00218         tab[pt+3] = res[3];  
00219         tab[pt+4] = res[4];  
00220         
00221   
00222     }
00223 }
00224 
00225 //END