copy of TCPEchoServer, with changes for a make-shift garage door opener
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
Diff: main.cpp
- Revision:
- 5:89cca64797f3
- Parent:
- 3:36fd3cfad85a
- Child:
- 6:ef919d4efe56
--- a/main.cpp Mon Aug 13 09:38:47 2012 +0000 +++ b/main.cpp Mon Jan 21 03:15:51 2013 +0000 @@ -1,34 +1,77 @@ -#include "mbed.h" -#include "EthernetInterface.h" - -#define ECHO_SERVER_PORT 7 - -int main (void) { - EthernetInterface eth; - eth.init(); //Use DHCP - eth.connect(); - printf("IP Address is %s\n", eth.getIPAddress()); - - TCPSocketServer server; - server.bind(ECHO_SERVER_PORT); - server.listen(); - - while (true) { - printf("\nWait for new connection...\n"); - TCPSocketConnection client; - server.accept(client); - client.set_blocking(false, 1500); // Timeout after (1.5)s - - printf("Connection from: %s\n", client.get_address()); - char buffer[256]; - while (true) { - int n = client.receive(buffer, sizeof(buffer)); - if (n <= 0) break; - - client.send_all(buffer, n); - if (n <= 0) break; - } - - client.close(); - } -} +#include "mbed.h" +#include "EthernetInterface.h" + +#define SERVER_PORT 80 + +DigitalOut garagebutton(p15); + +void doorcontrol() +{ + garagebutton = 1; + wait(0.5); + garagebutton = 0; +} + +int main (void) { + EthernetInterface eth; + eth.init("192.168.1.99", "255.255.255.0", "192.168.1.1"); //Use static IP + eth.connect(); + printf("IP Address is %s\r\n", eth.getIPAddress()); + + TCPSocketServer server; + server.bind(SERVER_PORT); + server.listen(); + + while (true) { + printf("Wait for new connection...\r\n"); + TCPSocketConnection client; + server.accept(client); + client.set_blocking(false, 1500); // Timeout after (1.5)s + + printf("Connection from: %s\r\n", client.get_address()); + char buffer[256]; + + char firstpage[] = {"<html><head><title>Garage Door</title></head><body><h4>Garage Door Opener</h4><form action=action.htm method=link><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"}; + char secondpage[] = {"<html><head><title>Pressing Button</title></head><body><h4>Garage Door Opener</h4><p>Door should be moving...</p><form action=action.htm method=link><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"}; + char error[] = {"<html><head><title>Error</title></head><body>Sorry, I don't understand your request :(</body></html>"}; + char error404[] = {"<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>"}; + while (true) { //accept data + int n = client.receive(buffer, sizeof(buffer)); + if (n <= 0) break; + + if(buffer[0]=='G' && buffer[1]=='E' && buffer[2]=='T') //HTTP GET request + { + if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == ' ') // root directory + { + client.send_all(firstpage, sizeof(firstpage)); + if (n <= 0) break; + } + else if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == 'i' && buffer[6] == 'n' && buffer[7] == 'd' && buffer[8] == 'e' + && buffer[9] == 'x') // index.* + { + client.send_all(firstpage, sizeof(firstpage)); + if (n <= 0) break; + } + else if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == 'a' + && buffer[6] == 'c' && buffer[7] == 't' && buffer[8] == 'i' && buffer[9] == 'o' && buffer[10] == 'n' + && buffer[11] == '.' && buffer[12] == 'h' && buffer[13] == 't' && buffer[14] == 'm') // action.htm + { + client.send_all(secondpage, sizeof(secondpage)); + doorcontrol(); + if (n <= 0) break; + } + else + { + client.send_all(error404, sizeof(error404)); + if (n <= 0) break; + } + } + else + { + //client.send_all(error, sizeof(error)); + if (n <= 0) break; + } + } + client.close(); + } +} \ No newline at end of file