Esp8266 client and server at the same time

Hi.First sorry for my poor English.
I would like to connect by wifi to esp8266 as a Server and also esp8266 connects to a Web Site as a client.
But It doesn't work at the same time.
I mean How can I set an esp8266 as a server and client at the same time.
Thanks.

leoncorleone:
Hi.First sorry for my poor English.
I would like to connect by wifi to esp8266 as a Server and also esp8266 connects to a Web Site as a client.
But It doesn't work at the same time.
I mean How can I set an esp8266 as a server and client at the same time.
Thanks.

You will have to write code that does both function, one to accept requests, the other to send requests.

The Client code, from the Ethernet.h examples:

char server[] = "www.google.com";    // name address for Google (using DNS)

 // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  // if there are incoming bytes available
  // from the server, read them and print them:
while(client.available()) {
    char c = client.read();
    Serial.print(c);
  

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}
}

The Server code is almost the same

EthernetServer server(80);

void setup(){
// configure mac, ip, etc
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

You would just switch back and forth between these two models. One initiates a connection, by using client.connect("servername"); the other starts the Server() object listening on port 80, EthernetServer server(80);. The connection is accepted by EthernetClient client =server.available();

Both use the Client() object to actually send and receive data.

Once a connection has be established, you use client.print() and client.read() to communicate with the other end.

Chuck.

Hello chucktodd,

Thank you very much for sharing such a great idea. I have tried many times on the switching between the two, unfortunately it does work for only one. may you help please,

My source code is below:

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> // Built-in

ESP8266WiFiMulti wifiMulti;

const char* host = "v-gro.com";
IPAddress IP(192, 168, 0, 99); // Set your server's fixed IP address here

const char* ssid = "Sovatna";
const char* pass = "Password"; //connect to Access Point
const char* ssids = "SOVATNA";
const char* passs = "Password";//connect to router

int x = 0;

void setup() {

Serial.begin(115200);
Serial.setTimeout(100);
Serial.println('\n');

Serial.println("done \n");

}

void loop() {
Serial.println(x);
if(x == 0){
WiFi.begin(ssid, pass);
if(WiFi.status() != WL_CONNECTED){
Serial.println("Not Connection");
delay(100);
}else{
WiFiClient client;
if(!client.connect(IP, 8080)){
Serial.println("local not connection");
}else{
Serial.println("********************************");
Serial.print("Byte sent to the AP: ");
Serial.println(client.print("Anyo\r"));
String data = client.readStringUntil('\r');
Serial.println("From the AP: " + data);
delay(1000);
}
}
x = 1;
}
if(x == 1){
WiFi.begin(ssids, passs);
if(WiFi.status() != WL_CONNECTED){
Serial.println("Server not connection");
delay(100);
}else{
Serial.println("wifi connected");
WiFiClient client;
if(!client.connect(host, 80)){
Serial.println("Server failed");
}else{
Serial.println("Connected");
delay(100);
}
client.stop();
}
//
x = 0;
}

}

Where is your WiFi.mode(...); statement?

Without such a statement your ESP8266 will be in whatever mode it was in the last time you loaded a program that did include one.

Don