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:
- 8:6f52a0c88bca
- Parent:
- 7:482a6f018329
- Child:
- 9:3716ffcf2f27
--- a/main.cpp Sat Jun 15 13:50:41 2019 +0000 +++ b/main.cpp Sun Nov 24 13:59:46 2019 +0000 @@ -12,7 +12,6 @@ #define PORT 80 Serial pc(PC_6, PC_7); -//Serial pc(PA_9, PA_10); EthernetInterface* net; TCPSocket server; TCPSocket* clientSocket; @@ -36,14 +35,17 @@ * @param * @retval */ -//extern "C" void mbed_mac_address(char* mac) { +//uint8_t mbed_otp_mac_address(char *mac) +//{ // mac[0] = 0x00; // mac[1] = 0x01; // mac[2] = 0x02; // mac[3] = 0x03; // mac[4] = 0x04; // mac[5] = 0x05; -//}; + +// return 1; +//} /** * @brief Analyses the received URL @@ -112,15 +114,18 @@ */ string& showWebPage(uint8_t status) { - /*$off*/ char roomTempStr[5]; //roomTemp = ds1820.read(); sprintf(roomTempStr, "%3.1f", roomTemp); - // CSS toggle switch + /*$off*/ httpContent = "<head>" + "<meta charset=\"utf-8\">" + "<meta name=\"viewport\" content=\" initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=0;\"/>" + "<title>Smart Home</title>" + "<link href='http://fonts.googleapis.com/css?family=Droid+Sans&v1' rel='stylesheet' type='text/css'>" "<style>" ".switch {" "position: relative;" @@ -143,7 +148,7 @@ "}" ".slider:before {" "position: absolute;" - "content: "";" + "content: \"\";" "height: 26px;" "width: 26px;" "left: 4px;" @@ -167,13 +172,12 @@ "</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>" "<pre>Heating:\t"; - if (status == ON) { + if(status == ON) { httpContent += "<a href=\"./?sw=0\" class=\"switch\"> " "<input type=\"checkbox\" checked>"; @@ -186,7 +190,8 @@ httpContent += "<div class=\"slider\"></div>" - "</a></pre>" + "</a>" + "</pre>" "<hr>" "<pre>2017 ARMmbed</pre>" "</body>"; @@ -229,6 +234,7 @@ */ int main(void) { + pc.baud(115200); pc.printf("Starting.. \r\n\r\n"); net = new EthernetInterface(); @@ -247,9 +253,12 @@ } // Show the network address - const char* ip = net->get_ip_address(); - const char* netmask = net->get_netmask(); - const char* gateway = net->get_gateway(); + const char* mac = net->get_mac_address(); + const char* ip = net->get_ip_address(); + const char* netmask = net->get_netmask(); + const char* gateway = net->get_gateway(); + + pc.printf("MAC address: %s\r\n", mac ? mac : "None"); pc.printf("IP address: %s\r\n", ip ? ip : "None"); pc.printf("Netmask: %s\r\n", netmask ? netmask : "None"); pc.printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None"); @@ -302,35 +311,32 @@ int cmd = analyseURL(received); - if (cmd == -2) { - // redirect to the right base url - httpHeader = MOVED_PERM; - sendHTTP(*clientSocket, httpHeader, movedPermanently(1)); - clientSocket->close(); - continue; + switch (cmd) { + case -3: + httpHeader = HTTP_OK; + break; + + case -2: + // redirect to the right base url + httpHeader = MOVED_PERM; + break; + + case -1: + httpHeader = UNAUTHORIZED; + httpContent = "<h1>401 Unauthorized</h1>"; + break; + + case 0: + httpHeader = HTTP_OK; + sw = OFF; // turn the switch off + break; + + case 1: + httpHeader = HTTP_OK; + sw = ON; // turn the switch on + break; } - if (cmd == -1) { - httpHeader = UNAUTHORIZED; - httpContent = "<h1>401 Unauthorized</h1>"; - sendHTTP(*clientSocket, httpHeader, httpContent); - clientSocket->close(); - continue; - } - - 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(); }