HTTP Server serving a simple webpage which enables to remotely turn a digital output on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.

Dependencies:   UIPEthernet

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. An inexpensive ENC28J60 Ethernet module is used to assure connection between the mbed board and the Ethernet network (Internet). The ENC28J60 Ethernet module is driven by the UIPEthernet library.

Needed parts:

  • mbed board
  • ENC28J60 Ethernet module
  • Wires
  • Web browser (Internet Explorer, Safari, Firefox, Chrome ...) running on Windows, Mac, Linux, iPhone or Android device.
/media/uploads/hudakz/webswitch_enc.jpg/media/uploads/hudakz/webswitch_mobile01.jpg

Notice that DHCP is turned on by default. If you prefer to use static IP address then uncomment line 234

The IP address assigned to the WebSwitch server along with an instruction how to use it is printed in the connected PC's serial terminal window during program start up.

Warning

Please notice that the 3.3V power supply chip (RT8183-B) installed on an STM32F103C8T6 board is not rated to power also the ENC28J60 board.


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

NOTE:

Revision:
14:810ac368dd6e
Parent:
12:7c46dcf6f7e2
Child:
15:9beb9b99695d
--- a/main.cpp	Tue Sep 03 09:34:17 2019 +0000
+++ b/main.cpp	Sat Sep 07 17:53:02 2019 +0000
@@ -8,6 +8,7 @@
 
 //#define DEBUG
 
+// Static IP address must be unique and compatible with your network.
 #define IP      "192.168.1.35"
 #define GATEWAY "192.168.1.1"
 #define NETMASK "255.255.255.0"
@@ -215,9 +216,11 @@
 {
     printf("Starting ...\r\n");
 
-    // IP address must be unique and compatible with your network.
     //net.set_network(IP, NETMASK, GATEWAY);  // include this for using static IP address
-    net.connect();
+    if (net.connect(30) != 0) { // 'connect' timeout in seconds (defaults to 60 sec)
+        printf("Unable to connet.\r\n");
+        return -1;
+    }
 
     // Show the network address
     const char*     ip = net.get_ip_address();
@@ -227,9 +230,9 @@
     printf("IP address: %s\r\n", ip ? ip : "None");
     printf("Netmask: %s\r\n", netmask ? netmask : "None");
     printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None");
-    printf("------------------------------------------------------\r\n");
-    printf("Usage: Type %s/%s/ into your web browser and hit ENTER\r\n", net.get_ip_address(), PASSWORD.c_str());
-    printf("------------------------------------------------------\r\n");
+    printf("------------------------------------------------------------------\r\n");
+    printf("Usage: Type %s/%s/ into your web browser and hit ENTER\r\n", ip, PASSWORD.c_str());
+    printf("------------------------------------------------------------------\r\n");
 
     /* Open the server on ethernet stack */
     server.open(&net);
@@ -237,8 +240,8 @@
     /* Bind the HTTP port (TCP 80) to the server */
     server.bind(PORT);
 
-    /* Can handle 5 simultaneous connections */
-    server.listen(5);
+    /* Can handle 4 simultaneous connections */
+    server.listen(4);
 
     while (true) {
         client = server.accept();