copy of TCPEchoServer, with changes for a make-shift garage door opener
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
main.cpp@8:9c587d7ed39e, 2013-01-21 (annotated)
- Committer:
- elevatorguy
- Date:
- Mon Jan 21 19:53:21 2013 +0000
- Revision:
- 8:9c587d7ed39e
- Parent:
- 7:e2ffd7edf9c9
- Child:
- 9:4d9da861bdcf
action redirect
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elevatorguy | 5:89cca64797f3 | 1 | #include "mbed.h" |
elevatorguy | 5:89cca64797f3 | 2 | #include "EthernetInterface.h" |
elevatorguy | 5:89cca64797f3 | 3 | |
elevatorguy | 5:89cca64797f3 | 4 | #define SERVER_PORT 80 |
elevatorguy | 5:89cca64797f3 | 5 | |
elevatorguy | 5:89cca64797f3 | 6 | DigitalOut garagebutton(p15); |
elevatorguy | 5:89cca64797f3 | 7 | |
elevatorguy | 5:89cca64797f3 | 8 | void doorcontrol() |
elevatorguy | 5:89cca64797f3 | 9 | { |
elevatorguy | 5:89cca64797f3 | 10 | garagebutton = 1; |
elevatorguy | 5:89cca64797f3 | 11 | wait(0.5); |
elevatorguy | 5:89cca64797f3 | 12 | garagebutton = 0; |
elevatorguy | 5:89cca64797f3 | 13 | } |
elevatorguy | 5:89cca64797f3 | 14 | |
elevatorguy | 5:89cca64797f3 | 15 | int main (void) { |
elevatorguy | 5:89cca64797f3 | 16 | EthernetInterface eth; |
elevatorguy | 5:89cca64797f3 | 17 | eth.init("192.168.1.99", "255.255.255.0", "192.168.1.1"); //Use static IP |
elevatorguy | 5:89cca64797f3 | 18 | eth.connect(); |
elevatorguy | 5:89cca64797f3 | 19 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
elevatorguy | 5:89cca64797f3 | 20 | |
elevatorguy | 5:89cca64797f3 | 21 | TCPSocketServer server; |
elevatorguy | 5:89cca64797f3 | 22 | server.bind(SERVER_PORT); |
elevatorguy | 5:89cca64797f3 | 23 | server.listen(); |
elevatorguy | 5:89cca64797f3 | 24 | |
elevatorguy | 5:89cca64797f3 | 25 | while (true) { |
elevatorguy | 5:89cca64797f3 | 26 | printf("Wait for new connection...\r\n"); |
elevatorguy | 5:89cca64797f3 | 27 | TCPSocketConnection client; |
elevatorguy | 5:89cca64797f3 | 28 | server.accept(client); |
elevatorguy | 5:89cca64797f3 | 29 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
elevatorguy | 5:89cca64797f3 | 30 | |
elevatorguy | 5:89cca64797f3 | 31 | printf("Connection from: %s\r\n", client.get_address()); |
elevatorguy | 5:89cca64797f3 | 32 | char buffer[256]; |
elevatorguy | 5:89cca64797f3 | 33 | |
elevatorguy | 6:ef919d4efe56 | 34 | // HTTP is statusline + headers + \r\n + body |
elevatorguy | 7:e2ffd7edf9c9 | 35 | // these are in that format |
elevatorguy | 6:ef919d4efe56 | 36 | char firstpage[] = {"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 213\r\n\r\n<html><head><title>Garage Door</title></head><body><h4>Garage Door Opener</h4><form action=index.html method=post><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"}; |
elevatorguy | 6:ef919d4efe56 | 37 | char secondpage[] = {"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 248\r\n\r\n<html><head><title>Pressing Button</title></head><body><h4>Garage Door Opener</h4><p>Door should be moving...</p><form action=index.html method=post><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"}; |
elevatorguy | 6:ef919d4efe56 | 38 | char error[] = {"HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 100\r\n\r\n<html><head><title>Error</title></head><body>Sorry, I don't understand your request :(</body></html>"}; |
elevatorguy | 6:ef919d4efe56 | 39 | char error404[] = {"HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 147\r\n\r\n<html><head><title>Error 404</title></head><body><h1>Page not found.</h1> <h2>404</h2><br /><p>(this is not a real webserver FYI)</p></body></html>"}; |
elevatorguy | 8:9c587d7ed39e | 40 | char pagemoved[] = { "HTTP/1.1 301 Moved Permanently\r\nLocation: /index.html\r\n\r\n"} ; |
elevatorguy | 6:ef919d4efe56 | 41 | //I used this website to get string length |
elevatorguy | 6:ef919d4efe56 | 42 | //http://www.string-functions.com/length.aspx |
elevatorguy | 6:ef919d4efe56 | 43 | |
elevatorguy | 5:89cca64797f3 | 44 | while (true) { //accept data |
elevatorguy | 5:89cca64797f3 | 45 | int n = client.receive(buffer, sizeof(buffer)); |
elevatorguy | 5:89cca64797f3 | 46 | if (n <= 0) break; |
elevatorguy | 5:89cca64797f3 | 47 | |
elevatorguy | 5:89cca64797f3 | 48 | if(buffer[0]=='G' && buffer[1]=='E' && buffer[2]=='T') //HTTP GET request |
elevatorguy | 5:89cca64797f3 | 49 | { |
elevatorguy | 5:89cca64797f3 | 50 | if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == ' ') // root directory |
elevatorguy | 5:89cca64797f3 | 51 | { |
elevatorguy | 5:89cca64797f3 | 52 | client.send_all(firstpage, sizeof(firstpage)); |
elevatorguy | 5:89cca64797f3 | 53 | if (n <= 0) break; |
elevatorguy | 5:89cca64797f3 | 54 | } |
elevatorguy | 5:89cca64797f3 | 55 | else if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == 'i' && buffer[6] == 'n' && buffer[7] == 'd' && buffer[8] == 'e' |
elevatorguy | 5:89cca64797f3 | 56 | && buffer[9] == 'x') // index.* |
elevatorguy | 5:89cca64797f3 | 57 | { |
elevatorguy | 5:89cca64797f3 | 58 | client.send_all(firstpage, sizeof(firstpage)); |
elevatorguy | 5:89cca64797f3 | 59 | if (n <= 0) break; |
elevatorguy | 5:89cca64797f3 | 60 | } |
elevatorguy | 5:89cca64797f3 | 61 | else if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == 'a' |
elevatorguy | 5:89cca64797f3 | 62 | && buffer[6] == 'c' && buffer[7] == 't' && buffer[8] == 'i' && buffer[9] == 'o' && buffer[10] == 'n' |
elevatorguy | 5:89cca64797f3 | 63 | && buffer[11] == '.' && buffer[12] == 'h' && buffer[13] == 't' && buffer[14] == 'm') // action.htm |
elevatorguy | 5:89cca64797f3 | 64 | { |
elevatorguy | 8:9c587d7ed39e | 65 | //redirect users automatically if they bookmarked action.htm |
elevatorguy | 8:9c587d7ed39e | 66 | client.send_all(pagemoved, sizeof(firstpage)); |
elevatorguy | 5:89cca64797f3 | 67 | if (n <= 0) break; |
elevatorguy | 5:89cca64797f3 | 68 | } |
elevatorguy | 5:89cca64797f3 | 69 | else |
elevatorguy | 5:89cca64797f3 | 70 | { |
elevatorguy | 5:89cca64797f3 | 71 | client.send_all(error404, sizeof(error404)); |
elevatorguy | 5:89cca64797f3 | 72 | if (n <= 0) break; |
elevatorguy | 5:89cca64797f3 | 73 | } |
elevatorguy | 5:89cca64797f3 | 74 | } |
elevatorguy | 6:ef919d4efe56 | 75 | else if (buffer[0]=='P' && buffer[1]=='O' && buffer[2]=='S' && buffer[3]=='T' && buffer[4]==' ' && buffer[5]=='/' |
elevatorguy | 6:ef919d4efe56 | 76 | && buffer[6]=='i' && buffer[7]=='n' && buffer[8]=='d' && buffer[9]=='e' && buffer[10]=='x' && buffer[11]=='.') |
elevatorguy | 6:ef919d4efe56 | 77 | { |
elevatorguy | 6:ef919d4efe56 | 78 | client.send_all(secondpage, sizeof(secondpage)); |
elevatorguy | 6:ef919d4efe56 | 79 | doorcontrol(); |
elevatorguy | 6:ef919d4efe56 | 80 | if (n <= 0) break; |
elevatorguy | 6:ef919d4efe56 | 81 | } |
elevatorguy | 5:89cca64797f3 | 82 | else |
elevatorguy | 5:89cca64797f3 | 83 | { |
elevatorguy | 6:ef919d4efe56 | 84 | client.send_all(error, sizeof(error)); |
elevatorguy | 5:89cca64797f3 | 85 | if (n <= 0) break; |
elevatorguy | 5:89cca64797f3 | 86 | } |
elevatorguy | 5:89cca64797f3 | 87 | } |
elevatorguy | 5:89cca64797f3 | 88 | client.close(); |
elevatorguy | 5:89cca64797f3 | 89 | } |
elevatorguy | 5:89cca64797f3 | 90 | } |