A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.

Dependencies:   UIPEthernet

Committer:
hudakz
Date:
Sat Sep 07 17:58:35 2019 +0000
Revision:
9:66b6c19e9bb7
Parent:
8:9e3b50f6dc81
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 2:519b6ae198ae 1 /*
hudakz 0:8b40576553d2 2 * In this example the HTTP request (text) received from a browser is echoed (sent back) to the browser.
hudakz 3:f3bfd257e138 3 * Ethernet connection is via an ENC28J60 Ethernet board driven by the UIPEthernet library
hudakz 0:8b40576553d2 4 */
hudakz 0:8b40576553d2 5 #include "mbed.h"
hudakz 7:f6058bcaa614 6 #include "UipEthernet.h"
hudakz 6:c5eb31c60c8f 7
hudakz 7:f6058bcaa614 8 #define IP "192.168.1.35"
hudakz 7:f6058bcaa614 9 #define GATEWAY "192.168.1.1"
hudakz 7:f6058bcaa614 10 #define NETMASK "255.255.255.0"
hudakz 7:f6058bcaa614 11 #define PORT 80
hudakz 2:519b6ae198ae 12
hudakz 7:f6058bcaa614 13 const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
hudakz 7:f6058bcaa614 14 UipEthernet net(MAC, D11, D12, D13, D10); // mac, mosi, miso, sck, cs
hudakz 7:f6058bcaa614 15 TcpServer server; // Ethernet server
hudakz 7:f6058bcaa614 16 TcpClient* client;
hudakz 7:f6058bcaa614 17 char receiveBuf[1024];
hudakz 7:f6058bcaa614 18 char echoHeader[256];
hudakz 7:f6058bcaa614 19 /**
hudakz 7:f6058bcaa614 20 * @brief
hudakz 7:f6058bcaa614 21 * @note
hudakz 7:f6058bcaa614 22 * @param
hudakz 7:f6058bcaa614 23 * @retval
hudakz 7:f6058bcaa614 24 */
hudakz 6:c5eb31c60c8f 25 int main(void)
hudakz 6:c5eb31c60c8f 26 {
hudakz 7:f6058bcaa614 27 printf("Starting ...\r\n");
hudakz 7:f6058bcaa614 28
hudakz 7:f6058bcaa614 29 //net.set_network(IP, NETMASK, GATEWAY); // include this to use static IP address
hudakz 7:f6058bcaa614 30 net.connect();
hudakz 7:f6058bcaa614 31
hudakz 7:f6058bcaa614 32 // Show the network address
hudakz 7:f6058bcaa614 33 const char* ip = net.get_ip_address();
hudakz 7:f6058bcaa614 34 const char* netmask = net.get_netmask();
hudakz 7:f6058bcaa614 35 const char* gateway = net.get_gateway();
hudakz 6:c5eb31c60c8f 36
hudakz 7:f6058bcaa614 37 printf("IP address: %s\r\n", ip ? ip : "None");
hudakz 7:f6058bcaa614 38 printf("Netmask: %s\r\n", netmask ? netmask : "None");
hudakz 7:f6058bcaa614 39 printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None");
hudakz 8:9e3b50f6dc81 40 printf("------------------------------------------------------\r\n");
hudakz 8:9e3b50f6dc81 41 printf("Usage: Type %s into your web browser and hit ENTER\r\n", net.get_ip_address());
hudakz 8:9e3b50f6dc81 42 printf("------------------------------------------------------\r\n");
hudakz 2:519b6ae198ae 43
hudakz 7:f6058bcaa614 44 /* Open the server on ethernet stack */
hudakz 7:f6058bcaa614 45 server.open(&net);
hudakz 7:f6058bcaa614 46
hudakz 7:f6058bcaa614 47 /* Bind the HTTP port (TCP 80) to the server */
hudakz 7:f6058bcaa614 48 server.bind(PORT);
hudakz 7:f6058bcaa614 49
hudakz 7:f6058bcaa614 50 /* Can handle 5 simultaneous connections */
hudakz 7:f6058bcaa614 51 server.listen(5);
hudakz 6:c5eb31c60c8f 52
hudakz 6:c5eb31c60c8f 53 while (1) {
hudakz 7:f6058bcaa614 54 client = server.accept();
hudakz 6:c5eb31c60c8f 55
hudakz 6:c5eb31c60c8f 56 if (client) {
hudakz 7:f6058bcaa614 57 size_t receiveLen = client->recv((uint8_t*)receiveBuf, client->available());
hudakz 7:f6058bcaa614 58 if (receiveBuf[0] == 'G' && receiveBuf[1] == 'E' && receiveBuf[2] == 'T') {
hudakz 7:f6058bcaa614 59 printf("\r\n-------------------------------------------------------\r\n");
hudakz 7:f6058bcaa614 60 printf("GET request received from a client with IP address %s\n\r", client->getpeername());
hudakz 7:f6058bcaa614 61 printf("%s\r\n", receiveBuf);
hudakz 7:f6058bcaa614 62 sprintf
hudakz 7:f6058bcaa614 63 (
hudakz 7:f6058bcaa614 64 echoHeader,
hudakz 7:f6058bcaa614 65 "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text\r\nConnection: About to close\r\n\r\n",
hudakz 7:f6058bcaa614 66 receiveLen
hudakz 7:f6058bcaa614 67 );
hudakz 7:f6058bcaa614 68 client->send((uint8_t*)echoHeader, strlen(echoHeader));
hudakz 7:f6058bcaa614 69 client->send((uint8_t*)receiveBuf, receiveLen);
hudakz 7:f6058bcaa614 70 printf("Echo done.\r\n");
hudakz 0:8b40576553d2 71 }
hudakz 8:9e3b50f6dc81 72 client->close();
hudakz 0:8b40576553d2 73 }
hudakz 0:8b40576553d2 74 }
hudakz 0:8b40576553d2 75 }