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: W5500Interface mbed
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 the mbed board, on/off by using a web browser. A WIZ550io module or W5500 Network-Shielld is used to assure connection between the mbed module and the Ethernet network (Internet).
Needed parts:
- mbed board
- WIZ550io module or W5500 Network-Shield
- Wires
- Web browser (Internet Explorer, Safari, Firefox, Chrome ...) running on Windows, Mac, Linux, iPhone or Android device.
The project was inspired by the Tuxgraphics Web Switch. Thank you Guido!
NOTE:
- For a Web Switch using an ENC28J60 Ethernet module see WebSwitch_ENC28J60
- For a Web Switch using mbed's built in Ethernet PHY see WebSwitch_mbed-dev or WebSwitch_mbed-os
Revision 6:3d74cb156c5c, committed 2017-05-01
- Comitter:
- hudakz
- Date:
- Mon May 01 20:04:30 2017 +0000
- Parent:
- 5:458d9d7b5c1b
- Commit message:
- Button added.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 458d9d7b5c1b -r 3d74cb156c5c main.cpp --- a/main.cpp Mon Feb 08 22:52:27 2016 +0000 +++ b/main.cpp Mon May 01 20:04:30 2017 +0000 @@ -13,6 +13,9 @@ using namespace std; +const int OFF = 0; +const int ON = 1; + Serial serial(USBTX, USBRX); #if defined(TARGET_LPC1768) @@ -64,6 +67,7 @@ DigitalOut sw(LED1); // Change LED1 to a pin of your choice. // However, make sure that it does not collide with any of the SPI pins +float roomTemp = 21.8; // A temperature sensor output const string PASSWORD = "secret"; // change as you like const string HTTP_OK = "HTTP/1.0 200 OK"; @@ -73,40 +77,39 @@ string httpHeader; // HTTP header string httpContent; // HTTP content -// 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 analyseGetURL(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); - pos++; - - string cmd(str.substr(pos, 5)); + string cmd(url.substr(pos, 5)); if(cmd == "?sw=0") - return(0); + return(OFF); if(cmd == "?sw=1") - return(1); + return(ON); return(-3); } @@ -134,21 +137,27 @@ * @param * @retval */ -string& httpPage(uint8_t status) { - httpContent = "<h2>Web Switch</h2>\r\n"; +string& showWebPage(uint8_t status) { + char roomTempStr[5]; + + //roomTemp = ds1820.read(); + sprintf(roomTempStr, "%3.1f", roomTemp); - if(status == 1) { - httpContent += "<pre>\r\n <font color=#FF0000>ON</font>"; - httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n"; + httpContent = "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>"; + httpContent += "<pre>Temperature:\t" + string(roomTempStr) + "°C\r\n</pre>"; + + if(status == ON) { + httpContent += "<pre>\r\nHeating:\t<font color=#FF0000>On </font>"; + httpContent += " <a href=\"./?sw=0\"><button>Turn off</button></a>\r\n"; } else { - httpContent += "<pre>\r\n <font color=#00FF00>OFF</font>"; - httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n"; + httpContent += "<pre>\r\nHeating:\t<font color=#999999>Off</font>"; + httpContent += " <a href=\"./?sw=1\"><button>Turn on</button></a>\r\n"; } - httpContent += " <a href=\".\">[refresh status]</a>\r\n"; httpContent += "</pre>\r\n"; httpContent += "<hr>\r\n"; + httpContent += "<pre>2017 ARMmbed</pre>"; return httpContent; } @@ -232,7 +241,6 @@ while(serverIsListening) { if(server.accept(client) >= 0) { char buf[1024] = { }; - size_t size = 0; serial.printf("Client connected!\n\rIP: %s\n\r", client.get_address()); @@ -246,8 +254,6 @@ break; default: - size = strlen(buf); - string received((char*)buf); if(received.substr(0, 3) != "GET") { @@ -266,7 +272,7 @@ continue; } - int cmd = analyseGetURL(received); + int cmd = analyseURL(received); if(cmd == -2) { @@ -285,16 +291,16 @@ continue; } - if(cmd == 1) { - sw = 1; // switch on + if(cmd == ON) { + sw = ON; // turn the switch on } - if(cmd == 0) { - sw = 0; // switch off + if(cmd == OFF) { + sw = OFF; // turn the switch off } httpHeader = HTTP_OK; - sendHTTP(client, httpHeader, httpPage(sw)); + sendHTTP(client, httpHeader, showWebPage(sw)); } closeClient(); }
diff -r 458d9d7b5c1b -r 3d74cb156c5c mbed.bld --- a/mbed.bld Mon Feb 08 22:52:27 2016 +0000 +++ b/mbed.bld Mon May 01 20:04:30 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/f141b2784e32 \ No newline at end of file +https://mbed.org/users/mbed_official/code/mbed/builds/794e51388b66 \ No newline at end of file