![](/media/cache/profiles/dc363817786ff182b7bc59565d864523.50x50_q85.jpg)
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
myGarageDoorOpenerHTTPServerExample.cpp
- Committer:
- jrsikken
- Date:
- 2011-06-12
- Revision:
- 3:bdea83a48a95
- Parent:
- 2:fb8f5c072e24
- Child:
- 4:017371090723
File content as of revision 3:bdea83a48a95:
#include "mbed.h" #include "EthernetNetIf.h" #include "HTTPServer.h" #include "RPCFunction.h" #include "NTPClient.h" DigitalOut led1(LED1, "led1"); DigitalOut led2(LED2, "led2"); 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");// Create the local filesystem under the name "local" EthernetNetIf eth( IpAddr(192,168,1,100), //IP Address IpAddr(255,255,255,0), //Network Mask IpAddr(192,168,1,254), //Gateway IpAddr(192,168,1,254) //DNS ); HTTPServer svr; NTPClient ntp; 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; int firsttime; 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>(); Base::add_rpc_class<DigitalIn>(); Base::add_rpc_class<DigitalOut>(); Base::add_rpc_class<DigitalInOut>(); Base::add_rpc_class<PwmOut>(); Base::add_rpc_class<Timer>(); Base::add_rpc_class<BusOut>(); Base::add_rpc_class<BusIn>(); Base::add_rpc_class<BusInOut>(); Base::add_rpc_class<Serial>(); printf("Setting up...\n"); EthernetErr ethErr = eth.setup(); if (ethErr) { printf("Error %d in setup.\n", ethErr); return -1; } 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 svr.bind(80); printf("Listening...\n"); Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); ntp.setTime(server); ctTime = time(NULL); printf("Time (UTC): %s\r\n", ctime(&ctTime)); FILE *fp = fopen("/local/test.txt", "a"); fprintf(fp,"Restart (UTC): %s \r\n", ctime(&ctTime)); fclose(fp); 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();//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.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 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 void sendbit(char a) { if (a==0) {//pin low rfout=1; wait_us(2010); rfout=0; wait_us(270); rfout=1; wait_us(2010); rfout=0; wait_us(270); } if (a==1) {//pin high rfout=1; wait_us(270); rfout=0; wait_us(2010); rfout=1; wait_us(270); rfout=0; wait_us(2010); } } void open(char * input, char * output) { led4=1; for (char i = 0; i<10; i++) { sendbit(1);//address A1 sendbit(0);//address A2 sendbit(0);//address A3 sendbit(0);//address A4 sendbit(1);//address A5 sendbit(0);//address A6 /data D6 sendbit(1);//address A7 /data D7 sendbit(0);//address A8 /data D8 sendbit(1);//address A9 /data D9 wait_ms(14); } led4=0; 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>"); }