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:
- 7:482a6f018329
- Parent:
- 6:55e13e19bec3
- Child:
- 8:6f52a0c88bca
--- a/main.cpp Sat May 18 13:30:52 2019 +0000 +++ b/main.cpp Sat Jun 15 13:50:41 2019 +0000 @@ -14,15 +14,12 @@ Serial pc(PC_6, PC_7); //Serial pc(PA_9, PA_10); EthernetInterface* net; - TCPSocket server; TCPSocket* clientSocket; SocketAddress clientAddress; char receiveBuf[1024] = { }; - const int OFF = 1; const int ON = 0; - DigitalOut sw(PA_6); // A switch float roomTemp = 21.8; // A temperature sensor output const string PASSWORD = "secret"; // change as you like @@ -33,6 +30,22 @@ string httpContent; // HTTP content /** + * @brief Defines a custom MAC address + * @note Uncomment the code below to define a unique MAC address. + * Modify the mac array items as needed. + * @param + * @retval + */ +//extern "C" void mbed_mac_address(char* mac) { +// mac[0] = 0x00; +// mac[1] = 0x01; +// mac[2] = 0x02; +// mac[3] = 0x03; +// mac[4] = 0x04; +// mac[5] = 0x05; +//}; + +/** * @brief Analyses the received URL * @note The string passed to this function will look like this: * GET /password HTTP/1..... @@ -106,76 +119,76 @@ sprintf(roomTempStr, "%3.1f", roomTemp); // CSS toggle switch - httpContent = \ + 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);" \ - "}" \ - "</style>" \ - "</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);" + "}" + "</style>" + "</head>" // Web page "<body>" - "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>" \ - "<pre>Temperature:\t" + string(roomTempStr) + "°C</pre>" \ + "<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 += + "<a href=\"./?sw=0\" class=\"switch\"> " "<input type=\"checkbox\" checked>"; } else { - httpContent += \ - "<a href=\"./?sw=1\" class=\"switch\"> " \ + httpContent += + "<a href=\"./?sw=1\" class=\"switch\"> " "<input type=\"checkbox\">"; } - httpContent += \ - "<div class=\"slider\"></div>" \ - "</a></pre>" \ - "<hr>" \ - "<pre>2017 ARMmbed</pre>" \ + httpContent += + "<div class=\"slider\"></div>" + "</a></pre>" + "<hr>" + "<pre>2017 ARMmbed</pre>" "</body>"; return httpContent; @@ -193,13 +206,13 @@ /*$off*/ char content_length[5] = { }; - header += \ - "\r\nContent-Type: text/html\r\n" \ + header += + "\r\nContent-Type: text/html\r\n" "Content-Length: "; sprintf(content_length, "%d", content.length()); - header += \ - string(content_length) + "\r\n" \ - "Pragma: no-cache\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; @@ -227,6 +240,7 @@ //net->set_network (IP, NETMASK, GATEWAY); // include this for using static IP address nsapi_size_or_error_t r = net->connect(); + if (r != 0) { pc.printf("Error! net->connect() returned: %d\n", r); return r; @@ -255,6 +269,7 @@ pc.printf("=========================================\r\n"); nsapi_error_t error; + clientSocket = server.accept(&error); if (error != 0) { pc.printf("Connection failed!\r\n"); @@ -303,11 +318,18 @@ continue; } - if (cmd == 1) - sw = ON; // turn the switch on - if (cmd == 0) - sw = OFF; // turn the switch off - + switch (cmd) { + case 0: + sw = OFF; // turn the switch off + break; + case 1: + sw = ON; // turn the switch on + break; + default: + clientSocket->close(); + continue; + } + httpHeader = HTTP_OK; sendHTTP(*clientSocket, httpHeader, showWebPage(sw)); clientSocket->close();