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
pmr_svr3.cpp@0:03e1db2fe866, 2010-08-08 (annotated)
- Committer:
- pmr1
- Date:
- Sun Aug 08 22:00:39 2010 +0000
- Revision:
- 0:03e1db2fe866
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmr1 | 0:03e1db2fe866 | 1 | #include "mbed.h" |
pmr1 | 0:03e1db2fe866 | 2 | #include "EthernetNetIf.h" |
pmr1 | 0:03e1db2fe866 | 3 | #include "HTTPServer.h" |
pmr1 | 0:03e1db2fe866 | 4 | #include "NTPClient.h" |
pmr1 | 0:03e1db2fe866 | 5 | |
pmr1 | 0:03e1db2fe866 | 6 | DigitalOut led1(LED1, "led1"); |
pmr1 | 0:03e1db2fe866 | 7 | DigitalOut led2(LED2, "led2"); |
pmr1 | 0:03e1db2fe866 | 8 | DigitalOut led3(LED3, "led3"); |
pmr1 | 0:03e1db2fe866 | 9 | DigitalOut led4(LED4, "led4"); |
pmr1 | 0:03e1db2fe866 | 10 | |
pmr1 | 0:03e1db2fe866 | 11 | AnalogIn Voltmeter (p20); |
pmr1 | 0:03e1db2fe866 | 12 | |
pmr1 | 0:03e1db2fe866 | 13 | LocalFileSystem fs("webfs"); // this could be any name |
pmr1 | 0:03e1db2fe866 | 14 | |
pmr1 | 0:03e1db2fe866 | 15 | EthernetNetIf eth; |
pmr1 | 0:03e1db2fe866 | 16 | HTTPServer svr; |
pmr1 | 0:03e1db2fe866 | 17 | NTPClient ntp; |
pmr1 | 0:03e1db2fe866 | 18 | |
pmr1 | 0:03e1db2fe866 | 19 | int updateVolts(time_t ctTime) |
pmr1 | 0:03e1db2fe866 | 20 | { |
pmr1 | 0:03e1db2fe866 | 21 | |
pmr1 | 0:03e1db2fe866 | 22 | FILE *fp = fopen("/webfs/volts.htm", "w"); // Open local filename |
pmr1 | 0:03e1db2fe866 | 23 | // it ignores path and date defaults 1/1/2008 becausse RTC not set |
pmr1 | 0:03e1db2fe866 | 24 | // if I call ithe localfilesystem www rather than 'local' it writes and is seen when drive is refreshed |
pmr1 | 0:03e1db2fe866 | 25 | fprintf(fp, "<title> Volt meter test page </title>\n"); |
pmr1 | 0:03e1db2fe866 | 26 | fprintf(fp,"<h1>Volts of the day from port 1</h1>"); |
pmr1 | 0:03e1db2fe866 | 27 | fprintf(fp, "volts %f V at %s \r\n",Voltmeter.read(), ctime(&ctTime)); |
pmr1 | 0:03e1db2fe866 | 28 | fclose(fp); |
pmr1 | 0:03e1db2fe866 | 29 | return(0); |
pmr1 | 0:03e1db2fe866 | 30 | } |
pmr1 | 0:03e1db2fe866 | 31 | |
pmr1 | 0:03e1db2fe866 | 32 | long int loadTime(void) |
pmr1 | 0:03e1db2fe866 | 33 | { |
pmr1 | 0:03e1db2fe866 | 34 | time_t ctTime; |
pmr1 | 0:03e1db2fe866 | 35 | ctTime = time(NULL); |
pmr1 | 0:03e1db2fe866 | 36 | char locTime[32]; |
pmr1 | 0:03e1db2fe866 | 37 | // printf("Current time is (UTC): %s\n\r", ctime(&ctTime)); |
pmr1 | 0:03e1db2fe866 | 38 | |
pmr1 | 0:03e1db2fe866 | 39 | Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); |
pmr1 | 0:03e1db2fe866 | 40 | ntp.setTime(server); |
pmr1 | 0:03e1db2fe866 | 41 | |
pmr1 | 0:03e1db2fe866 | 42 | ctTime = time(NULL); |
pmr1 | 0:03e1db2fe866 | 43 | set_time (ctTime); // sets local rtc |
pmr1 | 0:03e1db2fe866 | 44 | time_t seconds = time(NULL); |
pmr1 | 0:03e1db2fe866 | 45 | strftime(locTime,32, "%I:%M %p\r\n",localtime(&seconds)); |
pmr1 | 0:03e1db2fe866 | 46 | printf("RTC Time is now (UTC): %s\n\r",locTime ); |
pmr1 | 0:03e1db2fe866 | 47 | return (ctTime); |
pmr1 | 0:03e1db2fe866 | 48 | } |
pmr1 | 0:03e1db2fe866 | 49 | |
pmr1 | 0:03e1db2fe866 | 50 | |
pmr1 | 0:03e1db2fe866 | 51 | int main() { |
pmr1 | 0:03e1db2fe866 | 52 | |
pmr1 | 0:03e1db2fe866 | 53 | time_t systemTime; |
pmr1 | 0:03e1db2fe866 | 54 | Base::add_rpc_class<DigitalOut>(); |
pmr1 | 0:03e1db2fe866 | 55 | |
pmr1 | 0:03e1db2fe866 | 56 | printf("Setting up...\n"); |
pmr1 | 0:03e1db2fe866 | 57 | EthernetErr ethErr = eth.setup(); |
pmr1 | 0:03e1db2fe866 | 58 | if(ethErr) |
pmr1 | 0:03e1db2fe866 | 59 | { |
pmr1 | 0:03e1db2fe866 | 60 | printf("Error %d in setup.\n", ethErr); |
pmr1 | 0:03e1db2fe866 | 61 | return -1; |
pmr1 | 0:03e1db2fe866 | 62 | } |
pmr1 | 0:03e1db2fe866 | 63 | printf("Setup OK\n"); |
pmr1 | 0:03e1db2fe866 | 64 | |
pmr1 | 0:03e1db2fe866 | 65 | // FSHandler::mount("/webfs", "/files"); //Mount /wwww path on /files web path - this has no meaning |
pmr1 | 0:03e1db2fe866 | 66 | FSHandler::mount("/webfs", "/"); //Mount /wwww path on web root path |
pmr1 | 0:03e1db2fe866 | 67 | |
pmr1 | 0:03e1db2fe866 | 68 | |
pmr1 | 0:03e1db2fe866 | 69 | // svr.addHandler<SimpleHandler>("/"); hard code for Hello world |
pmr1 | 0:03e1db2fe866 | 70 | |
pmr1 | 0:03e1db2fe866 | 71 | |
pmr1 | 0:03e1db2fe866 | 72 | systemTime=loadTime(); |
pmr1 | 0:03e1db2fe866 | 73 | |
pmr1 | 0:03e1db2fe866 | 74 | printf("System is now (UTC): %s\n\r", ctime(&systemTime)); |
pmr1 | 0:03e1db2fe866 | 75 | Timer tm; |
pmr1 | 0:03e1db2fe866 | 76 | tm.start(); |
pmr1 | 0:03e1db2fe866 | 77 | |
pmr1 | 0:03e1db2fe866 | 78 | |
pmr1 | 0:03e1db2fe866 | 79 | svr.addHandler<RPCHandler>("/rpc"); // sets up the remote procedure call handler |
pmr1 | 0:03e1db2fe866 | 80 | svr.addHandler<FSHandler>("/files");// this does not see the subdirectory |
pmr1 | 0:03e1db2fe866 | 81 | svr.addHandler<FSHandler>("/"); //Default handler |
pmr1 | 0:03e1db2fe866 | 82 | svr.bind(80); |
pmr1 | 0:03e1db2fe866 | 83 | updateVolts(systemTime); |
pmr1 | 0:03e1db2fe866 | 84 | printf("Listening...\n\r"); |
pmr1 | 0:03e1db2fe866 | 85 | //Listen indefinitely |
pmr1 | 0:03e1db2fe866 | 86 | while(true) |
pmr1 | 0:03e1db2fe866 | 87 | { |
pmr1 | 0:03e1db2fe866 | 88 | Net::poll(); |
pmr1 | 0:03e1db2fe866 | 89 | if(tm.read()>0.5) |
pmr1 | 0:03e1db2fe866 | 90 | { |
pmr1 | 0:03e1db2fe866 | 91 | led1=!led1; //Show that we are alive |
pmr1 | 0:03e1db2fe866 | 92 | // writing to this file continually updates the drive as attached as a usb drive to the host and generates errors so not good |
pmr1 | 0:03e1db2fe866 | 93 | // and even at 2 second interval the auto play continually brings up windows |
pmr1 | 0:03e1db2fe866 | 94 | // updateVolts(); |
pmr1 | 0:03e1db2fe866 | 95 | tm.start(); |
pmr1 | 0:03e1db2fe866 | 96 | } |
pmr1 | 0:03e1db2fe866 | 97 | } |
pmr1 | 0:03e1db2fe866 | 98 | |
pmr1 | 0:03e1db2fe866 | 99 | return 0; |
pmr1 | 0:03e1db2fe866 | 100 | |
pmr1 | 0:03e1db2fe866 | 101 | } |