简体   繁体   中英

Arduino ESP32 DHCP server

I searched how to configure a DHCP server on ESP32 Arduino to distribute addresses for clients that connect to my ESP32 access point, but unfortunately I did not get any source code for that.

Any help?

As long as you use WiFi.softAP() , you do not need to explicitly configure a DHCP server on the ESP32. It will happen automatically - the library looks after it for you.

Here is a minimal example, where - in addition to setting the ESP32 up as an access point - a TCP server is also started on port 80.

WiFiServer server(80);

static const char *ap_ssid = "ESP32-001";
static const char *ap_pass = "temp_pass";

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

  WiFi.softAP(ap_ssid, ap_pass);
  Serial.print("Access point running. IP address: ");
  Serial.print(WiFi.softAPIP());
  Serial.println("");

  server.begin();
}

void loop() {
  WiFiClient client = server.available();

  if (client) {
    String client_ip = client.remoteIP().toString();
    Serial.print("Client connected. IP address = ");
    Serial.print(client_ip);
    Serial.println("");
    client.println("Hello ...");
    client.stop();
  }
}

I have attached the serial output in a screenshot below. Notice the

dhcps: send_offer>>udp_sendto result 0

message.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM