tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.63k stars 1.98k forks source link

Custom Param doesn't load when starting ConfigPortal on demand & WiFi has been already connected. #1736

Open iamsstef opened 6 months ago

iamsstef commented 6 months ago

Basic Infos

When the ConfigPortal is started on Demand, after it has already connected to WiFi, the param page doesn't show the custom HTML but only the save button, in some cases it also shows some random characters.

the code below is where it starts the config portal manually.

    } else if (httpCode == 220) {
      https.end();

      wm.setConfigPortalTimeout(120);
      wm.startConfigPortal(APSSID.c_str(),APPWD);

      while (wm.getConfigPortalActive()) {
        yield();
        wm.process();
        basicBlinker.run();
        updateLED(basicBlinker);
      }
    }

Hardware

WiFimanager Branch/Release: Master

Esp8266/Esp32:

Hardware: ESP-12e, esp01, esp25

Core Version: 2.4.0, staging

Description

Problem description

Settings in IDE

Module: Generic ESP8266 Module

Additional libraries:

Sketch

#BEGIN
#include <Arduino.h>

#include <BasicTimer.h>
#include <EEPROM.h>

#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
BearSSL::WiFiClientSecure wifiClient;

ADC_MODE(ADC_VCC);

bool wm_nonblocking = true; // change to true to use non blocking

WiFiManager wm; // global wm instance
WiFiManagerParameter custom_field; // global param ( for non blocking w params )

BasicBlinker basicBlinker(500);

int IONULL = 0;
int SETUPPIN = 3;
int call_url_length = 400;

String call_url;
String struid;
String strvolt;

String APSSID;
const char *APPWD = "password";

void setup() {
  EEPROM.begin(512);

  struid = String(ESP.getChipId(), HEX);
  strvolt = String(ESP.getVcc()/1023.0F);

  APSSID = "WifiButton-" + struid;

  pinMode(IONULL, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(SETUPPIN, FUNCTION_3);
  pinMode(SETUPPIN, INPUT_PULLUP);

  digitalWrite(IONULL, HIGH);

  updateLED(false);

  basicBlinker.setBlinkTime(200);
  basicBlinker.reset();
  basicBlinker = true;

  WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP

  if(wm_nonblocking) wm.setConfigPortalBlocking(false);

  call_url = readWord();
  String customHtml = "<br/><label for='call_url'>URL To call</label><input type='text' name='call_url' value='" + call_url + "' />";
  const char* custom_input_str = customHtml.c_str();
  new (&custom_field) WiFiManagerParameter(custom_input_str); // custom html input

  wm.addParameter(&custom_field);
  wm.setSaveParamsCallback(saveParamCallback);
  wm.setConfigPortalTimeoutCallback(portalTimeoutCallback);

  std::vector<const char *> menu = {"wifi","info","param","sep","restart","exit"};
  wm.setMenu(menu);
  wm.setClass("invert");
  wm.setConfigPortalTimeout(30);
  wm.setAPClientCheck(true);

  bool res;
  if (digitalRead(SETUPPIN) == LOW) {
    wm.setConfigPortalTimeout(120);

    wm.startConfigPortal(APSSID.c_str(),APPWD);
  } else {
    res = wm.autoConnect(APSSID.c_str(),APPWD);
  }

  while (WiFi.status() != WL_CONNECTED || wm.getConfigPortalActive()) {
    yield();
    wm.process();
    basicBlinker.run();
    updateLED(basicBlinker);
  }

  updateLED(false);

}

void portalTimeoutCallback() {
  updateLED(true);
  blink();

  digitalWrite(IONULL, LOW);
  ESP.deepSleep(0);
}

void writeWord(String word) {
  int wl;
  delay(10);

  if (word.length() > call_url_length) {
    wl = 200;
  } else {
    wl = word.length();
  }

  for (int i = 0; i < wl; ++i) {
    EEPROM.write(i, word[i]);
  }

  EEPROM.write(wl, '\0');
  EEPROM.commit();
}

String readWord() {
  String word;
  char data[call_url_length];
  unsigned char readChar;
  int i = 0;

  readChar = EEPROM.read(i);

  while (readChar != '\0' && i<call_url_length) {
    readChar = EEPROM.read(i);
    data[i]=readChar;
    i++;
  }

  data[i]='\0';
  return String(data);
}

String getParam(String name){
  //read parameter from server, for customhmtl input
  String value;
  if(wm.server->hasArg(name)) {
    value = wm.server->arg(name);
  }
  return value;
}

void saveParamCallback(){
  String call_url = getParam("call_url");
  writeWord(call_url);
  String readParam = readWord();
}

void loop() {
  // wait for WiFi connection
  if ((WiFi.status() == WL_CONNECTED)) {

    wifiClient.setInsecure();

    HTTPClient https;

    call_url.replace("{UID}", struid);
    call_url.replace("{VOLT}", strvolt);

    if (https.begin(wifiClient, call_url)) {  // HTTPS
      int httpCode = https.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          //String payload = https.getString();
        } else if (httpCode == 210) {
          wm.resetSettings();
          ESP.reset();
        } else if (httpCode == 220) {
          https.end();

          wm.setConfigPortalTimeout(120);
          wm.startConfigPortal(APSSID.c_str(),APPWD);

          while (wm.getConfigPortalActive()) {
            yield();
            wm.process();
            basicBlinker.run();
            updateLED(basicBlinker);
          }
        }
      } else {
        //Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      //Serial.printf("[HTTPS] Unable to connect\n");
    }
  }

  updateLED(true);
  blink();

  //shutdown
  digitalWrite(IONULL, LOW);
  ESP.deepSleep(0);
}

void blink() {
  updateLED(false);
  delay(100);
  updateLED(true);
  delay(100);
  updateLED(false);
  delay(100);
  updateLED(true);
}

void updateLED(bool onOrOff) {
  if (onOrOff == true) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

#END

Debug Messages

messages here
sidey79 commented 1 month ago

@iamsstef

I have the same problem. The parameters are shown if no AP is connected, but not if AP is connected. Did you find any solution?