Statische IP-Adresse auf ESP32 setzen
English
Deutsch
TL;DR
Bevor Sie WiFi.begin(...) aufrufen, rufen Sie dies auf:
how-to-set-static-ip-address-on-esp32.cpp
if (!WiFi.config(
IPAddress(192, 168, 19, 5), // ESP's IP address
IPAddress(192, 168, 19, 1), // Gateway
IPAddress(255, 255, 255, 0), // IP Address
IPAddress(192, 168, 19, 1) // DNS server
)) {
Serial.println("Failed to set static IP");
}und ersetzen Sie die IP-Adresse usw. durch die statische
Vollständiges Beispiel
Dies basiert auf unserem vorherigen Post ESP32 verbindet sich nicht mit dem WLAN-Netzwerk beheben:
esp32_set_static_ip_example.cpp
#include <Arduino.h>
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin("MyWifiSSID", "MyWifiPassword");
// Set static IP
if (!WiFi.config(
IPAddress(192, 168, 19, 5), // ESP's IP address
IPAddress(192, 168, 19, 2), // Gateway
IPAddress(255, 255, 255, 0), // IP Address
IPAddress(192, 168, 19, 1) // DNS server
)) {
Serial.println("Failed to set static IP");
}
// Wait for wifi to be connected
uint32_t notConnectedCounter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.println("Wifi connecting...");
notConnectedCounter++;
if(notConnectedCounter > 150) { // Reset board if not connected after 5s
Serial.println("Resetting due to Wifi not connecting...");
ESP.restart();
}
}
Serial.print("Wifi connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
}Check out similar posts by category:
Embedded, ESP8266/ESP32, Networking
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow