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.
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.
Wiring
DP83848 module | STM32F407VET6 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!
Diff: main.cpp
- Revision:
- 3:6edf142a16a8
- Parent:
- 2:8b20e8493a5b
- Child:
- 4:c11d58a6c9e2
--- a/main.cpp Sat May 18 09:20:28 2019 +0000 +++ b/main.cpp Sat May 18 10:01:08 2019 +0000 @@ -101,81 +101,77 @@ sprintf(roomTempStr, "%3.1f", roomTemp); // CSS toggle switch - httpContent = "<head>"; - httpContent += "<style>"; - - httpContent += ".switch {"; - httpContent += "position: relative;"; - httpContent += "display: inline-block;"; - httpContent += "width: 60px;"; - httpContent += "height: 34px;"; - httpContent += "}"; - - httpContent += ".switch input {display:none;}"; - - httpContent += ".slider {"; - httpContent += "position: absolute;"; - httpContent += "cursor: pointer;"; - httpContent += "top: 0;"; - httpContent += "left: 0;"; - httpContent += "right: 0;"; - httpContent += "bottom: 0;"; - httpContent += "border-radius: 34px;"; - httpContent += "background-color: #ccc;"; - httpContent += "-webkit-transition: .4s;"; - httpContent += "transition: .4s;"; - httpContent += "}"; - - httpContent += ".slider:before {"; - httpContent += "position: absolute;"; - httpContent += "content: \"\";"; - httpContent += "height: 26px;"; - httpContent += "width: 26px;"; - httpContent += "left: 4px;"; - httpContent += "bottom: 4px;"; - httpContent += "border-radius: 50%;"; - httpContent += "background-color: white;"; - httpContent += "-webkit-transition: .4s;"; - httpContent += "transition: .4s;"; - httpContent += "}"; - - httpContent += "input:checked + .slider {"; - httpContent += "background-color: #8ce196;"; - httpContent += "}"; - - httpContent += "input:focus + .slider {"; - httpContent += "box-shadow: 0 0 1px #8ce196;"; - httpContent += "}"; - - httpContent += "input:checked + .slider:before {"; - httpContent += "-webkit-transform: translateX(26px);"; - httpContent += "-ms-transform: translateX(26px);"; - httpContent += "transform: translateX(26px);"; - httpContent += "}"; - - httpContent += "</style>"; - httpContent += "</head>"; - + httpContent = \ + "<head>" + "<style>" \ + ".switch {" \ + "position: relative;" \ + "display: inline-block;" \ + "width: 60px;" \ + "height: 34px;" \ + "}" \ + ".switch input {display:none;}" \ + ".slider {" \ + "position: absolute;" \ + "cursor: pointer;" \ + "top: 0;" \ + "left: 0;" \ + "right: 0;" \ + "bottom: 0;" \ + "border-radius: 34px;" \ + "background-color: #ccc;" \ + "-webkit-transition: .4s;" \ + "transition: .4s;" \ + "}" \ + ".slider:before {" \ + "position: absolute;" \ + "content: \"\";" \ + "height: 26px;" \ + "width: 26px;" \ + "left: 4px;" \ + "bottom: 4px;" \ + "border-radius: 50%;" \ + "background-color: white;" \ + "-webkit-transition: .4s;" \ + "transition: .4s;" \ + "}" \ + "input:checked + .slider {" \ + "background-color: #8ce196;" \ + "}" \ + "input:focus + .slider {" \ + "box-shadow: 0 0 1px #8ce196;" \ + "}" \ + "input:checked + .slider:before {" \ + "-webkit-transform: translateX(26px);" \ + "-ms-transform: translateX(26px);" \ + "transform: translateX(26px);" \ + "}" \ + // Web page - httpContent += "<body>"; - httpContent += "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>"; - httpContent += "<pre>Temperature:\t" + string(roomTempStr) + "°C</pre>"; - httpContent += "<pre>Heating:\t"; + "</style>" \ + "</head>" \ + "<body>" + "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>" \ + "<pre>Temperature:\t" + string(roomTempStr) + "°C</pre>" \ + "<pre>Heating:\t"; if(status == ON) { - httpContent += "<a href=\"./?sw=0\" class=\"switch\"> "; - httpContent += "<input type=\"checkbox\" checked>"; + httpContent += \ + "<a href=\"./?sw=0\" class=\"switch\"> " \ + "<input type=\"checkbox\" checked>"; } else { - httpContent += "<a href=\"./?sw=1\" class=\"switch\"> "; - httpContent += "<input type=\"checkbox\">"; + httpContent += \ + "<a href=\"./?sw=1\" class=\"switch\"> " \ + "<input type=\"checkbox\">"; } - httpContent += "<div class=\"slider\"></div>"; - httpContent += "</a></pre>"; - httpContent += "<hr>"; - httpContent += "<pre>2017 ARMmbed</pre>"; - httpContent += "</body>"; + httpContent += \ + "<div class=\"slider\"></div>" \ + "</a></pre>" \ + "<hr>" \ + "<pre>2017 ARMmbed</pre>" \ + "</body>"; return httpContent; } @@ -189,13 +185,14 @@ void sendHTTP(TCPSocket& client, string& header, string& content) { char content_length[5] = { }; - header += "\r\nContent-Type: text/html\r\n"; - header += "Content-Length: "; + header += \ + "\r\nContent-Type: text/html\r\n" \ + "Content-Length: "; sprintf(content_length, "%d", content.length()); - header += string(content_length) + "\r\n"; - header += "Pragma: no-cache\r\n"; - header += "Connection: About to close\r\n"; - header += "\r\n"; + header += \ + string(content_length) + "\r\n" \ + "Pragma: no-cache\r\n" \ + "Connection: About to close\r\n\r\n"; string webpage = header + content; client.send((char*)webpage.c_str(), webpage.length());