ph1p / ikea-led-obegraensad

ESP32/Arduino hack for the ikea OBEGRÄNSAD led wall lamp
MIT License
610 stars 85 forks source link

fix: static IP config broken on ESP32 #94

Closed schw4rzlicht closed 10 months ago

schw4rzlicht commented 10 months ago

Closes #93

kohlsalem commented 10 months ago

Hmm, shouldnt the goal be, to set the static IP in the config portal of the wifi manager?

I would dream about having a firmware downloadable via browser (like on so many other projects, comiling in the ip would sound like a step in the wrong direction...

Not that i tried this in any direction, but chatgpt proposes something along

#include <WiFiManager.h>

WiFiManager wifiManager;

// Custom parameters for static IP configuration
WiFiManagerParameter custom_static_ip("static_ip", "Static IP Configuration", "0", 1);
WiFiManagerParameter custom_ip("ip", "Static IP", "192.168.1.100", 15);
WiFiManagerParameter custom_gateway("gateway", "Gateway", "192.168.1.1", 15);
WiFiManagerParameter custom_subnet("subnet", "Subnet", "255.255.255.0", 15);
WiFiManagerParameter custom_dns("dns", "DNS", "8.8.8.8", 15);

void setup() {
  // Add custom parameters to WiFiManager
  wifiManager.addParameter(&custom_static_ip);
  wifiManager.addParameter(&custom_ip);
  wifiManager.addParameter(&custom_gateway);
  wifiManager.addParameter(&custom_subnet);
  wifiManager.addParameter(&custom_dns);

  // Set the save config callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  // Try to connect to previously saved Wi-Fi
  if (!wifiManager.autoConnect("AP-Name")) {
    Serial.println("Failed to connect and hit timeout");
    // Reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  }

  // Print the local IP address
  Serial.println("Connected! IP address: " + WiFi.localIP().toString());
}

void loop() {
  // Handle other tasks in the loop if needed
}

void saveConfigCallback() {
  Serial.println("Save config callback called");
  // Save the custom parameters to preferences
  // Retrieve the values using custom_ip.getValue(), custom_gateway.getValue(), etc.
  // Save them to your preferred storage mechanism (e.g., EEPROM, Preferences)
}

void handleStaticIpCheckbox() {
  // Check the state of the "Static IP Configuration" checkbox
  bool staticIpEnabled = String(custom_static_ip.getValue()) == "1";

  // Enable or disable the static IP configuration fields based on the checkbox state
  custom_ip.setEnabled(staticIpEnabled);
  custom_gateway.setEnabled(staticIpEnabled);
  custom_subnet.setEnabled(staticIpEnabled);
  custom_dns.setEnabled(staticIpEnabled);
}
kohlsalem commented 10 months ago

Edit: Actualy setShowStaticFields(true); on the WifiManager should already do the job.

schw4rzlicht commented 10 months ago

Imo the issue with that approach is that you cannot access the web portal of the wifi manager any more as soon as the ESP is connected successfully.

I like your proposal but as compiling in the static IP is optional, I'd vote to fix it in the simplest way possible at this point and maybe improve Wifi Manager later on?

ph1p commented 10 months ago

Thank you! :) I'll also try out setShowStaticFields(true);

kohlsalem commented 10 months ago

Imo the issue with that approach is that you cannot access the web portal of the wifi manager any more as soon as the ESP is connected successfully.

I like your proposal but as compiling in the static IP is optional, I'd vote to fix it in the simplest way possible at this point and maybe improve Wifi Manager later on?

My proposal for that is to reanable hte config portal on longpress (>5 sec) of the button.

I need the same for "Night Time".

However, WifiManager is not my friend :-/