HTTP Server serving a simple webpage which enables to remotely turn LED1 on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.
Dependencies: EthernetInterface mbed-rtos mbed-dev
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/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.
The project was inspired by the Tuxgraphics Web Switch. Thank you Guido!
For a Web Switch using
- mbed OS 5 see WebSwitch_mbed-os
- ENC28J60 Ethernet module see WebSwitch_ENC28J60
- WIZ550io or W5500 Ethernet module see WebSwitch_Wiz550io.
Diff: main.cpp
- Revision:
- 1:c024e74b3a75
- Parent:
- 0:da3404d26549
- Child:
- 2:4a51bdd24745
--- a/main.cpp Thu Apr 06 18:56:06 2017 +0000 +++ b/main.cpp Fri Apr 14 07:17:03 2017 +0000 @@ -5,7 +5,7 @@ using namespace std; -#define PORT 80 +#define PORT 80 EthernetInterface ethernet; @@ -27,13 +27,13 @@ string httpHeader; // HTTP header string httpContent; // HTTP content /** - * @brief - * @note + * @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) { -// // define your own MAC Address // mac[0] = 0x00; // mac[1] = 0x01; // mac[2] = 0x02; @@ -41,32 +41,34 @@ // mac[4] = 0x04; // mac[5] = 0x05; //}; -// analyse the url given -// return values: -1 invalid password -// -2 no command given but password valid -// -3 just refresh page -// 0 switch off -// 1 switch on -// -// The string passed to this function will look like this: -// GET /password HTTP/1..... -// GET /password/ HTTP/1..... -// GET /password/?sw=1 HTTP/1..... -// GET /password/?sw=0 HTTP/1..... -int8_t analyseURL(string& str) { - if(str.substr(5, PASSWORD.size()) != PASSWORD) +/** + * @brief Analyses the received URL + * @note The string passed to this function will look like this: + * GET /password HTTP/1..... + * GET /password/ HTTP/1..... + * GET /password/?sw=1 HTTP/1..... + * GET /password/?sw=0 HTTP/1..... + * @param url URL string + * @retval -1 invalid password + * -2 no command given but password valid + * -3 just refresh page + * 0 switch off + * 1 switch on + */ +int8_t analyseURL(string& url) { + if(url.substr(5, PASSWORD.size()) != PASSWORD) return(-1); uint8_t pos = 5 + PASSWORD.size(); - if(str.substr(pos, 1) == " ") + if(url.substr(pos, 1) == " ") return(-2); - if(str.substr(pos++, 1) != "/") + if(url.substr(pos++, 1) != "/") return(-1); - string cmd(str.substr(pos, 5)); + string cmd(url.substr(pos, 5)); if(cmd == "?sw=0") return(OFF); @@ -129,12 +131,12 @@ * @retval */ void sendHTTP(TCPSocketConnection& client, string& header, string& content) { - char content_length[5] = { }; + char contentLength[5] = { }; header += "\r\nContent-Type: text/html\r\n"; header += "Content-Length: "; - sprintf(content_length, "%d", content.length()); - header += string(content_length) + "\r\n"; + sprintf(contentLength, "%d", content.length()); + header += string(contentLength) + "\r\n"; header += "Pragma: no-cache\r\n"; header += "Connection: About to close\r\n"; header += "\r\n"; @@ -154,11 +156,11 @@ int main(void) { //setup ethernet interface - ethernet.init(); //Use DHCP - //ethernet.init("192.168.1.36", "255.255.255.0", "192.168.1.1"); // static IP + //ethernet.init("192.168.1.36", "255.255.255.0", "192.168.1.1"); // Use static IP ethernet.connect(); - printf("Usage: Type %s/%s/ into your web browser and hit ENTER\r\n\r\n", ethernet.getIPAddress(), PASSWORD); + printf("USAGE: Type '%s/%s/' into your web browser and hit ENTER\r\n", ethernet.getIPAddress(), PASSWORD.c_str()); + printf("NOTE: Don't forget to type the last '/'.\r\n\r\n"); //setup tcp socket if(server.bind(PORT) < 0) { @@ -166,7 +168,7 @@ return -1; } else { - printf("HTTP server bind successed.\n\r"); + printf("HTTP server bind succeeded.\n\r"); serverIsListening = true; } @@ -186,7 +188,7 @@ } else { printf("\r\n=========================================\r\n"); - printf("Connection succeeded!\n\rIP: %s\n\r", client.get_address()); + printf("Connection accepted!\n\rIP: %s\n\r", client.get_address()); clientIsConnected = true; while(clientIsConnected) { @@ -215,7 +217,7 @@ if(received.substr(0, 6) == "GET / ") { httpHeader = HTTP_OK; - httpContent = "<p>Usage: Type http://ip_address/password/ into your web browser and hit ENTER</p>\r\n"; + httpContent = "<p>USAGE: Type 'http://ip_address/password/' into your web browser and hit ENTER</p>\r\n"; sendHTTP(client, httpHeader, httpContent); continue; } @@ -224,7 +226,7 @@ if(cmd == -2) { - // redirect to the right base url + // redirect to the correct base URL httpHeader = MOVED_PERM; sendHTTP(client, httpHeader, movedPermanently(1)); continue; @@ -251,7 +253,8 @@ } } - printf("Closing connection.\n\rHTTP server is listening...\n\r"); + printf("Closing connection.\n\r"); + printf("HTTP server is listening...\n\r"); client.close(); } }