Search code examples
arduinoesp8266arduino-ideesp32arduino-esp8266

connecting Esp8266 client to sub-directory of a local web server


I am going to connect Esp8266 client to local web server. Here I am connecting to a sub-directory inside local web server. In a browser It's easy with just typing: 192.168.1.103/public_html/register.php When it comes to esp8266, I use the following function: client.connect(host, 80). In this function, host can be either an array of IP address or a URL. Giving host a value of 192.168.1.103/public_html/register.php doesn't cause the esp8266 client to connect to the server sub-directory. However it has no problem with connecting to the IP address when I give the host the value of 192.168.1.103 (Server IP). I appreciate if you give me help finding a way to fix it.


Solution

  • The WiFiClient of ESP8266WiFi library wraps a TCP socket. A TCP socket connects to IP address and port where a server side socket is created. After the sockets are connected, any data can be send and received over it both ways.

    The HTTP communication protocol is an application layer communication protocol usually running over a TCP socket connection. It makes a HTTP server understand the request from a HTTP client and the client to understand the response.

    To make HTTP communication, send a valid HTTP request to the server over TCP socket (WiFiClient). Or use HTTPClient to handle the HTTP protocol for you.

    Example of HTTP GET request with WiFiClient:

      if (client.connect(server, 80)) {
        client.println("GET /public_html/register.php HTTP/1.1");
        client.print("Host: ");
        client.println(server);
        client.println("Connection: close");
        client.println();
        client.flush();
      }