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@2:fb8f5c072e24, 2011-06-12 (annotated)
- Committer:
- jrsikken
- Date:
- Sun Jun 12 13:50:51 2011 +0000
- Revision:
- 2:fb8f5c072e24
- Parent:
- 1:71c0a02e6587
- Child:
- 3:bdea83a48a95
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 led4(LED4, "led4"); |
jrsikken | 0:8c69f4c2bb0e | 10 | DigitalOut rfout(p22,"rfout"); |
jrsikken | 2:fb8f5c072e24 | 11 | InterruptIn button(p25); //energymeter flash detector |
jrsikken | 0:8c69f4c2bb0e | 12 | |
jrsikken | 0:8c69f4c2bb0e | 13 | LocalFileSystem fs("webfs");// Create the local filesystem under the name "local" |
jrsikken | 2:fb8f5c072e24 | 14 | LocalFileSystem local("local");// Create the local filesystem under the name "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 | 2:fb8f5c072e24 | 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 | 2:fb8f5c072e24 | 30 | Timer t;//timer to measure time between power meter flashes |
jrsikken | 2:fb8f5c072e24 | 31 | float watt;//is calculated from the time between flashes |
jrsikken | 2:fb8f5c072e24 | 32 | int logPower;//initialize global variables |
jrsikken | 2:fb8f5c072e24 | 33 | float time_s; |
jrsikken | 2:fb8f5c072e24 | 34 | |
jrsikken | 2:fb8f5c072e24 | 35 | void flip() {// measure time |
jrsikken | 2:fb8f5c072e24 | 36 | time_s = t.read(); // read time and store in global variable |
jrsikken | 2:fb8f5c072e24 | 37 | t.reset(); //reset time |
jrsikken | 2:fb8f5c072e24 | 38 | led2 = !led2;//toggle led2 |
jrsikken | 2:fb8f5c072e24 | 39 | logPower=1;//tell the main loop to calculate the power |
jrsikken | 2:fb8f5c072e24 | 40 | } |
jrsikken | 2:fb8f5c072e24 | 41 | |
jrsikken | 0:8c69f4c2bb0e | 42 | int main() { |
jrsikken | 0:8c69f4c2bb0e | 43 | Base::add_rpc_class<AnalogIn>(); |
jrsikken | 0:8c69f4c2bb0e | 44 | Base::add_rpc_class<AnalogOut>(); |
jrsikken | 0:8c69f4c2bb0e | 45 | Base::add_rpc_class<DigitalIn>(); |
jrsikken | 0:8c69f4c2bb0e | 46 | Base::add_rpc_class<DigitalOut>(); |
jrsikken | 0:8c69f4c2bb0e | 47 | Base::add_rpc_class<DigitalInOut>(); |
jrsikken | 0:8c69f4c2bb0e | 48 | Base::add_rpc_class<PwmOut>(); |
jrsikken | 0:8c69f4c2bb0e | 49 | Base::add_rpc_class<Timer>(); |
jrsikken | 0:8c69f4c2bb0e | 50 | Base::add_rpc_class<BusOut>(); |
jrsikken | 0:8c69f4c2bb0e | 51 | Base::add_rpc_class<BusIn>(); |
jrsikken | 0:8c69f4c2bb0e | 52 | Base::add_rpc_class<BusInOut>(); |
jrsikken | 0:8c69f4c2bb0e | 53 | Base::add_rpc_class<Serial>(); |
jrsikken | 0:8c69f4c2bb0e | 54 | |
jrsikken | 0:8c69f4c2bb0e | 55 | printf("Setting up...\n"); |
jrsikken | 0:8c69f4c2bb0e | 56 | EthernetErr ethErr = eth.setup(); |
jrsikken | 0:8c69f4c2bb0e | 57 | if (ethErr) { |
jrsikken | 0:8c69f4c2bb0e | 58 | printf("Error %d in setup.\n", ethErr); |
jrsikken | 0:8c69f4c2bb0e | 59 | return -1; |
jrsikken | 0:8c69f4c2bb0e | 60 | } |
jrsikken | 0:8c69f4c2bb0e | 61 | printf("Setup OK\n"); |
jrsikken | 0:8c69f4c2bb0e | 62 | |
jrsikken | 1:71c0a02e6587 | 63 | //FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path |
jrsikken | 0:8c69f4c2bb0e | 64 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
jrsikken | 0:8c69f4c2bb0e | 65 | |
jrsikken | 2:fb8f5c072e24 | 66 | //svr.addHandler<SimpleHandler>("/hello"); |
jrsikken | 0:8c69f4c2bb0e | 67 | svr.addHandler<RPCHandler>("/rpc"); |
jrsikken | 0:8c69f4c2bb0e | 68 | svr.addHandler<FSHandler>("/files"); |
jrsikken | 0:8c69f4c2bb0e | 69 | svr.addHandler<FSHandler>("/"); //Default handler |
jrsikken | 0:8c69f4c2bb0e | 70 | //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
jrsikken | 0:8c69f4c2bb0e | 71 | svr.bind(80); |
jrsikken | 0:8c69f4c2bb0e | 72 | printf("Listening...\n"); |
jrsikken | 0:8c69f4c2bb0e | 73 | |
jrsikken | 1:71c0a02e6587 | 74 | |
jrsikken | 1:71c0a02e6587 | 75 | Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); |
jrsikken | 1:71c0a02e6587 | 76 | ntp.setTime(server); |
jrsikken | 1:71c0a02e6587 | 77 | ctTime = time(NULL); |
jrsikken | 2:fb8f5c072e24 | 78 | printf("Time (UTC): %s\r\n", ctime(&ctTime)); |
jrsikken | 1:71c0a02e6587 | 79 | FILE *fp = fopen("/local/test.txt", "a"); |
jrsikken | 2:fb8f5c072e24 | 80 | fprintf(fp,"Restart (UTC): %s \r\n", ctime(&ctTime)); |
jrsikken | 1:71c0a02e6587 | 81 | fclose(fp); |
jrsikken | 1:71c0a02e6587 | 82 | |
jrsikken | 2:fb8f5c072e24 | 83 | button.rise(&flip); // attach the address of the flip function to the rising edge |
jrsikken | 2:fb8f5c072e24 | 84 | Timer tm;//timer to show that mbed is alive |
jrsikken | 0:8c69f4c2bb0e | 85 | tm.start(); |
jrsikken | 2:fb8f5c072e24 | 86 | t.start(); |
jrsikken | 0:8c69f4c2bb0e | 87 | while (true) { |
jrsikken | 0:8c69f4c2bb0e | 88 | Net::poll(); |
jrsikken | 0:8c69f4c2bb0e | 89 | if (tm.read()>0.5) { |
jrsikken | 0:8c69f4c2bb0e | 90 | led1=!led1; //Show that we are alive |
jrsikken | 0:8c69f4c2bb0e | 91 | tm.start(); |
jrsikken | 0:8c69f4c2bb0e | 92 | } |
jrsikken | 2:fb8f5c072e24 | 93 | if (logPower==1) { |
jrsikken | 2:fb8f5c072e24 | 94 | logPower=0; |
jrsikken | 2:fb8f5c072e24 | 95 | watt=3600/time_s;//calculate watt |
jrsikken | 2:fb8f5c072e24 | 96 | printf("T: %f s\r\n", time_s);//debug info |
jrsikken | 2:fb8f5c072e24 | 97 | printf("P: %f W\r\n", watt);//debug info |
jrsikken | 2:fb8f5c072e24 | 98 | FILE *fp = fopen("/local/power.txt", "a"); |
jrsikken | 2:fb8f5c072e24 | 99 | fprintf(fp,"P: %f W\r\n", watt); |
jrsikken | 2:fb8f5c072e24 | 100 | fclose(fp); |
jrsikken | 2:fb8f5c072e24 | 101 | } |
jrsikken | 0:8c69f4c2bb0e | 102 | }//end of while loop |
jrsikken | 0:8c69f4c2bb0e | 103 | }//end of main |
jrsikken | 0:8c69f4c2bb0e | 104 | |
jrsikken | 0:8c69f4c2bb0e | 105 | void sendbit(char a) { |
jrsikken | 2:fb8f5c072e24 | 106 | if (a==0) {//pin low |
jrsikken | 0:8c69f4c2bb0e | 107 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 108 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 109 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 110 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 111 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 112 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 113 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 114 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 115 | } |
jrsikken | 2:fb8f5c072e24 | 116 | if (a==1) {//pin high |
jrsikken | 0:8c69f4c2bb0e | 117 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 118 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 119 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 120 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 121 | rfout=1; |
jrsikken | 0:8c69f4c2bb0e | 122 | wait_us(270); |
jrsikken | 0:8c69f4c2bb0e | 123 | rfout=0; |
jrsikken | 0:8c69f4c2bb0e | 124 | wait_us(2010); |
jrsikken | 0:8c69f4c2bb0e | 125 | } |
jrsikken | 0:8c69f4c2bb0e | 126 | } |
jrsikken | 0:8c69f4c2bb0e | 127 | |
jrsikken | 1:71c0a02e6587 | 128 | void open(char * input, char * output) { |
jrsikken | 1:71c0a02e6587 | 129 | led4=1; |
jrsikken | 2:fb8f5c072e24 | 130 | for (char i = 0; i<10; i++) { |
jrsikken | 1:71c0a02e6587 | 131 | sendbit(1);//address A1 |
jrsikken | 1:71c0a02e6587 | 132 | sendbit(0);//address A2 |
jrsikken | 1:71c0a02e6587 | 133 | sendbit(0);//address A3 |
jrsikken | 1:71c0a02e6587 | 134 | sendbit(0);//address A4 |
jrsikken | 1:71c0a02e6587 | 135 | sendbit(1);//address A5 |
jrsikken | 1:71c0a02e6587 | 136 | sendbit(0);//address A6 /data D6 |
jrsikken | 1:71c0a02e6587 | 137 | sendbit(1);//address A7 /data D7 |
jrsikken | 1:71c0a02e6587 | 138 | sendbit(0);//address A8 /data D8 |
jrsikken | 1:71c0a02e6587 | 139 | sendbit(1);//address A9 /data D9 |
jrsikken | 1:71c0a02e6587 | 140 | wait_ms(14); |
jrsikken | 0:8c69f4c2bb0e | 141 | } |
jrsikken | 1:71c0a02e6587 | 142 | led4=0; |
jrsikken | 1:71c0a02e6587 | 143 | |
jrsikken | 1:71c0a02e6587 | 144 | FILE *fp = fopen("/local/test.txt", "a"); |
jrsikken | 1:71c0a02e6587 | 145 | ctTime = time(NULL); |
jrsikken | 2:fb8f5c072e24 | 146 | fprintf(fp,"Open (UTC): %s \r\n", ctime(&ctTime)); |
jrsikken | 1:71c0a02e6587 | 147 | fclose(fp); |
jrsikken | 1:71c0a02e6587 | 148 | |
jrsikken | 1:71c0a02e6587 | 149 | sprintf(output, "<html><head><meta http-equiv=refresh content='5; url=../../index.htm'></head><body BGCOLOR=#A1F9FF>Opening,wait...</body></html>"); |
jrsikken | 1:71c0a02e6587 | 150 | } |
jrsikken | 0:8c69f4c2bb0e | 151 | |
jrsikken | 0:8c69f4c2bb0e | 152 | |
jrsikken | 0:8c69f4c2bb0e | 153 | |
jrsikken | 0:8c69f4c2bb0e | 154 | |
jrsikken | 0:8c69f4c2bb0e | 155 | |
jrsikken | 0:8c69f4c2bb0e | 156 | |
jrsikken | 0:8c69f4c2bb0e | 157 | |
jrsikken | 0:8c69f4c2bb0e | 158 | |
jrsikken | 0:8c69f4c2bb0e | 159 | |
jrsikken | 0:8c69f4c2bb0e | 160 | |
jrsikken | 0:8c69f4c2bb0e | 161 | |
jrsikken | 0:8c69f4c2bb0e | 162 | |
jrsikken | 0:8c69f4c2bb0e | 163 |