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