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

Files at this revision

API Documentation at this revision

Comitter:
jrsikken
Date:
Tue Jun 14 06:28:04 2011 +0000
Parent:
3:bdea83a48a95
Commit message:

Changed in this revision

HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
myGarageDoorOpenerHTTPServerExample.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r bdea83a48a95 -r 017371090723 HTTPClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Tue Jun 14 06:28:04 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
diff -r bdea83a48a95 -r 017371090723 myGarageDoorOpenerHTTPServerExample.cpp
--- a/myGarageDoorOpenerHTTPServerExample.cpp	Sun Jun 12 15:18:50 2011 +0000
+++ b/myGarageDoorOpenerHTTPServerExample.cpp	Tue Jun 14 06:28:04 2011 +0000
@@ -1,11 +1,13 @@
 #include "mbed.h"
 #include "EthernetNetIf.h"
 #include "HTTPServer.h"
+//#include "HTTPClient.h"
 #include "RPCFunction.h"
 #include "NTPClient.h"
 
 DigitalOut led1(LED1, "led1");
 DigitalOut led2(LED2, "led2");
+DigitalOut led3(LED3, "led3");
 DigitalOut led4(LED4, "led4");
 DigitalOut rfout(p22,"rfout");
 InterruptIn button(p25); //energymeter flash detector
@@ -22,12 +24,14 @@
 HTTPServer svr;
 NTPClient ntp;
 time_t ctTime;
+//HTTPClient client;// for authentication, API key is set in client header
 
 void sendbit(char a);
 void open(char * input, char * output);//Open garage door
 RPCFunction rpc_open(&open, "open");
 
 Timer t;//timer to measure time between power meter flashes
+//Timer t_pachube;//timer to set interval to post values to pachube
 float watt;//is calculated from the time between flashes
 int logPower;//initialize global variables
 float time_s;
@@ -61,10 +65,8 @@
     }
     printf("Setup OK\n");
 
-    //FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
     FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
 
-    //svr.addHandler<SimpleHandler>("/hello");
     svr.addHandler<RPCHandler>("/rpc");
     svr.addHandler<FSHandler>("/files");
     svr.addHandler<FSHandler>("/"); //Default handler
@@ -84,27 +86,48 @@
     Timer tm;//timer to show that mbed is alive
     tm.start();//start timer to flash led1
     t.start();//start timer to measure power
+   // t_pachube.start();//start timer that triggers a post interval
+
     while (true) {
         Net::poll();
         if (tm.read()>0.5) {
             led1=!led1; //Show that we are alive
             tm.reset();
         }
+        /*
+        if (t_pachube.read()>120) {
+            led3=!led3; //Post to pachube
+            string apiKey = "r5Nh4t4AKEH7-TmWz-jRf-q5DBIglZ2t_I_3D0EXLt4"; // copy API key from settings
+            string environmentID = "26502";// use feed ID
+            string datastreamID = "0"; // use datastream ID
+            string data = FloatToStr(watt);//"500";  // datastream value
+            
+            printf("put data value: %f\r\n",watt);
+            HTTPClient client;// for authentication, API key is set in client header
+            client.setRequestHeader("X-PachubeApiKey", apiKey);
+            HTTPText csvContent("text/csv"); // text object holds data to be posted
+            csvContent.set(data);
+            string uri = "http://api.pachube.com/v1/feeds/" + environmentID + "/datastreams/" + datastreamID + ".csv?_method=put"; // uri for post includes feed ID and datastream ID
+            HTTPResult result = client.post(uri.c_str(), csvContent, NULL);// result should be 0 and response should be 200 for successful post
+            int response = client.getHTTPResponseCode();
+            printf("Pachube put response: %i\r\n",response);
+            t_pachube.reset(); 
+        }*/
         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
+                watt=3600/time_s;//calculate power
                 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
+                    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);
+                    fprintf(fp,"%f W\r\n", watt);
                     fclose(fp);
-                }
-            }
-            firsttime=0;
-        }
+                }//end of if(watt>0)
+            }//end of firsttimme=00
+            firsttime=0;//clear the firsttime variable
+        }//end of if(logPower==1)
     }//end of while loop
 }//end of main
 
@@ -129,7 +152,7 @@
         rfout=0;
         wait_us(2010);
     }
-}
+}//end of sendbit()
 
 void open(char * input, char * output) {
     led4=1;
@@ -146,13 +169,13 @@
         wait_ms(14);
     }
     led4=0;
-
+    //beginning of open sesame log
     FILE *fp = fopen("/local/test.txt", "a");
     ctTime = time(NULL);
     fprintf(fp,"Open (UTC): %s \r\n", ctime(&ctTime));
     fclose(fp);
-
-    sprintf(output, "<html><head><meta http-equiv=refresh content='5; url=../../index.htm'></head><body BGCOLOR=#A1F9FF>Opening,wait...</body></html>");
+    //end of open sesame log
+    sprintf(output, "<html><head><meta http-equiv=refresh content='2; url=../../index.htm'></head><body BGCOLOR=#A1F9FF>Opening,wait...</body></html>");
 }