2

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);
}
2
  • 2
    there are no IP addresses in WiFi. the SoftAP device can only display the IP from DHCP server records or maybe from ARP table after the client does some TCP/IP communication with the SoftAP device.
    – Juraj
    Feb 12 at 12:10
  • Thanks for the reply. Is there any way to get the Ip? Feb 12 at 12:46

1 Answer 1

1

Not digging deep in API, as @Jurai says, it lists IP from DHCP server on AP, which know nothing about static IPs.

  1. As you set static IP to "Client" - just use it (192.168.0.101) (KISS rule)
  2. On AP implement web API which allow client to register itself
  3. Use mDNS (aka Avahi, aka Bonjour) multicast discovery espressif-skd doc arduino-esp32 ESPmDNS library

What is Your goal in using static IP on Client?

1
  • There are some testbench who are controlled by esp32. Every testbench has a number and the idea is to have the same correspondent number inside the ip-address. The fourt octets of the ip-address would be the same as the testbench number. I managed this situation with having the client send its IP address to the Access Point upon establishing the connection. Thanks for the help. Feb 13 at 5:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.