
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
main.cpp@6:3d74cb156c5c, 2017-05-01 (annotated)
- Committer:
- hudakz
- Date:
- Mon May 01 20:04:30 2017 +0000
- Revision:
- 6:3d74cb156c5c
- Parent:
- 5:458d9d7b5c1b
Button added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 2:3803ea61daa1 | 1 | /* |
hudakz | 3:d0e3d1bd73e6 | 2 | * In this example LED1 on the mbed board is turned on/off using a web browser. |
hudakz | 1:343d3c7e13b8 | 3 | * However, you can easily modify the project to remotely switch on/off any external device. |
hudakz | 0:5c8e08cf3b33 | 4 | * The HTTP server is built from an mbed board and a WIZ550io board. |
hudakz | 0:5c8e08cf3b33 | 5 | * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>. |
hudakz | 5:458d9d7b5c1b | 6 | * For more details see <https://developer.mbed.org/users/hudakz/code/WebSwitch_WIZ550io/wiki/Homepage> |
hudakz | 4:31010e482971 | 7 | * Thanks to Jozsef Voros it works now also with the W5500 modules without a built-in MAC |
hudakz | 3:d0e3d1bd73e6 | 8 | * See below how to enable that. |
hudakz | 0:5c8e08cf3b33 | 9 | */ |
hudakz | 0:5c8e08cf3b33 | 10 | #include "mbed.h" |
hudakz | 0:5c8e08cf3b33 | 11 | #include "EthernetInterface.h" |
hudakz | 0:5c8e08cf3b33 | 12 | #include <string> |
hudakz | 0:5c8e08cf3b33 | 13 | |
hudakz | 0:5c8e08cf3b33 | 14 | using namespace std; |
hudakz | 0:5c8e08cf3b33 | 15 | |
hudakz | 6:3d74cb156c5c | 16 | const int OFF = 0; |
hudakz | 6:3d74cb156c5c | 17 | const int ON = 1; |
hudakz | 6:3d74cb156c5c | 18 | |
hudakz | 0:5c8e08cf3b33 | 19 | Serial serial(USBTX, USBRX); |
hudakz | 0:5c8e08cf3b33 | 20 | |
hudakz | 0:5c8e08cf3b33 | 21 | #if defined(TARGET_LPC1768) |
hudakz | 1:343d3c7e13b8 | 22 | EthernetInterface eth(p11, p12, p13, p8, p14); // MOSI, MISO, SCK, CS, RESET |
hudakz | 0:5c8e08cf3b33 | 23 | #elif defined(TARGET_LPC11U24) |
hudakz | 1:343d3c7e13b8 | 24 | EthernetInterface eth(P16, P15, P13, P17, P18); // MOSI, MISO, SCK, CS, RESET |
hudakz | 3:d0e3d1bd73e6 | 25 | #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \ |
hudakz | 3:d0e3d1bd73e6 | 26 | || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \ |
hudakz | 3:d0e3d1bd73e6 | 27 | || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \ |
hudakz | 3:d0e3d1bd73e6 | 28 | || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \ |
hudakz | 3:d0e3d1bd73e6 | 29 | || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \ |
hudakz | 3:d0e3d1bd73e6 | 30 | || defined(TARGET_K20D50M) || defined(TARGET_K22F) \ |
hudakz | 3:d0e3d1bd73e6 | 31 | || defined(TARGET_NRF51822) \ |
hudakz | 3:d0e3d1bd73e6 | 32 | || defined(TARGET_RZ_A1H) |
hudakz | 3:d0e3d1bd73e6 | 33 | EthernetInterface eth(D4, D5, D3, D6, D7); // MOSI, MISO, SCK, CS, RESET |
hudakz | 0:5c8e08cf3b33 | 34 | |
hudakz | 0:5c8e08cf3b33 | 35 | // If your board/plaform is not present yet then uncomment |
hudakz | 1:343d3c7e13b8 | 36 | // the following two lines and replace modify as appropriate. |
hudakz | 0:5c8e08cf3b33 | 37 | |
hudakz | 0:5c8e08cf3b33 | 38 | //#elif defined(TARGET_YOUR_BOARD) |
hudakz | 1:343d3c7e13b8 | 39 | //EthernetInterface eth(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, RESET); // MOSI, MISO, SCK, CS, RESET |
hudakz | 0:5c8e08cf3b33 | 40 | #endif |
hudakz | 0:5c8e08cf3b33 | 41 | |
hudakz | 0:5c8e08cf3b33 | 42 | // Note: |
hudakz | 0:5c8e08cf3b33 | 43 | // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS or RESET pins collide with LED1 pin |
hudakz | 0:5c8e08cf3b33 | 44 | // then either use different SPI port (if available on the board) and change the pin names |
hudakz | 0:5c8e08cf3b33 | 45 | // in the constructor spi(...) accordingly or instead of using LED1 pin, select |
hudakz | 0:5c8e08cf3b33 | 46 | // a free pin (not used by SPI port) and connect to it an external LED which is connected |
hudakz | 0:5c8e08cf3b33 | 47 | // to a 220 Ohm resitor that is connected to the groud. |
hudakz | 0:5c8e08cf3b33 | 48 | // In the second case remember to replace LED1 in sw(LED1) constructor (see below). |
hudakz | 0:5c8e08cf3b33 | 49 | // IP address must be also unique and compatible with your network. Change as appropriate. |
hudakz | 3:d0e3d1bd73e6 | 50 | |
hudakz | 5:458d9d7b5c1b | 51 | // If instead of WIZ550io you'd like to use a W5500 module without a built-in MAC please uncommend the following line |
hudakz | 5:458d9d7b5c1b | 52 | //#define W5500 1 |
hudakz | 3:d0e3d1bd73e6 | 53 | |
hudakz | 4:31010e482971 | 54 | #if defined(W5500) |
hudakz | 3:d0e3d1bd73e6 | 55 | // The MAC number must be unique within the connected network. Modify as appropriate. |
hudakz | 3:d0e3d1bd73e6 | 56 | uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; |
hudakz | 3:d0e3d1bd73e6 | 57 | #endif |
hudakz | 3:d0e3d1bd73e6 | 58 | |
hudakz | 0:5c8e08cf3b33 | 59 | const char MY_IP[] = "192.168.1.181"; |
hudakz | 0:5c8e08cf3b33 | 60 | const char MY_NETMASK[] = "255.255.255.0"; |
hudakz | 0:5c8e08cf3b33 | 61 | const char MY_GATEWAY[] = "192.168.1.1"; |
hudakz | 0:5c8e08cf3b33 | 62 | int MY_PORT = 80; |
hudakz | 0:5c8e08cf3b33 | 63 | |
hudakz | 0:5c8e08cf3b33 | 64 | TCPSocketServer server; |
hudakz | 0:5c8e08cf3b33 | 65 | TCPSocketConnection client; |
hudakz | 0:5c8e08cf3b33 | 66 | bool serverIsListening = false; |
hudakz | 0:5c8e08cf3b33 | 67 | |
hudakz | 0:5c8e08cf3b33 | 68 | DigitalOut sw(LED1); // Change LED1 to a pin of your choice. |
hudakz | 0:5c8e08cf3b33 | 69 | // However, make sure that it does not collide with any of the SPI pins |
hudakz | 6:3d74cb156c5c | 70 | float roomTemp = 21.8; // A temperature sensor output |
hudakz | 0:5c8e08cf3b33 | 71 | |
hudakz | 0:5c8e08cf3b33 | 72 | const string PASSWORD = "secret"; // change as you like |
hudakz | 0:5c8e08cf3b33 | 73 | const string HTTP_OK = "HTTP/1.0 200 OK"; |
hudakz | 5:458d9d7b5c1b | 74 | const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: "; |
hudakz | 0:5c8e08cf3b33 | 75 | const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized"; |
hudakz | 0:5c8e08cf3b33 | 76 | |
hudakz | 0:5c8e08cf3b33 | 77 | string httpHeader; // HTTP header |
hudakz | 0:5c8e08cf3b33 | 78 | string httpContent; // HTTP content |
hudakz | 0:5c8e08cf3b33 | 79 | |
hudakz | 6:3d74cb156c5c | 80 | /** |
hudakz | 6:3d74cb156c5c | 81 | * @brief Analyses the received URL |
hudakz | 6:3d74cb156c5c | 82 | * @note The string passed to this function will look like this: |
hudakz | 6:3d74cb156c5c | 83 | * GET /password HTTP/1..... |
hudakz | 6:3d74cb156c5c | 84 | * GET /password/ HTTP/1..... |
hudakz | 6:3d74cb156c5c | 85 | * GET /password/?sw=1 HTTP/1..... |
hudakz | 6:3d74cb156c5c | 86 | * GET /password/?sw=0 HTTP/1..... |
hudakz | 6:3d74cb156c5c | 87 | * @param url URL string |
hudakz | 6:3d74cb156c5c | 88 | * @retval -1 invalid password |
hudakz | 6:3d74cb156c5c | 89 | * -2 no command given but password valid |
hudakz | 6:3d74cb156c5c | 90 | * -3 just refresh page |
hudakz | 6:3d74cb156c5c | 91 | * 0 switch off |
hudakz | 6:3d74cb156c5c | 92 | * 1 switch on |
hudakz | 6:3d74cb156c5c | 93 | */ |
hudakz | 6:3d74cb156c5c | 94 | int8_t analyseURL(string& url) { |
hudakz | 6:3d74cb156c5c | 95 | if(url.substr(5, PASSWORD.size()) != PASSWORD) |
hudakz | 0:5c8e08cf3b33 | 96 | return(-1); |
hudakz | 0:5c8e08cf3b33 | 97 | |
hudakz | 0:5c8e08cf3b33 | 98 | uint8_t pos = 5 + PASSWORD.size(); |
hudakz | 0:5c8e08cf3b33 | 99 | |
hudakz | 6:3d74cb156c5c | 100 | if(url.substr(pos, 1) == " ") |
hudakz | 0:5c8e08cf3b33 | 101 | return(-2); |
hudakz | 0:5c8e08cf3b33 | 102 | |
hudakz | 6:3d74cb156c5c | 103 | if(url.substr(pos++, 1) != "/") |
hudakz | 0:5c8e08cf3b33 | 104 | return(-1); |
hudakz | 0:5c8e08cf3b33 | 105 | |
hudakz | 6:3d74cb156c5c | 106 | string cmd(url.substr(pos, 5)); |
hudakz | 0:5c8e08cf3b33 | 107 | |
hudakz | 0:5c8e08cf3b33 | 108 | if(cmd == "?sw=0") |
hudakz | 6:3d74cb156c5c | 109 | return(OFF); |
hudakz | 0:5c8e08cf3b33 | 110 | |
hudakz | 0:5c8e08cf3b33 | 111 | if(cmd == "?sw=1") |
hudakz | 6:3d74cb156c5c | 112 | return(ON); |
hudakz | 0:5c8e08cf3b33 | 113 | |
hudakz | 0:5c8e08cf3b33 | 114 | return(-3); |
hudakz | 0:5c8e08cf3b33 | 115 | } |
hudakz | 0:5c8e08cf3b33 | 116 | |
hudakz | 0:5c8e08cf3b33 | 117 | /** |
hudakz | 0:5c8e08cf3b33 | 118 | * @brief |
hudakz | 0:5c8e08cf3b33 | 119 | * @note |
hudakz | 0:5c8e08cf3b33 | 120 | * @param |
hudakz | 0:5c8e08cf3b33 | 121 | * @retval |
hudakz | 0:5c8e08cf3b33 | 122 | */ |
hudakz | 4:31010e482971 | 123 | string& movedPermanently(uint8_t flag) { |
hudakz | 0:5c8e08cf3b33 | 124 | if(flag == 1) |
hudakz | 0:5c8e08cf3b33 | 125 | httpContent = "/" + PASSWORD + "/"; |
hudakz | 0:5c8e08cf3b33 | 126 | else |
hudakz | 0:5c8e08cf3b33 | 127 | httpContent = ""; |
hudakz | 0:5c8e08cf3b33 | 128 | |
hudakz | 0:5c8e08cf3b33 | 129 | httpContent += "<h1>301 Moved Permanently</h1>\r\n"; |
hudakz | 0:5c8e08cf3b33 | 130 | |
hudakz | 0:5c8e08cf3b33 | 131 | return(httpContent); |
hudakz | 0:5c8e08cf3b33 | 132 | } |
hudakz | 0:5c8e08cf3b33 | 133 | |
hudakz | 0:5c8e08cf3b33 | 134 | /** |
hudakz | 0:5c8e08cf3b33 | 135 | * @brief |
hudakz | 0:5c8e08cf3b33 | 136 | * @note |
hudakz | 0:5c8e08cf3b33 | 137 | * @param |
hudakz | 0:5c8e08cf3b33 | 138 | * @retval |
hudakz | 0:5c8e08cf3b33 | 139 | */ |
hudakz | 6:3d74cb156c5c | 140 | string& showWebPage(uint8_t status) { |
hudakz | 6:3d74cb156c5c | 141 | char roomTempStr[5]; |
hudakz | 6:3d74cb156c5c | 142 | |
hudakz | 6:3d74cb156c5c | 143 | //roomTemp = ds1820.read(); |
hudakz | 6:3d74cb156c5c | 144 | sprintf(roomTempStr, "%3.1f", roomTemp); |
hudakz | 0:5c8e08cf3b33 | 145 | |
hudakz | 6:3d74cb156c5c | 146 | httpContent = "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>"; |
hudakz | 6:3d74cb156c5c | 147 | httpContent += "<pre>Temperature:\t" + string(roomTempStr) + "°C\r\n</pre>"; |
hudakz | 6:3d74cb156c5c | 148 | |
hudakz | 6:3d74cb156c5c | 149 | if(status == ON) { |
hudakz | 6:3d74cb156c5c | 150 | httpContent += "<pre>\r\nHeating:\t<font color=#FF0000>On </font>"; |
hudakz | 6:3d74cb156c5c | 151 | httpContent += " <a href=\"./?sw=0\"><button>Turn off</button></a>\r\n"; |
hudakz | 0:5c8e08cf3b33 | 152 | } |
hudakz | 0:5c8e08cf3b33 | 153 | else { |
hudakz | 6:3d74cb156c5c | 154 | httpContent += "<pre>\r\nHeating:\t<font color=#999999>Off</font>"; |
hudakz | 6:3d74cb156c5c | 155 | httpContent += " <a href=\"./?sw=1\"><button>Turn on</button></a>\r\n"; |
hudakz | 0:5c8e08cf3b33 | 156 | } |
hudakz | 0:5c8e08cf3b33 | 157 | |
hudakz | 0:5c8e08cf3b33 | 158 | httpContent += "</pre>\r\n"; |
hudakz | 0:5c8e08cf3b33 | 159 | httpContent += "<hr>\r\n"; |
hudakz | 6:3d74cb156c5c | 160 | httpContent += "<pre>2017 ARMmbed</pre>"; |
hudakz | 0:5c8e08cf3b33 | 161 | return httpContent; |
hudakz | 0:5c8e08cf3b33 | 162 | } |
hudakz | 0:5c8e08cf3b33 | 163 | |
hudakz | 0:5c8e08cf3b33 | 164 | /** |
hudakz | 0:5c8e08cf3b33 | 165 | * @brief |
hudakz | 0:5c8e08cf3b33 | 166 | * @note |
hudakz | 0:5c8e08cf3b33 | 167 | * @param |
hudakz | 0:5c8e08cf3b33 | 168 | * @retval |
hudakz | 0:5c8e08cf3b33 | 169 | */ |
hudakz | 4:31010e482971 | 170 | void sendHTTP(TCPSocketConnection& client, string& header, string& content) { |
hudakz | 5:458d9d7b5c1b | 171 | char contentLeght[5] = {}; |
hudakz | 0:5c8e08cf3b33 | 172 | |
hudakz | 0:5c8e08cf3b33 | 173 | header += "\r\nContent-Type: text/html\r\n"; |
hudakz | 0:5c8e08cf3b33 | 174 | header += "Content-Length: "; |
hudakz | 4:31010e482971 | 175 | sprintf(contentLeght, "%d", content.length()); |
hudakz | 4:31010e482971 | 176 | header += string(contentLeght) + "\r\n"; |
hudakz | 0:5c8e08cf3b33 | 177 | header += "Pragma: no-cache\r\n"; |
hudakz | 0:5c8e08cf3b33 | 178 | header += "Connection: About to close\r\n"; |
hudakz | 0:5c8e08cf3b33 | 179 | header += "\r\n"; |
hudakz | 0:5c8e08cf3b33 | 180 | |
hudakz | 0:5c8e08cf3b33 | 181 | string webpage = header + content; |
hudakz | 5:458d9d7b5c1b | 182 | client.send(const_cast<char*>(webpage.c_str()), webpage.length()); |
hudakz | 0:5c8e08cf3b33 | 183 | } |
hudakz | 0:5c8e08cf3b33 | 184 | |
hudakz | 0:5c8e08cf3b33 | 185 | /** |
hudakz | 0:5c8e08cf3b33 | 186 | * @brief |
hudakz | 0:5c8e08cf3b33 | 187 | * @note |
hudakz | 0:5c8e08cf3b33 | 188 | * @param |
hudakz | 0:5c8e08cf3b33 | 189 | * @retval |
hudakz | 0:5c8e08cf3b33 | 190 | */ |
hudakz | 4:31010e482971 | 191 | void closeClient(void) { |
hudakz | 4:31010e482971 | 192 | client.close(); |
hudakz | 4:31010e482971 | 193 | serial.printf("Connection closed.\n\rTCP server is listening...\n\r"); |
hudakz | 4:31010e482971 | 194 | } |
hudakz | 4:31010e482971 | 195 | |
hudakz | 4:31010e482971 | 196 | /** |
hudakz | 4:31010e482971 | 197 | * @brief |
hudakz | 4:31010e482971 | 198 | * @note |
hudakz | 4:31010e482971 | 199 | * @param |
hudakz | 4:31010e482971 | 200 | * @retval |
hudakz | 4:31010e482971 | 201 | */ |
hudakz | 0:5c8e08cf3b33 | 202 | int main(void) { |
hudakz | 4:31010e482971 | 203 | #if defined(W5500) |
hudakz | 3:d0e3d1bd73e6 | 204 | int ret = eth.init(MY_MAC, MY_IP, MY_NETMASK, MY_GATEWAY); |
hudakz | 3:d0e3d1bd73e6 | 205 | #else |
hudakz | 0:5c8e08cf3b33 | 206 | int ret = eth.init(MY_IP, MY_NETMASK, MY_GATEWAY); |
hudakz | 3:d0e3d1bd73e6 | 207 | #endif |
hudakz | 0:5c8e08cf3b33 | 208 | if(!ret) { |
hudakz | 0:5c8e08cf3b33 | 209 | serial.printf("Initialized, MY_MAC: %s\n", eth.getMACAddress()); |
hudakz | 0:5c8e08cf3b33 | 210 | serial.printf |
hudakz | 0:5c8e08cf3b33 | 211 | ( |
hudakz | 0:5c8e08cf3b33 | 212 | "Connected, MY_IP: %s, MY_NETMASK: %s, MY_GATEWAY: %s\n", |
hudakz | 0:5c8e08cf3b33 | 213 | eth.getIPAddress(), |
hudakz | 0:5c8e08cf3b33 | 214 | eth.getNetworkMask(), |
hudakz | 0:5c8e08cf3b33 | 215 | eth.getGateway() |
hudakz | 0:5c8e08cf3b33 | 216 | ); |
hudakz | 0:5c8e08cf3b33 | 217 | } |
hudakz | 0:5c8e08cf3b33 | 218 | else { |
hudakz | 0:5c8e08cf3b33 | 219 | serial.printf("Error eth.init() - ret = %d\n", ret); |
hudakz | 0:5c8e08cf3b33 | 220 | return -1; |
hudakz | 0:5c8e08cf3b33 | 221 | } |
hudakz | 0:5c8e08cf3b33 | 222 | |
hudakz | 0:5c8e08cf3b33 | 223 | //setup tcp socket |
hudakz | 0:5c8e08cf3b33 | 224 | if(server.bind(MY_PORT) < 0) { |
hudakz | 0:5c8e08cf3b33 | 225 | serial.printf("TCP server bind failed.\n\r"); |
hudakz | 0:5c8e08cf3b33 | 226 | return -1; |
hudakz | 0:5c8e08cf3b33 | 227 | } |
hudakz | 0:5c8e08cf3b33 | 228 | else { |
hudakz | 0:5c8e08cf3b33 | 229 | serial.printf("TCP server bind succeeded.\n\r"); |
hudakz | 0:5c8e08cf3b33 | 230 | serverIsListening = true; |
hudakz | 0:5c8e08cf3b33 | 231 | } |
hudakz | 0:5c8e08cf3b33 | 232 | |
hudakz | 0:5c8e08cf3b33 | 233 | if(server.listen(1) < 0) { |
hudakz | 0:5c8e08cf3b33 | 234 | serial.printf("TCP server listen failed.\n\r"); |
hudakz | 0:5c8e08cf3b33 | 235 | return -1; |
hudakz | 0:5c8e08cf3b33 | 236 | } |
hudakz | 0:5c8e08cf3b33 | 237 | else { |
hudakz | 0:5c8e08cf3b33 | 238 | serial.printf("TCP server is listening...\n\r"); |
hudakz | 0:5c8e08cf3b33 | 239 | } |
hudakz | 0:5c8e08cf3b33 | 240 | |
hudakz | 0:5c8e08cf3b33 | 241 | while(serverIsListening) { |
hudakz | 0:5c8e08cf3b33 | 242 | if(server.accept(client) >= 0) { |
hudakz | 0:5c8e08cf3b33 | 243 | char buf[1024] = { }; |
hudakz | 0:5c8e08cf3b33 | 244 | |
hudakz | 0:5c8e08cf3b33 | 245 | serial.printf("Client connected!\n\rIP: %s\n\r", client.get_address()); |
hudakz | 0:5c8e08cf3b33 | 246 | |
hudakz | 0:5c8e08cf3b33 | 247 | switch(client.receive(buf, 1023)) { |
hudakz | 0:5c8e08cf3b33 | 248 | case 0: |
hudakz | 0:5c8e08cf3b33 | 249 | serial.printf("Recieved buffer is empty.\n\r"); |
hudakz | 0:5c8e08cf3b33 | 250 | break; |
hudakz | 0:5c8e08cf3b33 | 251 | |
hudakz | 0:5c8e08cf3b33 | 252 | case -1: |
hudakz | 0:5c8e08cf3b33 | 253 | serial.printf("Failed to read data from client.\n\r"); |
hudakz | 0:5c8e08cf3b33 | 254 | break; |
hudakz | 0:5c8e08cf3b33 | 255 | |
hudakz | 0:5c8e08cf3b33 | 256 | default: |
hudakz | 0:5c8e08cf3b33 | 257 | string received((char*)buf); |
hudakz | 0:5c8e08cf3b33 | 258 | |
hudakz | 0:5c8e08cf3b33 | 259 | if(received.substr(0, 3) != "GET") { |
hudakz | 0:5c8e08cf3b33 | 260 | httpHeader = HTTP_OK; |
hudakz | 0:5c8e08cf3b33 | 261 | httpContent = "<h1>200 OK</h1>"; |
hudakz | 4:31010e482971 | 262 | sendHTTP(client, httpHeader, httpContent); |
hudakz | 4:31010e482971 | 263 | closeClient(); |
hudakz | 0:5c8e08cf3b33 | 264 | continue; |
hudakz | 0:5c8e08cf3b33 | 265 | } |
hudakz | 0:5c8e08cf3b33 | 266 | |
hudakz | 0:5c8e08cf3b33 | 267 | if(received.substr(0, 6) == "GET / ") { |
hudakz | 0:5c8e08cf3b33 | 268 | httpHeader = HTTP_OK; |
hudakz | 0:5c8e08cf3b33 | 269 | httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n"; |
hudakz | 4:31010e482971 | 270 | sendHTTP(client, httpHeader, httpContent); |
hudakz | 4:31010e482971 | 271 | closeClient(); |
hudakz | 0:5c8e08cf3b33 | 272 | continue; |
hudakz | 0:5c8e08cf3b33 | 273 | } |
hudakz | 0:5c8e08cf3b33 | 274 | |
hudakz | 6:3d74cb156c5c | 275 | int cmd = analyseURL(received); |
hudakz | 0:5c8e08cf3b33 | 276 | |
hudakz | 0:5c8e08cf3b33 | 277 | if(cmd == -2) { |
hudakz | 0:5c8e08cf3b33 | 278 | |
hudakz | 0:5c8e08cf3b33 | 279 | // redirect to the right base url |
hudakz | 5:458d9d7b5c1b | 280 | httpHeader = MOVED_PERM; |
hudakz | 4:31010e482971 | 281 | sendHTTP(client, httpHeader, movedPermanently(1)); |
hudakz | 4:31010e482971 | 282 | closeClient(); |
hudakz | 0:5c8e08cf3b33 | 283 | continue; |
hudakz | 0:5c8e08cf3b33 | 284 | } |
hudakz | 0:5c8e08cf3b33 | 285 | |
hudakz | 0:5c8e08cf3b33 | 286 | if(cmd == -1) { |
hudakz | 0:5c8e08cf3b33 | 287 | httpHeader = UNAUTHORIZED; |
hudakz | 0:5c8e08cf3b33 | 288 | httpContent = "<h1>401 Unauthorized</h1>"; |
hudakz | 4:31010e482971 | 289 | sendHTTP(client, httpHeader, httpContent); |
hudakz | 4:31010e482971 | 290 | closeClient(); |
hudakz | 0:5c8e08cf3b33 | 291 | continue; |
hudakz | 0:5c8e08cf3b33 | 292 | } |
hudakz | 0:5c8e08cf3b33 | 293 | |
hudakz | 6:3d74cb156c5c | 294 | if(cmd == ON) { |
hudakz | 6:3d74cb156c5c | 295 | sw = ON; // turn the switch on |
hudakz | 0:5c8e08cf3b33 | 296 | } |
hudakz | 0:5c8e08cf3b33 | 297 | |
hudakz | 6:3d74cb156c5c | 298 | if(cmd == OFF) { |
hudakz | 6:3d74cb156c5c | 299 | sw = OFF; // turn the switch off |
hudakz | 0:5c8e08cf3b33 | 300 | } |
hudakz | 0:5c8e08cf3b33 | 301 | |
hudakz | 0:5c8e08cf3b33 | 302 | httpHeader = HTTP_OK; |
hudakz | 6:3d74cb156c5c | 303 | sendHTTP(client, httpHeader, showWebPage(sw)); |
hudakz | 0:5c8e08cf3b33 | 304 | } |
hudakz | 4:31010e482971 | 305 | closeClient(); |
hudakz | 0:5c8e08cf3b33 | 306 | } |
hudakz | 0:5c8e08cf3b33 | 307 | } |
hudakz | 0:5c8e08cf3b33 | 308 | } |