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
Diff: myGarageDoorOpenerHTTPServerExample.cpp
- Revision:
- 2:fb8f5c072e24
- Parent:
- 1:71c0a02e6587
- Child:
- 3:bdea83a48a95
--- a/myGarageDoorOpenerHTTPServerExample.cpp Sat Jun 11 21:46:32 2011 +0000 +++ b/myGarageDoorOpenerHTTPServerExample.cpp Sun Jun 12 13:50:51 2011 +0000 @@ -6,12 +6,12 @@ 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 LocalFileSystem fs("webfs");// Create the local filesystem under the name "local" -LocalFileSystem local("local"); +LocalFileSystem local("local");// Create the local filesystem under the name "local" EthernetNetIf eth( IpAddr(192,168,1,100), //IP Address @@ -21,12 +21,24 @@ ); HTTPServer svr; NTPClient ntp; - time_t ctTime; +time_t ctTime; 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 +float watt;//is calculated from the time between flashes +int logPower;//initialize global variables +float time_s; + +void flip() {// measure time + time_s = t.read(); // read time and store in global variable + t.reset(); //reset time + led2 = !led2;//toggle led2 + logPower=1;//tell the main loop to calculate the power +} + int main() { Base::add_rpc_class<AnalogIn>(); Base::add_rpc_class<AnalogOut>(); @@ -51,7 +63,7 @@ //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<SimpleHandler>("/hello"); svr.addHandler<RPCHandler>("/rpc"); svr.addHandler<FSHandler>("/files"); svr.addHandler<FSHandler>("/"); //Default handler @@ -63,24 +75,35 @@ Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); ntp.setTime(server); ctTime = time(NULL); - printf("Internet time is (UTC): %s\n", ctime(&ctTime)); + printf("Time (UTC): %s\r\n", ctime(&ctTime)); FILE *fp = fopen("/local/test.txt", "a"); - fprintf(fp,"Mbed restart (UTC): %s \r\n", ctime(&ctTime)); + fprintf(fp,"Restart (UTC): %s \r\n", ctime(&ctTime)); fclose(fp); - Timer tm; + 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(); while (true) { Net::poll(); if (tm.read()>0.5) { led1=!led1; //Show that we are alive tm.start(); } + 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); + } }//end of while loop }//end of main void sendbit(char a) { - if (a==0) { //pin low + if (a==0) {//pin low rfout=1; wait_us(2010); rfout=0; @@ -90,7 +113,7 @@ rfout=0; wait_us(270); } - if (a==1) { //pin high + if (a==1) {//pin high rfout=1; wait_us(270); rfout=0; @@ -104,7 +127,7 @@ void open(char * input, char * output) { led4=1; - for (char i = 0; i<20; i++) { + for (char i = 0; i<10; i++) { sendbit(1);//address A1 sendbit(0);//address A2 sendbit(0);//address A3 @@ -120,7 +143,7 @@ FILE *fp = fopen("/local/test.txt", "a"); ctTime = time(NULL); - fprintf(fp,"Open sesame (UTC): %s \n", ctime(&ctTime)); + 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>");