![](/media/cache/profiles/4dfa8c1c082dc012ffd94c2baa621a41.jpg.50x50_q85.jpg)
Http Server
Dependencies: EthernetNetIf HTTPServer RPCInterface mbed
Fork of Gaurd_Server by
main.cpp@1:574f254ef68c, 2014-12-27 (annotated)
- Committer:
- SRINATHMADHU
- Date:
- Sat Dec 27 08:11:08 2014 +0000
- Revision:
- 1:574f254ef68c
- Parent:
- 0:7f9517ce9e71
Http Server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lotfi_baghli | 0:7f9517ce9e71 | 1 | #include "mbed.h" |
lotfi_baghli | 0:7f9517ce9e71 | 2 | #include "EthernetNetIf.h" |
lotfi_baghli | 0:7f9517ce9e71 | 3 | #include "HTTPServer.h" |
lotfi_baghli | 0:7f9517ce9e71 | 4 | #include "RPCFunction.h" |
lotfi_baghli | 0:7f9517ce9e71 | 5 | EthernetNetIf eth( |
SRINATHMADHU | 1:574f254ef68c | 6 | IpAddr(172,16,20,20), //IP Address |
lotfi_baghli | 0:7f9517ce9e71 | 7 | IpAddr(255,255,255,0), //Network Mask |
SRINATHMADHU | 1:574f254ef68c | 8 | IpAddr(172,16,20,1), //Gateway |
SRINATHMADHU | 1:574f254ef68c | 9 | IpAddr(172,16,1,1) |
lotfi_baghli | 0:7f9517ce9e71 | 10 | ); |
lotfi_baghli | 0:7f9517ce9e71 | 11 | HTTPServer svr; |
lotfi_baghli | 0:7f9517ce9e71 | 12 | |
lotfi_baghli | 0:7f9517ce9e71 | 13 | DigitalOut led1(LED1, "led1"); |
lotfi_baghli | 0:7f9517ce9e71 | 14 | DigitalOut led2(LED2, "led2"); |
lotfi_baghli | 0:7f9517ce9e71 | 15 | DigitalOut led3(LED3, "led3"); |
lotfi_baghli | 0:7f9517ce9e71 | 16 | DigitalOut led4(LED4, "led4"); |
SRINATHMADHU | 1:574f254ef68c | 17 | DigitalOut pin1(p21); |
SRINATHMADHU | 1:574f254ef68c | 18 | DigitalOut pin2(p22); |
SRINATHMADHU | 1:574f254ef68c | 19 | DigitalOut pin3(p23); |
SRINATHMADHU | 1:574f254ef68c | 20 | DigitalOut pin4(p24); |
lotfi_baghli | 0:7f9517ce9e71 | 21 | |
lotfi_baghli | 0:7f9517ce9e71 | 22 | LocalFileSystem fs("webfs"); |
lotfi_baghli | 0:7f9517ce9e71 | 23 | //Create a function of the required format |
lotfi_baghli | 0:7f9517ce9e71 | 24 | void rpcX10rf(char * input, char * output); |
lotfi_baghli | 0:7f9517ce9e71 | 25 | RPCFunction rpc_foo(&rpcX10rf, "rpcX10rf"); |
lotfi_baghli | 0:7f9517ce9e71 | 26 | void rpcX10rf(char * input, char * output) { |
SRINATHMADHU | 1:574f254ef68c | 27 | char houseCode; |
SRINATHMADHU | 1:574f254ef68c | 28 | short int numberCode, action; |
SRINATHMADHU | 1:574f254ef68c | 29 | printf("%s\r\n", input); |
SRINATHMADHU | 1:574f254ef68c | 30 | sscanf(input, " %c,%d,%d", &houseCode, &numberCode, &action); |
SRINATHMADHU | 1:574f254ef68c | 31 | printf("%c, %d,%d\r\n", houseCode, numberCode, action); |
SRINATHMADHU | 1:574f254ef68c | 32 | if(numberCode==1 && action==1){pin1=1;led2=1;} |
SRINATHMADHU | 1:574f254ef68c | 33 | else if(numberCode==1 && action==0){pin1=0;led2=0;} |
SRINATHMADHU | 1:574f254ef68c | 34 | else if(numberCode==2 && action==1){pin2=1;led3=1;} |
SRINATHMADHU | 1:574f254ef68c | 35 | else if(numberCode==2 && action==0){pin2=0;led3=0;} |
SRINATHMADHU | 1:574f254ef68c | 36 | else if(numberCode==3 && action==1){pin3=1;led4=1;} |
SRINATHMADHU | 1:574f254ef68c | 37 | else {pin3=0;led4=0;} |
SRINATHMADHU | 1:574f254ef68c | 38 | } |
lotfi_baghli | 0:7f9517ce9e71 | 39 | int main() { |
lotfi_baghli | 0:7f9517ce9e71 | 40 | Base::add_rpc_class<DigitalOut>(); |
lotfi_baghli | 0:7f9517ce9e71 | 41 | EthernetErr ethErr = eth.setup(); |
lotfi_baghli | 0:7f9517ce9e71 | 42 | if (ethErr) { |
lotfi_baghli | 0:7f9517ce9e71 | 43 | printf("Error %d in setup.\n", ethErr); |
lotfi_baghli | 0:7f9517ce9e71 | 44 | return -1; |
lotfi_baghli | 0:7f9517ce9e71 | 45 | } |
lotfi_baghli | 0:7f9517ce9e71 | 46 | printf("\r\nSetup OK\r\n"); |
lotfi_baghli | 0:7f9517ce9e71 | 47 | |
lotfi_baghli | 0:7f9517ce9e71 | 48 | FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path |
lotfi_baghli | 0:7f9517ce9e71 | 49 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
lotfi_baghli | 0:7f9517ce9e71 | 50 | |
lotfi_baghli | 0:7f9517ce9e71 | 51 | //svr.addHandler<SimpleHandler>("/hello"); |
lotfi_baghli | 0:7f9517ce9e71 | 52 | svr.addHandler<RPCHandler>("/rpc"); |
lotfi_baghli | 0:7f9517ce9e71 | 53 | svr.addHandler<FSHandler>("/files"); |
lotfi_baghli | 0:7f9517ce9e71 | 54 | svr.addHandler<FSHandler>("/"); //Default handler |
lotfi_baghli | 0:7f9517ce9e71 | 55 | //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
lotfi_baghli | 0:7f9517ce9e71 | 56 | svr.bind(80); |
lotfi_baghli | 0:7f9517ce9e71 | 57 | |
lotfi_baghli | 0:7f9517ce9e71 | 58 | printf("Listening...\n"); |
lotfi_baghli | 0:7f9517ce9e71 | 59 | |
lotfi_baghli | 0:7f9517ce9e71 | 60 | Timer tm; |
lotfi_baghli | 0:7f9517ce9e71 | 61 | tm.start(); |
lotfi_baghli | 0:7f9517ce9e71 | 62 | //Listen indefinitely |
lotfi_baghli | 0:7f9517ce9e71 | 63 | while (1) { |
lotfi_baghli | 0:7f9517ce9e71 | 64 | Net::poll(); |
lotfi_baghli | 0:7f9517ce9e71 | 65 | if (tm.read()>.5) { |
lotfi_baghli | 0:7f9517ce9e71 | 66 | led1=!led1; //Show that we are alive |
lotfi_baghli | 0:7f9517ce9e71 | 67 | tm.start(); |
lotfi_baghli | 0:7f9517ce9e71 | 68 | } |
lotfi_baghli | 0:7f9517ce9e71 | 69 | } |
lotfi_baghli | 0:7f9517ce9e71 | 70 | } |