Tiny HTTP server controlling a DigitalOutput.

Turn LED1, or other digital output, on/off using a web browser.

In this example we create a HTTP server for the STM32F407VET6 black board that will serve a simple Web page to remotely turn LED1, or other digital output, on/off by using a web browser.

/media/uploads/hudakz/webswitch03.png/media/uploads/hudakz/webswitch_mobile01.jpg

Notice that DHCP is turned on by default. The IP address assigned to the WebSwitch server along with an instruction how to use it is printed to the connected PC's serial terminal window during program start up.
To use static IP address uncomment and adjust line #221 in main.cpp.

A DP83848 module is used as Ethernet interface.

/media/uploads/hudakz/dp83848_01.jpg

Wiring

DP83848 moduleSTM32F407VET6 board
VCC<=>+3.3V
GND<=>GND
MDIO<=>PA_2
MDC<=>PC_1
OSCIN<=>PA_1
CRS<=>PA_7
RX0<=>PC_4
RX1<=>PC_5
TX_EN<=>PB_11
TX0<=>PB_12
TX1<=>PB_13

Notice that because the RX_ER line is not used the DP83848 module doesn't have to be modified.

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

Revision:
5:92942d4f4ff6
Parent:
4:c11d58a6c9e2
Child:
6:55e13e19bec3
diff -r c11d58a6c9e2 -r 92942d4f4ff6 main.cpp
--- a/main.cpp	Sat May 18 10:25:05 2019 +0000
+++ b/main.cpp	Sat May 18 11:18:59 2019 +0000
@@ -24,7 +24,7 @@
 const int           OFF = 1;
 const int           ON = 0;
  
-DigitalOut          sw(PA_6);
+DigitalOut          sw(PA_6);           // A switch
 float               roomTemp = 21.8;    // A temperature sensor output
  
 const string        PASSWORD     = "secret";    // change as you like
@@ -49,17 +49,20 @@
  *          1 switch on
  */
 int8_t analyseURL(string& url) {
+    if(url.length() < 5 + PASSWORD.size() + 1)
+        return(-1);
+    
     if(url.substr(5, PASSWORD.size()) != PASSWORD)
         return(-1);
  
     uint8_t pos = 5 + PASSWORD.size();
- 
-    if(url.substr(pos, 1) == " ")
+       
+    if(url.substr(pos, 1) != "/")
+        return(-1);
+
+    if(url.substr(pos++, 1) == " ")
         return(-2);
  
-    if(url.substr(pos++, 1) != "/")
-        return(-1);
- 
     string  cmd(url.substr(pos, 5));
  
     if(cmd == "?sw=0")
@@ -248,6 +251,7 @@
         clientSocket = server.accept(&error);
         if (error != 0) {
             pc.printf("Connection failed!\r\n");
+            clientSocket->close();
             continue;
         }
         clientSocket->getpeername(&clientAddress);
@@ -261,6 +265,7 @@
             httpHeader = HTTP_OK;
             httpContent = "<h1>200 OK</h1>";
             sendHTTP(*clientSocket, httpHeader, httpContent);
+            clientSocket->close();
             continue;
         }
  
@@ -268,6 +273,7 @@
             httpHeader = HTTP_OK;
             httpContent = "<p>Usage: Type http://ip_address/password/ into your web browser and hit ENTER</p>\r\n";
             sendHTTP(*clientSocket, httpHeader, httpContent);
+            clientSocket->close();
             continue;
         }
  
@@ -278,6 +284,7 @@
             // redirect to the right base url
             httpHeader = MOVED_PERM;
             sendHTTP(*clientSocket, httpHeader, movedPermanently(1));
+            clientSocket->close();
             continue;
         }
  
@@ -285,6 +292,7 @@
             httpHeader = UNAUTHORIZED;
             httpContent = "<h1>401 Unauthorized</h1>";
             sendHTTP(*clientSocket, httpHeader, httpContent);
+            clientSocket->close();
             continue;
         }