Dependencies:   mbed

Dependents:   TCP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPServerHelloWorld.cpp Source File

HTTPServerHelloWorld.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPServer.h"
00004 #include "HTTPClient.h"
00005 
00006 #define LOCAL "local"
00007 Serial pc(p9,p10);
00008 HTTPServer svr;
00009 
00010 DigitalOut led1(LED1, "led1");
00011 DigitalOut led2(LED2, "led2");
00012 DigitalOut led3(LED3, "led3");
00013 DigitalOut led4(LED4, "led4");
00014 
00015 EthernetNetIf eth(
00016   IpAddr(169,254,113,94),   //IP Address
00017   IpAddr(255,255,0,0),      //Network Mask
00018   IpAddr(129,168,1,1),      //Gateway  /129.168.1.1
00019   IpAddr(129,168,1,1)       //DNS      /129.168.1.1  
00020 );
00021 
00022 LocalFileSystem local(LOCAL);
00023 HTTPClient http;
00024 
00025 void lire_repertoire(struct dirent *p, DIR *d,FILE *file);
00026 
00027 
00028 int main() {
00029      
00030       DIR *d;
00031       struct dirent *p; 
00032       d = opendir("/" LOCAL);      
00033       
00034       pc.baud(9600);
00035       pc.printf("Setting up...\n\r");
00036       EthernetErr ethErr = eth.setup();
00037       if(ethErr)
00038       {
00039         pc.printf("Error %d in setup.\n\r", ethErr);
00040         return -1;
00041       }
00042       pc.printf("Setup OK\n\r");
00043       
00044       FSHandler::mount("/local", "/"); //Mount /local path on web root path
00045  
00046       svr.addHandler<FSHandler>("/");
00047       svr.addHandler<RPCHandler>("/rpc");
00048       svr.bind(80);
00049       
00050       pc.printf("En Ecoute...\n\r");
00051         
00052       Timer tm;
00053       tm.start();      
00054       
00055       FILE *fp = fopen( "/" LOCAL "/testmbed.htm", "w");
00056       if ( fp == NULL )
00057       {
00058         pc.printf("Could not open file for write\n");
00059       }
00060       fprintf(fp,"<B><font size=\"5\"> Liste de programme dans le Mbed :</font></B><br><br>");
00061       lire_repertoire(p,d,fp);      
00062       
00063       fclose(fp); 
00064       pc.printf("\n - OK\n\r");
00065       
00066       IpAddr ip = eth.getIp();
00067       pc.printf("Addresse IP local: %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); 
00068       HTTPText txt;
00069       HTTPResult r = http.get("http://www.martobre.fr/films/get_ip.php", &txt);
00070       
00071       if(r==HTTP_OK)
00072       {
00073          pc.printf("Adresse IP public :\n\r\"%s\"\n\r", txt.gets()); 
00074       }
00075       else
00076       {
00077          pc.printf("Error %d\n", r);
00078       }
00079 
00080       //Listen indefinitely
00081       while(true)
00082       {
00083         Net::poll();
00084         if(tm.read()>.5)
00085         {
00086           led1=!led1; //Show that we are alive
00087           tm.start();
00088         }
00089       }
00090 }
00091 void lire_repertoire(struct dirent *p, DIR *d,FILE *file){
00092     char compteur=0;
00093     
00094     if ( d != NULL ){
00095         while ( (p = readdir(d)) != NULL ){
00096         compteur++;
00097             fprintf(file,"%d/%s <br>",compteur, p->d_name);           
00098         }
00099     }
00100     else{
00101         pc.printf("Could not open directory!\r");
00102     }
00103 }