This programme Sets a web server using light weigh ip (using Donatien Garnier's server code) which reads a voltage and saves it value in an htm file to be displayed on local page. Server 2 2nd edition Two issues here 1 ) the compiler throws a warning about assignment in RPCHandler.cpp and 2) the local .htm file created on the mbed flash memory is always date stamped with the default date 01/01/2008 11:00

Dependencies:   EthernetNetIf NTPClient_NetServices mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pmr_svr3.cpp Source File

pmr_svr3.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPServer.h"
00004 #include "NTPClient.h"
00005 
00006 DigitalOut led1(LED1, "led1");
00007 DigitalOut led2(LED2, "led2");
00008 DigitalOut led3(LED3, "led3");
00009 DigitalOut led4(LED4, "led4");
00010 
00011 AnalogIn Voltmeter (p20);
00012 
00013 LocalFileSystem fs("webfs");  // this could be any name
00014 
00015 EthernetNetIf eth;  
00016 HTTPServer svr;
00017 NTPClient ntp;
00018 
00019 int updateVolts(time_t ctTime)
00020  {
00021     
00022     FILE *fp = fopen("/webfs/volts.htm", "w");  // Open local filename
00023     // it ignores path and date defaults 1/1/2008 becausse RTC not set
00024     // if I call ithe localfilesystem www rather than 'local' it writes and is seen when drive is refreshed
00025     fprintf(fp, "<title> Volt meter test page </title>\n");
00026     fprintf(fp,"<h1>Volts of the day from port 1</h1>");
00027     fprintf(fp, "volts %f V at %s \r\n",Voltmeter.read(), ctime(&ctTime));
00028     fclose(fp);
00029     return(0);
00030  }
00031 
00032  long int loadTime(void)
00033  {
00034    time_t ctTime;
00035   ctTime = time(NULL); 
00036   char locTime[32]; 
00037 //  printf("Current time is (UTC): %s\n\r", ctime(&ctTime));  
00038 
00039   Host server(IpAddr(), 123, "0.uk.pool.ntp.org");  
00040   ntp.setTime(server);
00041     
00042   ctTime = time(NULL);  
00043   set_time (ctTime);  // sets local rtc
00044   time_t seconds = time(NULL);
00045   strftime(locTime,32, "%I:%M %p\r\n",localtime(&seconds));
00046   printf("RTC Time is now (UTC): %s\n\r",locTime );   
00047   return (ctTime);
00048  }
00049 
00050 
00051 int main() {
00052 
00053   time_t systemTime;
00054   Base::add_rpc_class<DigitalOut>();
00055 
00056   printf("Setting up...\n");
00057   EthernetErr ethErr = eth.setup();
00058   if(ethErr)
00059   {
00060     printf("Error %d in setup.\n", ethErr);
00061     return -1;
00062   }
00063   printf("Setup OK\n");
00064   
00065 //  FSHandler::mount("/webfs", "/files"); //Mount /wwww path on /files web path  - this has no meaning
00066   FSHandler::mount("/webfs", "/"); //Mount /wwww path on web root path
00067  
00068   
00069 //  svr.addHandler<SimpleHandler>("/");  hard code for Hello world 
00070 
00071   
00072   systemTime=loadTime();  
00073   
00074   printf("System is now (UTC): %s\n\r", ctime(&systemTime)); 
00075   Timer tm;
00076   tm.start();
00077   
00078   
00079   svr.addHandler<RPCHandler>("/rpc");   // sets up the remote procedure call handler
00080   svr.addHandler<FSHandler>("/files");//  this does not see the subdirectory
00081   svr.addHandler<FSHandler>("/"); //Default handler
00082   svr.bind(80);
00083   updateVolts(systemTime);
00084   printf("Listening...\n\r");
00085   //Listen indefinitely
00086   while(true)
00087   {
00088     Net::poll();
00089     if(tm.read()>0.5)
00090     {
00091       led1=!led1; //Show that we are alive
00092  // writing to this file continually updates the drive as attached as a usb drive to the host and generates errors  so not good
00093  // and even at 2 second interval the auto play continually brings up windows
00094   //   updateVolts();
00095       tm.start();
00096     }
00097   }
00098   
00099   return 0;
00100 
00101 }