HTTP Server serving a simple webpage which enables to remotely turn LED1 on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.
Dependencies: W5500Interface mbed
Turn LED1, or other digital output, on/off using a web browser.
In this example we create a HTTP server that will serve a simple Web page to remotely turn LED1, or other digital output on the mbed board, on/off by using a web browser. A WIZ550io module or W5500 Network-Shielld is used to assure connection between the mbed module and the Ethernet network (Internet).
Needed parts:
- mbed board
- WIZ550io module or W5500 Network-Shield
- Wires
- Web browser (Internet Explorer, Safari, Firefox, Chrome ...) running on Windows, Mac, Linux, iPhone or Android device.
The project was inspired by the Tuxgraphics Web Switch. Thank you Guido!
NOTE:
- For a Web Switch using an ENC28J60 Ethernet module see WebSwitch_ENC28J60
- For a Web Switch using mbed's built in Ethernet PHY see WebSwitch_mbed-dev or WebSwitch_mbed-os
Diff: main.cpp
- Revision:
- 4:31010e482971
- Parent:
- 3:d0e3d1bd73e6
- Child:
- 5:458d9d7b5c1b
--- a/main.cpp Sat Feb 06 09:34:36 2016 +0000 +++ b/main.cpp Sat Feb 06 10:34:47 2016 +0000 @@ -3,7 +3,7 @@ * However, you can easily modify the project to remotely switch on/off any external device. * The HTTP server is built from an mbed board and a WIZ550io board. * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>. - * Thanks to Jozsef Voros it works now also with the WIZ5500 modules (without a built-in MAC) + * Thanks to Jozsef Voros it works now also with the W5500 modules without a built-in MAC * See below how to enable that. */ #include "mbed.h" @@ -44,10 +44,10 @@ // In the second case remember to replace LED1 in sw(LED1) constructor (see below). // IP address must be also unique and compatible with your network. Change as appropriate. -// If intead of WIZ550io you would like to use WIZ5500 module please uncommend the following line -#define WIZ5500 1 +// If intead of WIZ550io you'd like to use a W5500 module without a built-in MAC please uncommend the following line +#define W5500 1 -#if defined(WIZ5500) +#if defined(W5500) // The MAC number must be unique within the connected network. Modify as appropriate. uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; #endif @@ -66,7 +66,7 @@ const string PASSWORD = "secret"; // change as you like const string HTTP_OK = "HTTP/1.0 200 OK"; -const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: "; +const string permanentlyMoved = "HTTP/1.0 301 Moved Permanently\r\nLocation: "; const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized"; string httpHeader; // HTTP header @@ -85,7 +85,7 @@ // GET /password/?sw=1 HTTP/1..... // GET /password/?sw=0 HTTP/1..... -int8_t analyse_get_url(string& str) { +int8_t analyseGetURL(string& str) { if(str.substr(5, PASSWORD.size()) != PASSWORD) return(-1); @@ -116,7 +116,7 @@ * @param * @retval */ -string& moved_perm(uint8_t flag) { +string& movedPermanently(uint8_t flag) { if(flag == 1) httpContent = "/" + PASSWORD + "/"; else @@ -157,13 +157,13 @@ * @param * @retval */ -void http_send(TCPSocketConnection& client, string& header, string& content) { - char content_length[5] = { }; +void sendHTTP(TCPSocketConnection& client, string& header, string& content) { + char contentLeght[5] = { }; header += "\r\nContent-Type: text/html\r\n"; header += "Content-Length: "; - sprintf(content_length, "%d", content.length()); - header += string(content_length) + "\r\n"; + sprintf(contentLeght, "%d", content.length()); + header += string(contentLeght) + "\r\n"; header += "Pragma: no-cache\r\n"; header += "Connection: About to close\r\n"; header += "\r\n"; @@ -178,8 +178,19 @@ * @param * @retval */ +void closeClient(void) { + client.close(); + serial.printf("Connection closed.\n\rTCP server is listening...\n\r"); +} + +/** + * @brief + * @note + * @param + * @retval + */ int main(void) { -#if defined(WIZ5500) +#if defined(W5500) int ret = eth.init(MY_MAC, MY_IP, MY_NETMASK, MY_GATEWAY); #else int ret = eth.init(MY_IP, MY_NETMASK, MY_GATEWAY); @@ -241,35 +252,35 @@ if(received.substr(0, 3) != "GET") { httpHeader = HTTP_OK; httpContent = "<h1>200 OK</h1>"; - http_send(client, httpHeader, httpContent); - client.close(); + sendHTTP(client, httpHeader, httpContent); + closeClient(); continue; } if(received.substr(0, 6) == "GET / ") { httpHeader = HTTP_OK; httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n"; - http_send(client, httpHeader, httpContent); - client.close(); + sendHTTP(client, httpHeader, httpContent); + closeClient(); continue; } - int cmd = analyse_get_url(received); + int cmd = analyseGetURL(received); if(cmd == -2) { // redirect to the right base url - httpHeader = MOVED_PERM; - http_send(client, httpHeader, moved_perm(1)); - client.close(); + httpHeader = permanentlyMoved; + sendHTTP(client, httpHeader, movedPermanently(1)); + closeClient(); continue; } if(cmd == -1) { httpHeader = UNAUTHORIZED; httpContent = "<h1>401 Unauthorized</h1>"; - http_send(client, httpHeader, httpContent); - client.close(); + sendHTTP(client, httpHeader, httpContent); + closeClient(); continue; } @@ -282,12 +293,9 @@ } httpHeader = HTTP_OK; - http_send(client, httpHeader, view(sw)); + sendHTTP(client, httpHeader, view(sw)); } - - client.close(); - - serial.printf("Connection closed.\n\rTCP server is listening...\n\r"); + closeClient(); } } }