I have an ESP32 board as a client with a static IP address. The client with the static IP address connects to a second ESP32 that is acting as an access point. The connection is established and the access point recognizes the new connected device.
The problem is, with the client having a static IP address the access point displays the client IP as 0.0.0.0. If the client does not have the IP address set as static, the access point displays the correct IP address.
The code for the client:
#include <WiFi.h>
const char* ssid = "ESP32-AP";
const char* password = "123456789";
IPAddress ip(192, 168, 0, 101);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// Configure the static IP address
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi network");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
static long general_t1 = 0;
if (millis() - general_t1 > 2000) {
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
general_t1 = millis();
}
}
The code for the access point:
#include <Arduino.h>
#include <WiFi.h>
#include "esp_wifi.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char *ssid = "ESP32-AP"; // SSID Name
const char *password = "123456789"; // SSID Password - Set to NULL to have an open AP
const int channel = 10; // WiFi Channel number between 1 and 13
const bool hide_SSID = false; // To disable SSID broadcast -> SSID will not appear in a basic WiFi scan
const int max_connection = 5; // Maximum simultaneous connected clients on the AP
IPAddress local_ip(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
//*** void display_connected_devices() **************************************************************
void display_connected_devices() {
wifi_sta_list_t wifi_sta_list;
tcpip_adapter_sta_list_t adapter_sta_list;
esp_wifi_ap_get_sta_list(&wifi_sta_list);
tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);
String deviceInfo = "";
if (adapter_sta_list.num == 0) {
deviceInfo = "No Device connected";
}
else {
for (int i = 0; i < adapter_sta_list.num; i++) {
tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
deviceInfo += "Device " + String(i) + " = ";
deviceInfo += IPAddress((&station.ip)->addr).toString();
deviceInfo += "\n";
}
}
// Display accumulated device information
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("AP IP: " + WiFi.softAPIP().toString());
display.setCursor(0, 20);
display.println(deviceInfo);
display.display();
display.clearDisplay();
}
//***********************************************************************************************
//** void setup()
//***********************************************************************************************
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(25, 10);
display.println(F("ESP-32"));
display.setCursor(25, 30);
display.println(F("Router"));
display.display();
delay(2000);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(local_ip, gateway, subnet);
WiFi.softAP(ssid, password, channel, hide_SSID, max_connection);
Serial.print("[+] AP Created with IP Gateway ");
Serial.println(WiFi.softAPIP());
}
//***********************************************************************************************
//** void loop()
//***********************************************************************************************
void loop() {
display_connected_devices();
delay(5000);
}