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@0:8c69f4c2bb0e, 2011-06-10 (annotated)
- Committer:
- jrsikken
- Date:
- Fri Jun 10 21:20:19 2011 +0000
- Revision:
- 0:8c69f4c2bb0e
- Child:
- 1:71c0a02e6587
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jrsikken | 0:8c69f4c2bb0e | 1 | #include "mbed.h" |
jrsikken | 0:8c69f4c2bb0e | 2 | #include "EthernetNetIf.h" |
jrsikken | 0:8c69f4c2bb0e | 3 | #include "HTTPServer.h" |
jrsikken | 0:8c69f4c2bb0e | 4 | #include "RPCFunction.h" |
jrsikken | 0:8c69f4c2bb0e | 5 | |
jrsikken | 0:8c69f4c2bb0e | 6 | DigitalOut led1(LED1, "led1"); |
jrsikken | 0:8c69f4c2bb0e | 7 | DigitalOut led2(LED2, "led2"); |
jrsikken | 0:8c69f4c2bb0e | 8 | DigitalOut led3(LED3, "led3"); |
jrsikken | 0:8c69f4c2bb0e | 9 | DigitalOut led4(LED4, "led4"); |
jrsikken | 0:8c69f4c2bb0e | 10 | DigitalOut rfout(p22,"rfout"); |
jrsikken | 0:8c69f4c2bb0e | 11 | |
jrsikken | 0:8c69f4c2bb0e | 12 | LocalFileSystem fs("webfs");// Create the local filesystem under the name "local" |
jrsikken | 0:8c69f4c2bb0e | 13 | |
jrsikken | 0:8c69f4c2bb0e | 14 | EthernetNetIf eth( |
jrsikken | 0:8c69f4c2bb0e | 15 | IpAddr(192,168,1,100), //IP Address |
jrsikken | 0:8c69f4c2bb0e | 16 | IpAddr(255,255,255,0), //Network Mask |
jrsikken | 0:8c69f4c2bb0e | 17 | IpAddr(192,168,1,254), //Gateway |
jrsikken | 0:8c69f4c2bb0e | 18 | IpAddr(192,168,1,254) //DNS |
jrsikken | 0:8c69f4c2bb0e | 19 | ); |
jrsikken | 0:8c69f4c2bb0e | 20 | HTTPServer svr; |
jrsikken | 0:8c69f4c2bb0e | 21 | |
jrsikken | 0:8c69f4c2bb0e | 22 | void sendbit(char a); |
jrsikken | 0:8c69f4c2bb0e | 23 | void open(char * input, char * output);//Open garage door |
jrsikken | 0:8c69f4c2bb0e | 24 | RPCFunction rpc_open(&open, "open"); |
jrsikken | 0:8c69f4c2bb0e | 25 | |
jrsikken | 0:8c69f4c2bb0e | 26 | int main() { |
jrsikken | 0:8c69f4c2bb0e | 27 | Base::add_rpc_class<AnalogIn>(); |
jrsikken | 0:8c69f4c2bb0e | 28 | Base::add_rpc_class<AnalogOut>(); |
jrsikken | 0:8c69f4c2bb0e | 29 | Base::add_rpc_class<DigitalIn>(); |
jrsikken | 0:8c69f4c2bb0e | 30 | Base::add_rpc_class<DigitalOut>(); |
jrsikken | 0:8c69f4c2bb0e | 31 | Base::add_rpc_class<DigitalInOut>(); |
jrsikken | 0:8c69f4c2bb0e | 32 | Base::add_rpc_class<PwmOut>(); |
jrsikken | 0:8c69f4c2bb0e | 33 | Base::add_rpc_class<Timer>(); |
jrsikken | 0:8c69f4c2bb0e | 34 | Base::add_rpc_class<BusOut>(); |
jrsikken | 0:8c69f4c2bb0e | 35 | Base::add_rpc_class<BusIn>(); |
jrsikken | 0:8c69f4c2bb0e | 36 | Base::add_rpc_class<BusInOut>(); |
jrsikken | 0:8c69f4c2bb0e | 37 | Base::add_rpc_class<Serial>(); |
jrsikken | 0:8c69f4c2bb0e | 38 | |
jrsikken | 0:8c69f4c2bb0e | 39 | printf("Setting up...\n"); |
jrsikken | 0:8c69f4c2bb0e | 40 | EthernetErr ethErr = eth.setup(); |
jrsikken | 0:8c69f4c2bb0e | 41 | if (ethErr) { |
jrsikken | 0:8c69f4c2bb0e | 42 | printf("Error %d in setup.\n", ethErr); |
jrsikken | 0:8c69f4c2bb0e | 43 | return -1; |
jrsikken | 0:8c69f4c2bb0e | 44 | } |
jrsikken | 0:8c69f4c2bb0e | 45 | printf("Setup OK\n"); |
jrsikken | 0:8c69f4c2bb0e | 46 | |
jrsikken | 0:8c69f4c2bb0e | 47 | FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path |
jrsikken | 0:8c69f4c2bb0e | 48 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
jrsikken | 0:8c69f4c2bb0e | 49 | |
jrsikken | 0:8c69f4c2bb0e | 50 | svr.addHandler<SimpleHandler>("/hello"); |
jrsikken | 0:8c69f4c2bb0e | 51 | svr.addHandler<RPCHandler>("/rpc"); |
jrsikken | 0:8c69f4c2bb0e | 52 | svr.addHandler<FSHandler>("/files"); |
jrsikken | 0:8c69f4c2bb0e | 53 | svr.addHandler<FSHandler>("/"); //Default handler |
jrsikken | 0:8c69f4c2bb0e | 54 | //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
jrsikken | 0:8c69f4c2bb0e | 55 | svr.bind(80); |
jrsikken | 0:8c69f4c2bb0e | 56 | printf("Listening...\n"); |
jrsikken | 0:8c69f4c2bb0e | 57 | |
jrsikken | 0:8c69f4c2bb0e | 58 | Timer tm; |
jrsikken | 0:8c69f4c2bb0e | 59 | tm.start(); |
jrsikken | 0:8c69f4c2bb0e | 60 | while (true) { |
jrsikken | 0:8c69f4c2bb0e | 61 | Net::poll(); |
jrsikken | 0:8c69f4c2bb0e | 62 | if (tm.read()>0.5) { |
jrsikken | 0:8c69f4c2bb0e | 63 | led1=!led1; //Show that we are alive |
jrsikken | 0:8c69f4c2bb0e | 64 | tm.start(); |
jrsikken | 0:8c69f4c2bb0e | 65 | } |
jrsikken | 0:8c69f4c2bb0e | 66 | }//end of while loop |
jrsikken | 0:8c69f4c2bb0e | 67 | }//end of main |
jrsikken | 0:8c69f4c2bb0e | 68 | |
jrsikken | 0:8c69f4c2bb0e | 69 | void sendbit(char a) { |
jrsikken | 0:8c69f4c2bb0e | 70 | if (a==0) { //pin low |
jrsikken | 0:8c69f4c2bb0e | 71 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 72 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 73 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 74 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 75 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 76 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 77 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 78 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 79 | } |
jrsikken | 0:8c69f4c2bb0e | 80 | if (a==1) { //pin high |
jrsikken | 0:8c69f4c2bb0e | 81 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 82 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 83 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 84 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 85 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 86 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 87 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 88 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 89 | } |
jrsikken | 0:8c69f4c2bb0e | 90 | } |
jrsikken | 0:8c69f4c2bb0e | 91 | |
jrsikken | 0:8c69f4c2bb0e | 92 | void open(char * input, char * output) { |
jrsikken | 0:8c69f4c2bb0e | 93 | led4=1; |
jrsikken | 0:8c69f4c2bb0e | 94 | for (char i = 0; i<20; i++) { |
jrsikken | 0:8c69f4c2bb0e | 95 | sendbit(1);//address A1 |
jrsikken | 0:8c69f4c2bb0e | 96 | sendbit(0);//address A2 |
jrsikken | 0:8c69f4c2bb0e | 97 | sendbit(0);//address A3 |
jrsikken | 0:8c69f4c2bb0e | 98 | sendbit(0);//address A4 |
jrsikken | 0:8c69f4c2bb0e | 99 | sendbit(1);//address A5 |
jrsikken | 0:8c69f4c2bb0e | 100 | sendbit(0);//address A6 /data D6 |
jrsikken | 0:8c69f4c2bb0e | 101 | sendbit(1);//address A7 /data D7 |
jrsikken | 0:8c69f4c2bb0e | 102 | sendbit(0);//address A8 /data D8 |
jrsikken | 0:8c69f4c2bb0e | 103 | sendbit(1);//address A9 /data D9 |
jrsikken | 0:8c69f4c2bb0e | 104 | wait_ms(14); |
jrsikken | 0:8c69f4c2bb0e | 105 | } |
jrsikken | 0:8c69f4c2bb0e | 106 | led4=0; |
jrsikken | 0:8c69f4c2bb0e | 107 | sprintf(output, "<html><head><meta http-equiv=refresh content='5; url=../../index.htm'></head><body BGCOLOR=#A1F9FF>Opening,wait...</body></html>"); |
jrsikken | 0:8c69f4c2bb0e | 108 | } |
jrsikken | 0:8c69f4c2bb0e | 109 | |
jrsikken | 0:8c69f4c2bb0e | 110 | |
jrsikken | 0:8c69f4c2bb0e | 111 | |
jrsikken | 0:8c69f4c2bb0e | 112 | |
jrsikken | 0:8c69f4c2bb0e | 113 | |
jrsikken | 0:8c69f4c2bb0e | 114 | |
jrsikken | 0:8c69f4c2bb0e | 115 | |
jrsikken | 0:8c69f4c2bb0e | 116 | |
jrsikken | 0:8c69f4c2bb0e | 117 | |
jrsikken | 0:8c69f4c2bb0e | 118 | |
jrsikken | 0:8c69f4c2bb0e | 119 | |
jrsikken | 0:8c69f4c2bb0e | 120 | |
jrsikken | 0:8c69f4c2bb0e | 121 |