-->
Page 1 of 1

Code works on ESP8266 but only partially works on ESP32

PostPosted: Fri Nov 27, 2020 12:33 am
by smatchymo
I'm slowly learning the ins and outs of networking with ESP8266 ESP-01 and ESP32. I'm trying to run an ESP32 as a server that reads from two ESP8266 ESP-01 clients. Everything works when I have an ESP-01 as the server. However, when I use an ESP32 instead, it only reads the data the first few attempts from each client. After that it is unable to communicate with the clients. Here is my code when the ESP-01 is the server. The only change I make with the ESP32 is changing the library to #include <WiFi.h>. I wonder if I'm selecting the wrong board when loading the code from the Arduino IDE. Any thoughts?

Code: Select all#include <SPI.h>
#include <ESP8266WiFi.h>

//------- WI-FI details ----------//
char ssid[] = "*********"; //SSID here
char pass[] = "*********"; // Passowrd here
//-----------------------------//

#define MAX_CLIENTS 2

WiFiServer server(80);
WiFiClient *clients[MAX_CLIENTS] = { NULL };

IPAddress ip(192, 168, 1, 137);            // IP address of the server
IPAddress gateway(192,168,1,1);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network

String request = "";

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

void loop() {
  WiFiClient newClient = server.available();
  if (newClient) {
    Serial.println("New client.");
    if (newClient.connected()) {
      Serial.println("Sever connected.");
      for (int i=0 ; i<MAX_CLIENTS ; ++i) {
        if (NULL == clients[i]) {
            clients[i] = new WiFiClient(newClient);
            break;
        }
      }
    }
    for (int i=0 ; i<MAX_CLIENTS ; ++i) {
      if (NULL != clients[i] && clients[i]->available() ) {
        clients[i] = new WiFiClient(newClient);
        request = newClient.readStringUntil('\r');
        Serial.print("From client: ");
        Serial.println(request);
        newClient.println("Data received!\r"); //
      }
    }
    Serial.println("flushing");
    newClient.flush();
    newClient.stop();
    request = "";
  }
}

void internetServer() {
  if (WiFi.status() != WL_CONNECTED) {
    while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      WiFi.config(ip, gateway, subnet);
      WiFi.begin(ssid, pass);
      delay(5000);
    }
    server.begin();                       
    Serial.println("Connected to wifi");
    Serial.print("Status: "); Serial.println(WiFi.status());
    Serial.print("IP: ");     Serial.println(WiFi.localIP());
    Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
    Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
    Serial.print("SSID: "); Serial.println(WiFi.SSID());
    Serial.print("Signal: "); Serial.println(WiFi.RSSI());
    Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
  }
}

Re: Code works on ESP8266 but only partially works on ESP32

PostPosted: Thu Dec 10, 2020 9:59 pm
by guysnraw
No not just wifi.h but you gotto change many in code bcoz they are different. Try searching in web for exact code of your usage.