![](/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-11
- Revision:
- 1:71c0a02e6587
- Parent:
- 0:8c69f4c2bb0e
- Child:
- 2:fb8f5c072e24
File content as of revision 1:71c0a02e6587:
#include "mbed.h" #include "EthernetNetIf.h" #include "HTTPServer.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"); LocalFileSystem fs("webfs");// Create the local filesystem under the name "local" LocalFileSystem local("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"); 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 //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); printf("Internet time is (UTC): %s\n", ctime(&ctTime)); FILE *fp = fopen("/local/test.txt", "a"); fprintf(fp,"Mbed restart (UTC): %s \r\n", ctime(&ctTime)); fclose(fp); Timer tm; tm.start(); while (true) { Net::poll(); if (tm.read()>0.5) { led1=!led1; //Show that we are alive tm.start(); } }//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<20; 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 sesame (UTC): %s \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>"); }