Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
main.cpp
- Committer:
- elevatorguy
- Date:
- 2013-01-21
- Revision:
- 5:89cca64797f3
- Parent:
- 3:36fd3cfad85a
- Child:
- 6:ef919d4efe56
File content as of revision 5:89cca64797f3:
#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();
}
}
