The program uses RPC over http to open a garage door over the internet. On a htm page is a link that calls a RPC function

Dependencies:   EthernetNetIf NTPClient_NetServices mbed HTTPServer

Revision:
3:bdea83a48a95
Parent:
2:fb8f5c072e24
Child:
4:017371090723
--- a/myGarageDoorOpenerHTTPServerExample.cpp	Sun Jun 12 13:50:51 2011 +0000
+++ b/myGarageDoorOpenerHTTPServerExample.cpp	Sun Jun 12 15:18:50 2011 +0000
@@ -31,6 +31,7 @@
 float watt;//is calculated from the time between flashes
 int logPower;//initialize global variables
 float time_s;
+int firsttime;
 
 void flip() {// measure time
     time_s = t.read(); // read time and store in global variable
@@ -67,11 +68,9 @@
     svr.addHandler<RPCHandler>("/rpc");
     svr.addHandler<FSHandler>("/files");
     svr.addHandler<FSHandler>("/"); //Default handler
-    //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
     svr.bind(80);
     printf("Listening...\n");
 
-
     Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
     ntp.setTime(server);
     ctTime = time(NULL);
@@ -80,24 +79,31 @@
     fprintf(fp,"Restart (UTC): %s \r\n", ctime(&ctTime));
     fclose(fp);
 
-    button.rise(&flip);  // attach the address of the flip function to the rising edge
+    firsttime=1;//the first time the power must not be calculated
+    button.rise(&flip);// attach the address of the flip function to the rising edge
     Timer tm;//timer to show that mbed is alive
-    tm.start();
-    t.start();
+    tm.start();//start timer to flash led1
+    t.start();//start timer to measure power
     while (true) {
         Net::poll();
         if (tm.read()>0.5) {
             led1=!led1; //Show that we are alive
-            tm.start();
+            tm.reset();
         }
-        if (logPower==1) {
-            logPower=0;
-            watt=3600/time_s;//calculate watt
-            printf("T: %f s\r\n", time_s);//debug info
-            printf("P: %f W\r\n", watt);//debug info
-            FILE *fp = fopen("/local/power.txt", "a");
-            fprintf(fp,"P: %f W\r\n", watt);
-            fclose(fp);
+        if (logPower==1) {//this code is running when the interrupt sets a flag
+            logPower=0;//clear the flag
+            if (firsttime==0) {//the first measurement is incorrect
+                watt=3600/time_s;//calculate watt
+                if (watt>0) {
+                    ctTime = time(NULL);        
+                    //printf("T: %f s\r\n", time_s);//debug info
+                    //printf("P: %f W\r\n", watt);//debug info
+                    FILE *fp = fopen("/local/power.txt", "a");
+                    fprintf(fp,"%s : %f W\r\n", ctime(&ctTime), watt);
+                    fclose(fp);
+                }
+            }
+            firsttime=0;
         }
     }//end of while loop
 }//end of main