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:
- 2:4a51bdd24745
- Parent:
- 1:c024e74b3a75
--- a/main.cpp Fri Apr 14 07:17:03 2017 +0000 +++ b/main.cpp Mon May 01 20:03:14 2017 +0000 @@ -19,6 +19,7 @@ const int ON = 1; DigitalOut sw(LED1); +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"; @@ -26,10 +27,11 @@ const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized"; string httpHeader; // HTTP header 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. + * Modify the mac array items as needed. * @param * @retval */ @@ -103,24 +105,26 @@ * @retval */ string& showWebPage(uint8_t status) { - httpContent = "<h2>WebSwitch - Smart Home</h2>\r\n"; + char roomTempStr[5]; - httpContent += "<pre>Temperature:\t21.8°C\r\n</pre>"; + //roomTemp = ds1820.read(); + sprintf(roomTempStr, "%3.1f", roomTemp); + + 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\">[Turn off]</a>\r\n"; + 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\nHeating:\t<font color=#BBBBBB>OFF</font>"; - httpContent += " <a href=\"./?sw=1\">[Turn 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 += " \r\n"; - // httpContent += " <a href=\".\">Refresh status]</a>\r\n"; httpContent += "</pre>\r\n"; httpContent += "<hr>\r\n"; - httpContent += "<pre>2017 ARMmbed Open Source</pre>"; + httpContent += "<pre>2017 ARMmbed</pre>"; return httpContent; } @@ -157,7 +161,7 @@ //setup ethernet interface ethernet.init(); //Use DHCP - //ethernet.init("192.168.1.36", "255.255.255.0", "192.168.1.1"); // Use static IP + //ethernet.init("192.168.1.181", "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", ethernet.getIPAddress(), PASSWORD.c_str()); printf("NOTE: Don't forget to type the last '/'.\r\n\r\n"); @@ -259,3 +263,4 @@ } } } +