Statische IP-Adresse auf ESP32 setzen

TL;DR

Bevor du WiFi.begin(...) aufrufst, rufe 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 ersetze 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");

  // Statische IP setzen
  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");
  }

  // Warten, bis WLAN verbunden ist
  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() {
  // Platziere hier deinen Hauptcode, der wiederholt ausgeführt wird:
}

Ähnliche Beiträge nach Kategorie: Embedded ESP8266/ESP32 Netzwerke