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.
/media/uploads/hudakz/webswitch_wiz.jpg/media/uploads/hudakz/webswitch_mobile01.jpg

The project was inspired by the Tuxgraphics Web Switch. Thank you Guido!

NOTE:

Committer:
hudakz
Date:
Sun Mar 15 22:47:37 2015 +0000
Revision:
0:5c8e08cf3b33
Child:
1:343d3c7e13b8
Web Switch to turn on/off LED1 using the WIZ550io board. Compile, download, run and type 192.168.1.181/secret/ into your web browser.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:5c8e08cf3b33 1 /* In this example LED1 on the mbed board is switched on/off using a web browser.
hudakz 0:5c8e08cf3b33 2 * The HTTP server is built from an mbed board and a WIZ550io board.
hudakz 0:5c8e08cf3b33 3 * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>.
hudakz 0:5c8e08cf3b33 4 */
hudakz 0:5c8e08cf3b33 5 #include "mbed.h"
hudakz 0:5c8e08cf3b33 6 #include "EthernetInterface.h"
hudakz 0:5c8e08cf3b33 7 #include <string>
hudakz 0:5c8e08cf3b33 8
hudakz 0:5c8e08cf3b33 9 using namespace std;
hudakz 0:5c8e08cf3b33 10
hudakz 0:5c8e08cf3b33 11 Serial serial(USBTX, USBRX);
hudakz 0:5c8e08cf3b33 12
hudakz 0:5c8e08cf3b33 13 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:5c8e08cf3b33 14 #if defined(TARGET_LPC1768)
hudakz 0:5c8e08cf3b33 15 SPI spi(p11, p12, p13); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 16 EthernetInterface eth(&spi, p8, p14); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 17 #elif defined(TARGET_FRDM_KL25Z)
hudakz 0:5c8e08cf3b33 18 SPI spi(PTD2, PTD3, PTD1); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 19 EthernetInterface eth(&spi, PTD0, PTD5); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 20 #elif defined(TARGET_FRDM_KL46Z)
hudakz 0:5c8e08cf3b33 21 SPI spi(PTD2, PTD3, PTD1); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 22 EthernetInterface eth(&spi, PTD0, PTD5); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 23 #elif defined(TARGET_LPC11U24)
hudakz 0:5c8e08cf3b33 24 SPI spi(P16, P15, P13); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 25 EthernetInterface eth(&spi, P17, P18); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 26 #elif defined(TARGET_NUCLEO_F030R8)
hudakz 0:5c8e08cf3b33 27 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 28 EthernetInterface eth(&spi, PB_10, PA_8); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 29 #elif defined(TARGET_NUCLEO_F072RB)
hudakz 0:5c8e08cf3b33 30 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 31 EthernetInterface eth(&spi, PB_10, PA_8); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 32 #elif defined(TARGET_NUCLEO_F103RB)
hudakz 0:5c8e08cf3b33 33 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 34 EthernetInterface eth(&spi, PB_10, PA_8); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 35 #elif defined(TARGET_NUCLEO_F401RE)
hudakz 0:5c8e08cf3b33 36 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 37 EthernetInterface eth(&spi, PB_10, PA_8); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 38 #elif defined(TARGET_NUCLEO_F411RE)
hudakz 0:5c8e08cf3b33 39 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 40 EthernetInterface eth(&spi, PB_10, PA_8); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 41
hudakz 0:5c8e08cf3b33 42 // If your board/plaform is not present yet then uncomment
hudakz 0:5c8e08cf3b33 43 // the following three lines and replace modify as appropriate.
hudakz 0:5c8e08cf3b33 44
hudakz 0:5c8e08cf3b33 45 //#elif defined(TARGET_YOUR_BOARD)
hudakz 0:5c8e08cf3b33 46 //SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // MOSI, MISO, SCK
hudakz 0:5c8e08cf3b33 47 //EthernetInterface eth(&spi, SPI_CS, RESET); // SPI, CS, RESET
hudakz 0:5c8e08cf3b33 48 #endif
hudakz 0:5c8e08cf3b33 49
hudakz 0:5c8e08cf3b33 50 // Note:
hudakz 0:5c8e08cf3b33 51 // 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 52 // then either use different SPI port (if available on the board) and change the pin names
hudakz 0:5c8e08cf3b33 53 // in the constructor spi(...) accordingly or instead of using LED1 pin, select
hudakz 0:5c8e08cf3b33 54 // a free pin (not used by SPI port) and connect to it an external LED which is connected
hudakz 0:5c8e08cf3b33 55 // to a 220 Ohm resitor that is connected to the groud.
hudakz 0:5c8e08cf3b33 56 // In the second case remember to replace LED1 in sw(LED1) constructor (see below).
hudakz 0:5c8e08cf3b33 57 // IP address must be also unique and compatible with your network. Change as appropriate.
hudakz 0:5c8e08cf3b33 58 const char MY_IP[] = "192.168.1.181";
hudakz 0:5c8e08cf3b33 59 const char MY_NETMASK[] = "255.255.255.0";
hudakz 0:5c8e08cf3b33 60 const char MY_GATEWAY[] = "192.168.1.1";
hudakz 0:5c8e08cf3b33 61 int MY_PORT = 80;
hudakz 0:5c8e08cf3b33 62
hudakz 0:5c8e08cf3b33 63 TCPSocketServer server;
hudakz 0:5c8e08cf3b33 64 TCPSocketConnection client;
hudakz 0:5c8e08cf3b33 65 bool serverIsListening = false;
hudakz 0:5c8e08cf3b33 66
hudakz 0:5c8e08cf3b33 67 DigitalOut sw(LED1); // Change LED1 to a pin of your choice.
hudakz 0:5c8e08cf3b33 68 // However, make sure that it does not collide with any of the SPI pins
hudakz 0:5c8e08cf3b33 69 // already used in the UIPEthernet(...) constructor above!
hudakz 0:5c8e08cf3b33 70
hudakz 0:5c8e08cf3b33 71 const string PASSWORD = "secret"; // change as you like
hudakz 0:5c8e08cf3b33 72 const string HTTP_OK = "HTTP/1.0 200 OK";
hudakz 0:5c8e08cf3b33 73 const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: ";
hudakz 0:5c8e08cf3b33 74 const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized";
hudakz 0:5c8e08cf3b33 75
hudakz 0:5c8e08cf3b33 76 string httpHeader; // HTTP header
hudakz 0:5c8e08cf3b33 77 string httpContent; // HTTP content
hudakz 0:5c8e08cf3b33 78
hudakz 0:5c8e08cf3b33 79 // analyse the url given
hudakz 0:5c8e08cf3b33 80 // return values: -1 invalid password
hudakz 0:5c8e08cf3b33 81 // -2 no command given but password valid
hudakz 0:5c8e08cf3b33 82 // -3 just refresh page
hudakz 0:5c8e08cf3b33 83 // 0 switch off
hudakz 0:5c8e08cf3b33 84 // 1 switch on
hudakz 0:5c8e08cf3b33 85 //
hudakz 0:5c8e08cf3b33 86 // The string passed to this function will look like this:
hudakz 0:5c8e08cf3b33 87 // GET /password HTTP/1.....
hudakz 0:5c8e08cf3b33 88 // GET /password/ HTTP/1.....
hudakz 0:5c8e08cf3b33 89 // GET /password/?sw=1 HTTP/1.....
hudakz 0:5c8e08cf3b33 90
hudakz 0:5c8e08cf3b33 91 // GET /password/?sw=0 HTTP/1.....
hudakz 0:5c8e08cf3b33 92 int8_t analyse_get_url(string& str) {
hudakz 0:5c8e08cf3b33 93 if(str.substr(5, PASSWORD.size()) != PASSWORD)
hudakz 0:5c8e08cf3b33 94 return(-1);
hudakz 0:5c8e08cf3b33 95
hudakz 0:5c8e08cf3b33 96 uint8_t pos = 5 + PASSWORD.size();
hudakz 0:5c8e08cf3b33 97
hudakz 0:5c8e08cf3b33 98 if(str.substr(pos, 1) == " ")
hudakz 0:5c8e08cf3b33 99 return(-2);
hudakz 0:5c8e08cf3b33 100
hudakz 0:5c8e08cf3b33 101 if(str.substr(pos, 1) != "/")
hudakz 0:5c8e08cf3b33 102 return(-1);
hudakz 0:5c8e08cf3b33 103
hudakz 0:5c8e08cf3b33 104 pos++;
hudakz 0:5c8e08cf3b33 105
hudakz 0:5c8e08cf3b33 106 string cmd(str.substr(pos, 5));
hudakz 0:5c8e08cf3b33 107
hudakz 0:5c8e08cf3b33 108 if(cmd == "?sw=0")
hudakz 0:5c8e08cf3b33 109 return(0);
hudakz 0:5c8e08cf3b33 110
hudakz 0:5c8e08cf3b33 111 if(cmd == "?sw=1")
hudakz 0:5c8e08cf3b33 112 return(1);
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 0:5c8e08cf3b33 123 string& moved_perm(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 0:5c8e08cf3b33 140 string& view(uint8_t status) {
hudakz 0:5c8e08cf3b33 141 httpContent = "<h2>Web Switch</h2>\r\n";
hudakz 0:5c8e08cf3b33 142
hudakz 0:5c8e08cf3b33 143 if(status == 1) {
hudakz 0:5c8e08cf3b33 144 httpContent += "<pre>\r\n <font color=#FF0000>ON</font>";
hudakz 0:5c8e08cf3b33 145 httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n";
hudakz 0:5c8e08cf3b33 146 }
hudakz 0:5c8e08cf3b33 147 else {
hudakz 0:5c8e08cf3b33 148 httpContent += "<pre>\r\n <font color=#00FF00>OFF</font>";
hudakz 0:5c8e08cf3b33 149 httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n";
hudakz 0:5c8e08cf3b33 150 }
hudakz 0:5c8e08cf3b33 151
hudakz 0:5c8e08cf3b33 152 httpContent += " <a href=\".\">[refresh status]</a>\r\n";
hudakz 0:5c8e08cf3b33 153 httpContent += "</pre>\r\n";
hudakz 0:5c8e08cf3b33 154 httpContent += "<hr>\r\n";
hudakz 0:5c8e08cf3b33 155 return httpContent;
hudakz 0:5c8e08cf3b33 156 }
hudakz 0:5c8e08cf3b33 157
hudakz 0:5c8e08cf3b33 158 /**
hudakz 0:5c8e08cf3b33 159 * @brief
hudakz 0:5c8e08cf3b33 160 * @note
hudakz 0:5c8e08cf3b33 161 * @param
hudakz 0:5c8e08cf3b33 162 * @retval
hudakz 0:5c8e08cf3b33 163 */
hudakz 0:5c8e08cf3b33 164 void http_send(TCPSocketConnection& client, string& header, string& content) {
hudakz 0:5c8e08cf3b33 165 char content_length[5] = { };
hudakz 0:5c8e08cf3b33 166
hudakz 0:5c8e08cf3b33 167 header += "\r\nContent-Type: text/html\r\n";
hudakz 0:5c8e08cf3b33 168 header += "Content-Length: ";
hudakz 0:5c8e08cf3b33 169 sprintf(content_length, "%d", content.length());
hudakz 0:5c8e08cf3b33 170 header += string(content_length) + "\r\n";
hudakz 0:5c8e08cf3b33 171 header += "Pragma: no-cache\r\n";
hudakz 0:5c8e08cf3b33 172 header += "Connection: About to close\r\n";
hudakz 0:5c8e08cf3b33 173 header += "\r\n";
hudakz 0:5c8e08cf3b33 174
hudakz 0:5c8e08cf3b33 175 string webpage = header + content;
hudakz 0:5c8e08cf3b33 176 client.send (const_cast<char*>(webpage.c_str ()), webpage.length ());
hudakz 0:5c8e08cf3b33 177 }
hudakz 0:5c8e08cf3b33 178
hudakz 0:5c8e08cf3b33 179 /**
hudakz 0:5c8e08cf3b33 180 * @brief
hudakz 0:5c8e08cf3b33 181 * @note
hudakz 0:5c8e08cf3b33 182 * @param
hudakz 0:5c8e08cf3b33 183 * @retval
hudakz 0:5c8e08cf3b33 184 */
hudakz 0:5c8e08cf3b33 185 int main(void) {
hudakz 0:5c8e08cf3b33 186 int ret = eth.init(MY_IP, MY_NETMASK, MY_GATEWAY);
hudakz 0:5c8e08cf3b33 187
hudakz 0:5c8e08cf3b33 188 if(!ret) {
hudakz 0:5c8e08cf3b33 189 serial.printf("Initialized, MY_MAC: %s\n", eth.getMACAddress());
hudakz 0:5c8e08cf3b33 190 serial.printf
hudakz 0:5c8e08cf3b33 191 (
hudakz 0:5c8e08cf3b33 192 "Connected, MY_IP: %s, MY_NETMASK: %s, MY_GATEWAY: %s\n",
hudakz 0:5c8e08cf3b33 193 eth.getIPAddress(),
hudakz 0:5c8e08cf3b33 194 eth.getNetworkMask(),
hudakz 0:5c8e08cf3b33 195 eth.getGateway()
hudakz 0:5c8e08cf3b33 196 );
hudakz 0:5c8e08cf3b33 197 }
hudakz 0:5c8e08cf3b33 198 else {
hudakz 0:5c8e08cf3b33 199 serial.printf("Error eth.init() - ret = %d\n", ret);
hudakz 0:5c8e08cf3b33 200 return -1;
hudakz 0:5c8e08cf3b33 201 }
hudakz 0:5c8e08cf3b33 202
hudakz 0:5c8e08cf3b33 203 //setup tcp socket
hudakz 0:5c8e08cf3b33 204 if(server.bind(MY_PORT) < 0) {
hudakz 0:5c8e08cf3b33 205 serial.printf("TCP server bind failed.\n\r");
hudakz 0:5c8e08cf3b33 206 return -1;
hudakz 0:5c8e08cf3b33 207 }
hudakz 0:5c8e08cf3b33 208 else {
hudakz 0:5c8e08cf3b33 209 serial.printf("TCP server bind succeeded.\n\r");
hudakz 0:5c8e08cf3b33 210 serverIsListening = true;
hudakz 0:5c8e08cf3b33 211 }
hudakz 0:5c8e08cf3b33 212
hudakz 0:5c8e08cf3b33 213 if(server.listen(1) < 0) {
hudakz 0:5c8e08cf3b33 214 serial.printf("TCP server listen failed.\n\r");
hudakz 0:5c8e08cf3b33 215 return -1;
hudakz 0:5c8e08cf3b33 216 }
hudakz 0:5c8e08cf3b33 217 else {
hudakz 0:5c8e08cf3b33 218 serial.printf("TCP server is listening...\n\r");
hudakz 0:5c8e08cf3b33 219 }
hudakz 0:5c8e08cf3b33 220
hudakz 0:5c8e08cf3b33 221 while(serverIsListening) {
hudakz 0:5c8e08cf3b33 222 if(server.accept(client) >= 0) {
hudakz 0:5c8e08cf3b33 223 char buf[1024] = { };
hudakz 0:5c8e08cf3b33 224 size_t size = 0;
hudakz 0:5c8e08cf3b33 225
hudakz 0:5c8e08cf3b33 226 serial.printf("Client connected!\n\rIP: %s\n\r", client.get_address());
hudakz 0:5c8e08cf3b33 227
hudakz 0:5c8e08cf3b33 228 switch(client.receive(buf, 1023)) {
hudakz 0:5c8e08cf3b33 229 case 0:
hudakz 0:5c8e08cf3b33 230 serial.printf("Recieved buffer is empty.\n\r");
hudakz 0:5c8e08cf3b33 231 break;
hudakz 0:5c8e08cf3b33 232
hudakz 0:5c8e08cf3b33 233 case -1:
hudakz 0:5c8e08cf3b33 234 serial.printf("Failed to read data from client.\n\r");
hudakz 0:5c8e08cf3b33 235 break;
hudakz 0:5c8e08cf3b33 236
hudakz 0:5c8e08cf3b33 237 default:
hudakz 0:5c8e08cf3b33 238 size = strlen(buf);
hudakz 0:5c8e08cf3b33 239
hudakz 0:5c8e08cf3b33 240 string received((char*)buf);
hudakz 0:5c8e08cf3b33 241
hudakz 0:5c8e08cf3b33 242 if(received.substr(0, 3) != "GET") {
hudakz 0:5c8e08cf3b33 243 httpHeader = HTTP_OK;
hudakz 0:5c8e08cf3b33 244 httpContent = "<h1>200 OK</h1>";
hudakz 0:5c8e08cf3b33 245 http_send(client, httpHeader, httpContent);
hudakz 0:5c8e08cf3b33 246 continue;
hudakz 0:5c8e08cf3b33 247 }
hudakz 0:5c8e08cf3b33 248
hudakz 0:5c8e08cf3b33 249 if(received.substr(0, 6) == "GET / ") {
hudakz 0:5c8e08cf3b33 250 httpHeader = HTTP_OK;
hudakz 0:5c8e08cf3b33 251 httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n";
hudakz 0:5c8e08cf3b33 252 http_send(client, httpHeader, httpContent);
hudakz 0:5c8e08cf3b33 253 continue;
hudakz 0:5c8e08cf3b33 254 }
hudakz 0:5c8e08cf3b33 255
hudakz 0:5c8e08cf3b33 256 int cmd = analyse_get_url(received);
hudakz 0:5c8e08cf3b33 257
hudakz 0:5c8e08cf3b33 258 if(cmd == -2) {
hudakz 0:5c8e08cf3b33 259
hudakz 0:5c8e08cf3b33 260 // redirect to the right base url
hudakz 0:5c8e08cf3b33 261 httpHeader = MOVED_PERM;
hudakz 0:5c8e08cf3b33 262 http_send(client, httpHeader, moved_perm(1));
hudakz 0:5c8e08cf3b33 263 continue;
hudakz 0:5c8e08cf3b33 264 }
hudakz 0:5c8e08cf3b33 265
hudakz 0:5c8e08cf3b33 266 if(cmd == -1) {
hudakz 0:5c8e08cf3b33 267 httpHeader = UNAUTHORIZED;
hudakz 0:5c8e08cf3b33 268 httpContent = "<h1>401 Unauthorized</h1>";
hudakz 0:5c8e08cf3b33 269 http_send(client, httpHeader, httpContent);
hudakz 0:5c8e08cf3b33 270 continue;
hudakz 0:5c8e08cf3b33 271 }
hudakz 0:5c8e08cf3b33 272
hudakz 0:5c8e08cf3b33 273 if(cmd == 1) {
hudakz 0:5c8e08cf3b33 274 sw = 1; // switch on
hudakz 0:5c8e08cf3b33 275 }
hudakz 0:5c8e08cf3b33 276
hudakz 0:5c8e08cf3b33 277 if(cmd == 0) {
hudakz 0:5c8e08cf3b33 278 sw = 0; // switch off
hudakz 0:5c8e08cf3b33 279 }
hudakz 0:5c8e08cf3b33 280
hudakz 0:5c8e08cf3b33 281 httpHeader = HTTP_OK;
hudakz 0:5c8e08cf3b33 282 http_send(client, httpHeader, view(sw));
hudakz 0:5c8e08cf3b33 283 }
hudakz 0:5c8e08cf3b33 284
hudakz 0:5c8e08cf3b33 285 client.close();
hudakz 0:5c8e08cf3b33 286
hudakz 0:5c8e08cf3b33 287 serial.printf("Connection closed.\n\rTCP server is listening...\n\r");
hudakz 0:5c8e08cf3b33 288 }
hudakz 0:5c8e08cf3b33 289 }
hudakz 0:5c8e08cf3b33 290 }