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.
/media/uploads/hudakz/webswitch_wiz.jpg/media/uploads/hudakz/webswitch_mobile01.jpg

The project was inspired by the Tuxgraphics Web Switch. Thank you Guido!

NOTE:

Revision:
5:458d9d7b5c1b
Parent:
4:31010e482971
Child:
6:3d74cb156c5c
diff -r 31010e482971 -r 458d9d7b5c1b main.cpp
--- a/main.cpp	Sat Feb 06 10:34:47 2016 +0000
+++ b/main.cpp	Mon Feb 08 22:52:27 2016 +0000
@@ -3,6 +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/>.
+ * For more details see <https://developer.mbed.org/users/hudakz/code/WebSwitch_WIZ550io/wiki/Homepage>
  * Thanks to Jozsef Voros it works now also with the W5500 modules without a built-in MAC
  * See below how to enable that.
  */
@@ -44,8 +45,8 @@
 // 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'd like to use a W5500 module without a built-in MAC please uncommend the following line
-#define W5500   1
+// If instead 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(W5500)
 // The MAC number must be unique within the connected network. Modify as appropriate.
@@ -66,7 +67,7 @@
 
 const string        PASSWORD = "secret";    // change as you like
 const string        HTTP_OK = "HTTP/1.0 200 OK";
-const string        permanentlyMoved = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
+const string        MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
 const string        UNAUTHORIZED = "HTTP/1.0 401 Unauthorized";
 
 string              httpHeader;     // HTTP header
@@ -133,7 +134,7 @@
  * @param
  * @retval
  */
-string& view(uint8_t status) {
+string& httpPage(uint8_t status) {
     httpContent = "<h2>Web Switch</h2>\r\n";
 
     if(status == 1) {
@@ -158,7 +159,7 @@
  * @retval
  */
 void sendHTTP(TCPSocketConnection& client, string& header, string& content) {
-    char    contentLeght[5] = { };
+    char    contentLeght[5] = {};
 
     header += "\r\nContent-Type: text/html\r\n";
     header += "Content-Length: ";
@@ -169,7 +170,7 @@
     header += "\r\n";
 
     string  webpage = header + content;
-    client.send (const_cast<char*>(webpage.c_str ()), webpage.length ());
+    client.send(const_cast<char*>(webpage.c_str()), webpage.length());
 }
 
 /**
@@ -270,7 +271,7 @@
                 if(cmd == -2) {
 
                     // redirect to the right base url
-                    httpHeader = permanentlyMoved;
+                    httpHeader = MOVED_PERM;
                     sendHTTP(client, httpHeader, movedPermanently(1));
                     closeClient();
                     continue;
@@ -293,7 +294,7 @@
                 }
 
                 httpHeader = HTTP_OK;
-                sendHTTP(client, httpHeader, view(sw));
+                sendHTTP(client, httpHeader, httpPage(sw));
             }
             closeClient();
         }