Trial code integration web page update with analogue data and ntp support

Dependencies:   NTPClient_NetServices mbed

Committer:
pmr1
Date:
Fri Aug 06 17:57:45 2010 +0000
Revision:
0:8cc2035bebfc

        

Who changed what in which revision?

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