/**
 ******************************************************************************
 * @file     WiFi_StaticIP.ino
 * @brief    ESP32 connect to Wi-Fi with static IP configuration
 * @version  V1.2
 * @date     2025-10-14
 * @author   liangjingheng
 * @company  Waveshare
 * @license  MIT
 ******************************************************************************
 * Features:
 * 1. Connect ESP32 to "JSBZY-5G" Wi-Fi network with static IP
 * 2. Configure static IP, gateway, subnet mask, and DNS matching network settings
 * 3. Print connection status and network info via serial monitor
 * 4. Auto-reconnect if Wi-Fi connection drops
 ******************************************************************************
 */

#include <WiFi.h>

// Wi-Fi credentials (match network in screenshot)
const char *ssid = "waveshare";           // your Wi-Fi SSID 
const char *password = "12345678";        // your Wi-Fi password

// Static IP configuration (matched to screenshot's network parameters)
IPAddress local_ip(192, 168, 9, 100);   // Static IP for ESP32 
IPAddress gateway(192, 168, 11, 1);    // Gateway IP 
IPAddress subnet(255, 255, 252, 0);    // Subnet mask 
IPAddress dns(192, 168, 11, 1);        // DNS server 

void setup() {
  // Initialize serial communication (115200 baud rate)
  Serial.begin(115200);
  while (!Serial) {
    ; // Wait for serial port to initialize (for boards like Leonardo)
  }

  // Set Wi-Fi mode to Station (client mode)
  WiFi.mode(WIFI_STA);

  // Apply static IP configuration
  WiFi.config(local_ip, gateway, subnet, dns);

  // Start Wi-Fi connection
  WiFi.begin(ssid, password);
  Serial.println("Connecting to Wi-Fi (JSBZY-5G) with static IP...");

  // Wait for connection with retry logic
  int retryCount = 0;
  const int maxRetries = 15; // Retry for 15 seconds (15 * 1000ms)
  while (WiFi.status() != WL_CONNECTED && retryCount < maxRetries) {
    delay(1000);
    Serial.print(".");
    retryCount++;
  }

  // Print connection result
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nSuccessfully connected to Wi-Fi!");
    Serial.print("Static IP Address: ");
    Serial.println(WiFi.localIP());
    Serial.print("Gateway: ");
    Serial.println(gateway);
    Serial.print("Subnet Mask: ");
    Serial.println(subnet);
  } else {
    Serial.println("\nFailed to connect to Wi-Fi!");
    Serial.print("WiFi Status Code: ");
    Serial.println(WiFi.status()); // Print code for debugging (e.g., 6 = AUTH_FAIL)
  }
}

void loop() {
  // Periodically check Wi-Fi connection and auto-reconnect
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Wi-Fi disconnected, attempting reconnection...");
    WiFi.begin(ssid, password); // Re-initiate connection
    int retry = 0;
    while (WiFi.status() != WL_CONNECTED && retry < 5) {
      delay(1000);
      Serial.print(".");
      retry++;
    }
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("\nReconnected to Wi-Fi!");
      Serial.print("Current IP: ");
      Serial.println(WiFi.localIP());
    }
  }
  delay(5000); // Check connection every 5 seconds
}