copy of TCPEchoServer, with changes for a make-shift garage door opener

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoServer by mbed official

Committer:
elevatorguy
Date:
Mon Jan 21 03:15:51 2013 +0000
Revision:
5:89cca64797f3
Parent:
3:36fd3cfad85a
Child:
6:ef919d4efe56
initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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 5:89cca64797f3 34 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>"};
elevatorguy 5:89cca64797f3 35 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>"};
elevatorguy 5:89cca64797f3 36 char error[] = {"<html><head><title>Error</title></head><body>Sorry, I don't understand your request :(</body></html>"};
elevatorguy 5:89cca64797f3 37 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>"};
elevatorguy 5:89cca64797f3 38 while (true) { //accept data
elevatorguy 5:89cca64797f3 39 int n = client.receive(buffer, sizeof(buffer));
elevatorguy 5:89cca64797f3 40 if (n <= 0) break;
elevatorguy 5:89cca64797f3 41
elevatorguy 5:89cca64797f3 42 if(buffer[0]=='G' && buffer[1]=='E' && buffer[2]=='T') //HTTP GET request
elevatorguy 5:89cca64797f3 43 {
elevatorguy 5:89cca64797f3 44 if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == ' ') // root directory
elevatorguy 5:89cca64797f3 45 {
elevatorguy 5:89cca64797f3 46 client.send_all(firstpage, sizeof(firstpage));
elevatorguy 5:89cca64797f3 47 if (n <= 0) break;
elevatorguy 5:89cca64797f3 48 }
elevatorguy 5:89cca64797f3 49 else if(buffer[3]==' ' && buffer[4]=='/' && buffer[5] == 'i' && buffer[6] == 'n' && buffer[7] == 'd' && buffer[8] == 'e'
elevatorguy 5:89cca64797f3 50 && buffer[9] == 'x') // index.*
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] == 'a'
elevatorguy 5:89cca64797f3 56 && buffer[6] == 'c' && buffer[7] == 't' && buffer[8] == 'i' && buffer[9] == 'o' && buffer[10] == 'n'
elevatorguy 5:89cca64797f3 57 && buffer[11] == '.' && buffer[12] == 'h' && buffer[13] == 't' && buffer[14] == 'm') // action.htm
elevatorguy 5:89cca64797f3 58 {
elevatorguy 5:89cca64797f3 59 client.send_all(secondpage, sizeof(secondpage));
elevatorguy 5:89cca64797f3 60 doorcontrol();
elevatorguy 5:89cca64797f3 61 if (n <= 0) break;
elevatorguy 5:89cca64797f3 62 }
elevatorguy 5:89cca64797f3 63 else
elevatorguy 5:89cca64797f3 64 {
elevatorguy 5:89cca64797f3 65 client.send_all(error404, sizeof(error404));
elevatorguy 5:89cca64797f3 66 if (n <= 0) break;
elevatorguy 5:89cca64797f3 67 }
elevatorguy 5:89cca64797f3 68 }
elevatorguy 5:89cca64797f3 69 else
elevatorguy 5:89cca64797f3 70 {
elevatorguy 5:89cca64797f3 71 //client.send_all(error, sizeof(error));
elevatorguy 5:89cca64797f3 72 if (n <= 0) break;
elevatorguy 5:89cca64797f3 73 }
elevatorguy 5:89cca64797f3 74 }
elevatorguy 5:89cca64797f3 75 client.close();
elevatorguy 5:89cca64797f3 76 }
elevatorguy 5:89cca64797f3 77 }