-->
Page 1 of 1

ESP8266 to ESP8266 Client Server Connection can't Connect

PostPosted: Wed Nov 23, 2016 12:27 pm
by tresnax
I have a project to connecting dual esp8266 using client server method, my expectation the server can get command from user from web request, and the request will be forward to client from server to turn on led. But the connection from server always failed

I'm using ESP-12F AT Command Firmware and programming using Arduino IDE

the code of server :
Code: Select all//--------------------------------------------------------------------------
  #include <ESP8266WiFi.h>
//--------------------------------------------------------------------------
// Membuat Variabel Autentikasi
//--------------------------------------------------------------------------
  const char* ssid = "master-sport";
  const char* password = "1234567890";
//--------------------------------------------------------------------------
// Mengatur IP Address
//--------------------------------------------------------------------------
  IPAddress CLIENTA(192,168,4,100);
//--------------------------------------------------------------------------
// Create an instance of the server
// specify the port to listen on as an argument
//--------------------------------------------------------------------------
  WiFiServer server(80);
  WiFiClient client;
//===========================================================================
  void setup() {
   Serial.begin(115200);
   delay(10);

  // Mengatur WiFi ----------------------------------------------------------
   Serial.println();
   Serial.print("Configuring access point...");

   WiFi.mode(WIFI_AP);
   WiFi.softAP(ssid, password);

  // Start the server -------------------------------------------------------
   server.begin();
   Serial.println("Server dijalankan");

  // Print the IP address ---------------------------------------------------
   Serial.println(WiFi.localIP());
 }

//===========================================================================

  void loop() {
  // Check if a client has connected ----------------------------------------
    client = server.available();
    if (!client) {
    return;
  }

  // Wait until the client sends some data ----------------------------------
   Serial.println("new client");
   while(!client.available()){
     delay(1);
  }

  // Read the first line of the request -------------------------------------
   String req = client.readStringUntil('\r');
   Serial.println(req);
   client.flush();

  // Match the request ------------------------------------------------------
   const char* host;
   int val;
   if (req.indexOf("/sr/0") != -1){
      host = "sa";
      val = 0;
    }
   else if (req.indexOf("/sr/1") != -1){
      host = "sa";
      val = 1;
    }
   else {
     Serial.println("invalid request");
     client.stop();
     return;
    }

   client.flush();
   Serial.println("Client disonnected");


  // Pengiriman data ke client -----------------------------------------------
   int value  = 0;
   delay(5000);
   ++value;

   Serial.print("connecting to ");
   Serial.println(CLIENTA);

  // Memebuat TCP Connection Menggunakan WiFiClient --------------------------
   const int httpPort = 80;
   if (!client.connect(CLIENTA, httpPort)) {
     Serial.println("connection failed");
     return;
   }

  // Membuat URL untuk dikirimkan -------------------------------------------
   String url = host;
   url += "/";
   url += val;

   Serial.print("Requesting URL: ");
   Serial.println(url);

  // Mengirimkan Request ke Client ------------------------------------------
   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + CLIENTA + "\r\n" +
               "Connection: close\r\n\r\n");
   unsigned long timeout = millis();
   while (client.available() == 0) {
     if (millis() - timeout > 5000) {
       Serial.println(">>> Client Timeout !");
       client.stop();
       return;
    }
  }

  // Melihat semua line balasan dari server dan menampilkan di Serial -------
   while(client.available()){
     String line = client.readStringUntil('\r');
     Serial.print(line);
   }

   Serial.println();
   Serial.println("closing connection");
}
//=============================================================================

Client code :
Code: Select all//--------------------------------------------------------------------------
  #include <ESP8266WiFi.h>
  #include <Adafruit_NeoPixel.h>
  #ifdef __AVR__
  #include <avr/power.h>
  #endif
  #define PIN 13
//--------------------------------------------------------------------------
// Membuat Variabel Autentikasi
//--------------------------------------------------------------------------
  const char* ssid = "master-sport";
  const char* password = "1234567890";
//--------------------------------------------------------------------------
// Mengatur IP Address
//--------------------------------------------------------------------------
  IPAddress IP(192,168,4,100);
  IPAddress NETMASK(255,255,255,0);
  IPAddress NETWORK(192,168,4,1);
//--------------------------------------------------------------------------
// Create an instance of the server
// specify the port to listen on as an argument
//--------------------------------------------------------------------------
  WiFiServer server(80);
//--------------------------------------------------------------------------
// Menasukan LED RGB
//--------------------------------------------------------------------------
  Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
//===========================================================================
  void setup() {
   Serial.begin(115200);
   delay(10);

  // Connect to WiFi network ------------------------------------------------
   Serial.println();
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);

  // Mengatur WiFi ----------------------------------------------------------
   WiFi.mode(WIFI_STA);
   WiFi.begin(ssid, password);
   WiFi.config(IP, NETMASK, NETWORK);

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

  // Start the server -------------------------------------------------------
   server.begin();
   Serial.println("Server dijalankan");

  // Menjalankan LED RGB ----------------------------------------------------
   strip.begin();
   strip.show();

  // Print the IP address ---------------------------------------------------
   Serial.println(WiFi.localIP());
 }

//===========================================================================

  void loop() {
  // Check if a client has connected ----------------------------------------
    WiFiClient client = server.available();
    if (!client) {
    return;
  }

  // Wait until the client sends some data ----------------------------------
   Serial.println("new client");
   while(!client.available()){
     delay(1);
  }

  // Read the first line of the request -------------------------------------
   String req = client.readStringUntil('\r');
   Serial.println(req);
   client.flush();

  // Match the request ------------------------------------------------------
   int val;
   if (req.indexOf("/sa/0") != -1){
      strip.setPixelColor(4,0x000000); //LED menjadi mati -------------------
      strip.show();
    }
   else if (req.indexOf("/sa/1") != -1){
      strip.setPixelColor(4, 0xFF0000); //LED menjadi merah -----------------
      strip.show();
    }
   else {
     Serial.println("invalid request");
     client.stop();
     return;
    }

   client.flush();
   Serial.println("Client disonnected");
}

//============================================================================


whats wrong that code ?