Question with WifClient example.

I am trying to communicate between two esp8266 and they are made as two servers, when I communicate using the link 192.168.1.200 from my computer it's all okay also the link 192.168.1.200/p2t and this 'p2t' dose a certain thing within the ESP like turning on LED..
now if I try to use the example of WiFiClient to communicate from one ESP to another which has that server, I can only communicate with the link 192.168.1.200 and I fail with the 192.168.1.200/p2t .
I get

connecting to 192.168.1.202/p2t:80
connection failed

when using p2t
and it's good when I communicate with the 192.168.1.200 and I get:

connecting to 192.168.1.202:80
receiving from remote server

closing connection

how can I achieve that?
here is the default code of the example:

#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "*****"
#define STAPSK  "*****"
#endif

const char* ssid     = STASSID;
const char* password = STAPSK;

const char* hostest = "192.168.1.202/p2t";
const uint16_t port = 80;

void setup() {
  Serial.begin(115200);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  static bool wait = false;

  Serial.print("connecting to ");
  Serial.print(hostest);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(hostest, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }


  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  if (wait) {
    delay(3000); // execute once every 5 minutes, don't flood remote service
  }
  wait = true;
}

Juraj:
ESP8266 TCP/IP communication - Arduino Stack Exchange

Thanks for your replay, but I don't know how is that supposed to help me, I said that I can connect to the link 192.168.1.200/p2t when I try to communicate with my laptop or mobile, so I think that the server-side is working, the problem is when I try to communicate with another esp instead of a computer, tho the esp is communicating successfully with 192.168.1.200/ link.

  1. you don't need HTTP protocol to communicate between two microcontrolers

  2. if you want to use HTTP, then for client use the ESP8266HttpClient library

1:My project is based on two servers, this is just a side feature that I need to do, so the needing of HTTP is a must, especially I am using ESP8266WiFi and ESP8266WebServer libraries
2: I tried that example and now it doesn't connect at all nor 192.168.1.200 neither 192.168.1.200/p2t, tho it's connected to the network: the original code:

/**
   BasicHTTPClient.ino

    Created on: 24.05.2015

*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

ESP8266WiFiMulti WiFiMulti;

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("****", "**");

}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {
  Serial.print("Connected with ip: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
    WiFiClient client;

    HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    if (http.begin(client, "192.168.1.202")) {  // HTTP


      Serial.print("[HTTP] GET...\n");
      // start connection and send HTTP header
      int httpCode = http.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  }

  delay(10000);
}

the output

Connected with ip: http://192.168.1.11/
[HTTP] begin...
[HTTP} Unable to connect

Again my server-side is working, and what proves that is I can connect to it with my laptop and mobile phone.
any ideas please.

if you connect WiFiClient it is a TCP socket and it connects to IP and port.

a HTTP request with HttpClient is to URL so http://192.168.1.202/p2t
the HttpClient object parses the URL makes a TCP connection to IP and port (80 for http://) and then sends a HTTP GET request for /p2t in this case

Juraj:
if you connect WiFiClient it is a TCP socket and it connects to IP and port.

a HTTP request with HttpClient is to URL so http://192.168.1.202/p2t
the HttpClient object parses the URL makes a TCP connection to IP and port (80 for http://) and then sends a HTTP GET request for /p2t in this case

Thanks for your explanation, your explanation made me retry that example of HttpClient and it worked, I don't know why it didn't work on the first try when you first told me,
Done, thank you again :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.