LRSD stephane / Mbed 2 deprecated WEBserverv4

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //  server v4 ou version 4   modified LRSD  
00002 //  for mbed with  TestBed  card  Electronik laden
00003 // Example d' 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 //  LRSD  evolution from    pub-iva2k-ethsntp  project
00009 // Instructions:
00010 // 1  Plug ETH connector into your network (with a switch beetwen   pc)
00011 // 3  Plug MBED tu pc  using USB cable
00012 // 4  need to have Installed MBED serial driver (http://mbed.org/handbook/SerialPC)
00013 // 5     Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct)
00014 // 6  Open terminal on the mbed serial port
00015 // 7  Push MBED reset button
00016 // 8  Terminal will display info message with mac address, followed by IP address 
00017 // 9  Open browser and enter the following URL:    http://192,168,1,77/   !! ( sd card site)
00018 // 10 The browser will show "index.htm"  web page that you put in the sd card
00019 // 11 Open browser and enter the following URL:  http://192,168,1,77/testbed.htm  
00020 // 12 here in  your browser     clic on Led  on ou off  !!
00021 // 13               or  Lecture , an1.htm in sd  show a voltage in testbed ( pot)
00022 // 14               or put a number and envoyer , give a number to testbed card     
00023 // les fichiers htm utiles a mettre en SD sont dans le r'epertoire htm.                
00024 //modifs   lxip/HHTPServer/testbed.h  description des adresses a decoder pour les actions dans loop
00025  
00026 // global usefull things 
00027 int aip4,aip3,aip2,aip1;  
00028 int pon4,pon3,pon2,pon1;  
00029 int gDebug=1;
00030 int var = 0;   // image pour la led 
00031 int adc = 0; 
00032 char rr;
00033 float ana = 1.0;
00034 float gWait = 0.05;        // Main loop wait timeout
00035 char *url = "0123            ";
00036 char *add = "0000      ";
00037 char *lieu ;
00038 int  par1 ; 
00039 
00040 #include "mbed.h"
00041 #include "NewTextLCD.h"
00042 #include "SDFileSystem.h"
00043 #include "HTTPRPC.h"
00044 #include "HTTPServer.h"
00045 #include "HTTPFS.h"
00046 #include "HTTPStaticPage.h"
00047 #include "HTTPLinkStatus.h"
00048 #include "SNTPClient.h"
00049 #define CLS "\033[2J"
00050 
00051 
00052     // E/S sur  la carte mbed 005.1
00053 DigitalOut led1(LED1, "led1");  // mbed
00054 DigitalOut led2(LED2, "led2");
00055 DigitalOut led3(LED3, "led3");
00056 DigitalOut led4(LED4, "led4");
00057     // E/S  hors carte mbed sur la carte TestBed
00058 DigitalIn sw1(p8, "sw1");       // testbed bt1  ( bt2 on  sd card !!)
00059 DigitalOut led5(p6, "led5");    // 2 led on testbed
00060 DigitalOut led6(p5, "led6");
00061 AnalogIn Potm(p15);             //potentiometre carte testbed 
00062 TextLCD lcd(p26, p25, p24, p23, p22, p20, p19, TextLCD::LCD20x4); // testbed LCD
00063 LocalFileSystem local("local");
00064 SDFileSystem sd(p11, p12, p13, p14, "sd");        //test bed sd card 
00065 
00066 extern Ethernet eth;        // eth is defined elsewhere, avoid compiler error.
00067 Serial pc(USBTX, USBRX);    // usb sur la Mbed
00068 
00069 
00070 //*******************************************MAIN***************************
00071 //****************************************************************************   
00072     int main(void)
00073      {
00074     lcd.cls();
00075     pc.baud(115200);
00076     lcd.printf("LRSD Webserver V4 \n");
00077     printf("LRSD  debout  !!\n");
00078       
00079     char mac[6];
00080     bool use_sd = false;
00081     char res[16]= "<H2>000000";    // resultat conv an 
00082     char rr[10]= "00000000";
00083     led1=1; led2=1; led3=1;  led4=1; led5 = 0;  //led off
00084 
00085     // Start RTC
00086     time_t seconds = time(NULL);
00087     if (seconds == (unsigned)-1 || seconds == 0) {
00088       seconds = 1256729737;     // Set RTC time to Wed, 28 Oct 2009 11:35:37
00089       set_time(seconds);
00090       printf("RTC initialized, start time %d seconds\r\n", seconds);
00091     }
00092     
00093        aip4 = 192; aip3 = 168 ; aip2 = 1 ; aip1 = 77;      
00094        pon4 = 192; pon3 = 168 ; pon2 = 1 ; pon1 = 0;      
00095     lcd.locate(0,1); 
00096     lcd.printf("IP:%hhu.%hhu.%hhu.%hhu\n",   aip4, aip3, aip2, aip1); 
00097      HTTPServer http("mbed",                  //  static IP address and domain name.
00098                  IPv4(aip4,aip3,aip2,aip1),   // IPv4 is a helper function which allows to rtype ipaddresses direct
00099                  IPv4(255,255,0,0),        
00100                  IPv4(pon4,pon3,pon2,pon1),        
00101                  IPv4(pon4,pon3,pon2,pon1),   
00102                    80);             // port 80  souvent 
00103     char *hostname = "mbed";
00104    // HTTPServer http(hostname);    // Use DHCP
00105     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.
00106    
00107     eth.address(mac);
00108     pc.printf(CLS "\r\n\r\nHTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n%s",
00109         hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
00110         gDebug?"Debug is ON\r\n":""
00111     );
00112 
00113     // Check if we can use SD card for the web server
00114     FILE *fp = fopen("/sd/index.htm", "r");
00115     if (fp == NULL) {
00116         if (gDebug) printf("DEBUG: No SD card found or no index.htm file - using LocalFilesystem for WebServer.\r\n");
00117     } else {
00118         use_sd = true;
00119         fclose(fp);
00120         if (gDebug) printf("DEBUG: Found SD card with index.htm file - using SD for WebServer.\r\n");
00121     }
00122 
00123     if (0 != SNTPReadIniFile("/sd/sntp.ini") )
00124         SNTPReadIniFile("/local/sntp.ini");
00125     SNTPWriteIniFile(stdout);
00126 
00127     http.addHandler(new HTTPLinkStatus("/", 
00128 #if MBED_BOB2
00129       p25, p26,            // MBED-BoB2-mods
00130 #else
00131       LED3, LED4,
00132 #endif
00133       0.1,   // ici ???
00134       /*do_urlfile*/ true, /*do_link_printf*/ true, /*do_log_printf*/ false, 
00135       /*log_file*/ ( (gDebug>1) ? (use_sd ? "/sd/httpd.log" : "/local/httpd.log") : NULL)
00136     ));    // Should be the first handler to get a preview of all requests
00137     //  fin du ici ?? 
00138     
00139     http.addHandler(new HTTPFileSystemHandler("/", use_sd ? "/sd/" : "/local/"));
00140     led3=0;
00141 
00142 // FIXME: BUG If eth is not plugged, http.bind() hangs!
00143     http.bind();
00144     SNTPClientInit();
00145     led4 = 0;
00146    // pc.printf("\r");    // Add linefeed for stupid Hyperterminal
00147     
00148     while(1) {  //BOUCLE POLL
00149         http.poll();    // Have to call this method at least every 250ms to let the http server run.      
00150         wait(gWait);
00151         
00152        ana = Potm.read();   //  travail en temps reel like ( gWait)
00153        lcd.locate(0,2); //ligne 2 lcd 
00154        ana = 3.2*ana;   // mise au niveau 3v2
00155               lcd.printf("Value: %1.2f", ana);
00156               sprintf(rr, "%f", ana);  // met ana en chaine dans rr[]
00157               res[4]=rr[0];  res[5]=rr[1]; res[6]=rr[2]; res[7]=rr[3] ;res[8]=rr[4];  // on fait mieux
00158               res[9]='V' ;res[10]= 0;
00159  
00160   if ( adc == 1)
00161           {
00162           FILE *fp = fopen("/sd/an1.htm", "w");     // on met an1 en sd card , il sera lu par le code html 
00163           fprintf( fp,res );   
00164           fclose(fp);
00165          
00166           adc = 0;
00167           }      
00168          
00169   lcd.locate(0,3);
00170   lcd.printf("retour = %2hhu\n", par1);     
00171   if ( var == 1) {led6 = 1 ;}  // dans serveur on scrute et on force var 
00172   else { led6 = 0;}
00173   if ( par1 > 0 ) { led5 = !led5 ; par1 = par1 - 1; }  // exemple clignote "par1" fois
00174   if ( par1 == 0 ) led5=1;
00175     }
00176 }
00177 
00178 //END