1

Trying to communicate from one ESP32 to another ESP32 ,with one acting as a AP and another acting as Client but cant seem to connect the esp client to the esp AP, but connecting to AP using my smartphone works.Sorry if this seems to be a simple questions, I am new to esp32s and WiFI communication.

Code for the Access-point

#include <WiFi.h>
const char* ssid = "ESP32-Access-Point";
const char* password = "SyedAhmedAli";
 WiFiServer server(80); 
 void setup() {
 Serial.begin(115200);
 Serial.println("Setting AP (Access Point)...");
 WiFi.softAP(ssid, password); 
 IPAddress IP = WiFi.softAPIP();
 Serial.print("AP IP address: ");
 Serial.println(IP);
 Serial.print("MAC address: ");
 Serial.println(WiFi.softAPmacAddress());
 server.begin();
}
void loop(){
 WiFiClient client = server.available(); // Listen for incoming clients
 if (client) 
 { Serial.println("New Client."); 
 while (client.connected()) 
 { 
 Serial.println(client.connected());
 Serial.println("Client connected.");
 Serial.println("");
 }
 client.stop();
 Serial.println("Client disconnected.");
 Serial.println("");
 }
}

Code for the Client

#include <WiFi.h>
 #include <SPI.h>
const char* ssid = "ESP32-Access-Point";
const char* password = "SyedAhmedAli";
void setup() {
 Serial.begin(115200);
 WiFi.begin(ssid, password);
 scanNetworks();
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.println("Connecting to WiFi..");
 }
 Serial.println("Connected to the WiFi network");
 }
void loop() {
}
void scanNetworks() {
 // scan for nearby networks:
 Serial.println("** Scan Networks **");
 byte numSsid = WiFi.scanNetworks();
 // print the list of networks seen:
 Serial.print("SSID List:");
 Serial.println(numSsid);
 // print the network number and name for each network found:
 for (int thisNet = 0; thisNet<numSsid; thisNet++) {
 Serial.print(thisNet);
 Serial.print(") Network: ");
 Serial.println(WiFi.SSID(thisNet));
 }
}
asked Dec 6, 2018 at 13:31
6
  • remove scanNetworks() call from station code. you execute it while the esp should connect to AP Commented Dec 6, 2018 at 14:21
  • Should i use station mode in the client ? Commented Dec 6, 2018 at 16:06
  • WiFi has Access Points and stations. no clients Commented Dec 6, 2018 at 18:13
  • @Juraj Hey! I got a message on client side that it has connected, but server side is not printing that a client has connected, perhaps you know why? Commented May 23, 2019 at 6:22
  • 1
    @Atizs, do you send some data? many Arduino networking libraries wait until data are available from the client Commented May 23, 2019 at 6:35

2 Answers 2

2

As @juraj mentions, in the Arduino code for ESP32, you cannot initiate a scan while an attempt to connect to AP is already ongoing.

Call scanNetworks() before attempting to connect (before the WiFi.begin(ssid, password);).

or

Call scanNetworks() after the connection to the AP has been established (after the while (WiFi.status() != WL_CONNECTED){}).

I don't see any point to scan networks while trying to connect to a known WiFi AP anyway.

gre_gor
6,65812 gold badges105 silver badges106 bronze badges
answered Dec 6, 2018 at 21:52
Sign up to request clarification or add additional context in comments.

1 Comment

@SyedAhmedAli Hey! I got a message on client side that it has connected, but server side is quiet. Did you also have this?
0

ESPnow (see example here ) is easy for ESP to ESP communication. (also ESP8266 etc)

answered Sep 22, 2020 at 3:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.