tzapu / WiFiManager

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

Problem to Save on FS #863

Open leofuscaldi opened 5 years ago

leofuscaldi commented 5 years ago

Basic Infos

Hardware

WiFimanager Branch/Release:

Esp8266/Esp32:

Hardware: ESP-12F,

ESP Core Version: 2.4.0, staging

Description

Cannot save anything on ESP12F. It seems that it fails to mount FS.

mounting FS...
failed to mount FS

Settings in IDE

Module: NodeMcu

Additional libraries:

Sketch is the AutoConnectWithFSParametersAndCustomIP where I`ve changed the IP and gateway.

#include <FS.h>                   //this needs to be first, or it all crashes and burns...

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson

//define your default values here, if there are different values in config.json, they are overwritten.
char mqtt_server[40];
char mqtt_port[6] = "1883";
char blynk_token[34] = "YOUR_BLYNK_TOKEN";

//flag for saving data
bool shouldSaveConfig = false;

//callback notifying us of the need to save config
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();

  //clean FS, for testing
  //SPIFFS.format();

  //read configuration from FS json
  Serial.println("mounting FS...");

  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");

          strcpy(mqtt_server, json["mqtt_server"]);
          strcpy(mqtt_port, json["mqtt_port"]);
          strcpy(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load json config");
        }
        configFile.close();
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
  //end read

  // The extra parameters to be configured (can be either global or just in the setup)
  // After connecting, parameter.getValue() will get you the configured value
  // id/name placeholder/prompt default length
  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);

  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  //set static ip
  wifiManager.setSTAStaticIPConfig(IPAddress(192,168,8,88), IPAddress(192,168,8,1), IPAddress(255,255,255,0));

  //add all your parameters here
  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_blynk_token);

  //reset settings - for testing
  //wifiManager.resetSettings();

  //set minimu quality of signal so it ignores AP's under that quality
  //defaults to 8%
  //wifiManager.setMinimumSignalQuality();

  //sets timeout until configuration portal gets turned off
  //useful to make it all retry or go to sleep
  //in seconds
  //wifiManager.setTimeout(120);

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");

  //read updated parameters
  strcpy(mqtt_server, custom_mqtt_server.getValue());
  strcpy(mqtt_port, custom_mqtt_port.getValue());
  strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["mqtt_server"] = mqtt_server;
    json["mqtt_port"] = mqtt_port;
    json["blynk_token"] = blynk_token;

    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }

    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
    //end save
  }

  Serial.println("local ip");
  Serial.println(WiFi.localIP());

}

void loop() {
  // put your main code here, to run repeatedly:

}

Debug Messages

mounting FS... failed to mount FS WM: [3] allocating params bytes: 20 WM: [2] Added Parameter: server WM: [2] Added Parameter: port WM: [2] Added Parameter: blynk WM: [1] AutoConnect WM: [2] Connecting as wifi client... WM: [1] STA static IP: 192.168.8.88 WM: [2] Custom static IP/GW/Subnet/DNS WM: [2] Custom STA IP/GW/Subnet WM: [1] STA IP set: 192.168.8.88 WM: [3] WIFI station disconnect WM: [1] No saved credentials, skipping wifi WM: [2] Connection result: WL_NO_SSID_AVAIL WM: [3] lastconxresult: WL_NO_SSID_AVAIL WM: [1] AutoConnect: FAILED WM: [2] AccessPoint set password is VALID WM: [1] password WM: [3] WIFI station disconnect WM: [3] WiFi station enable WM: [2] Disabling STA WM: [2] Enabling AP WM: [1] StartAP with SSID: AutoConnectAP WM: [1] AP IP address: 192.168.4.1 WM: [3] setupConfigPortal WM: [1] Starting Web Portal WM: [3] dns server started with ip: 192.168.4.1 WM: [2] HTTP server started WM: [2] WiFi Scan ASYNC started WM: [2] Config Portal Running, blocking, waiting for clients... WM: [2] NUM CLIENTS: 0 WM: [2] WiFi Scan ASYNC completed in 2203 ms WM: [2] WiFi Scan ASYNC found: 2 WM: [2] <- HTTP Root WM: [3] -> 192.168.4.1 WM: [3] lastconxresult: WL_CONNECTED WM: [2] Scan is cached 17023 ms ago WM: [3] -> connectivitycheck.gstatic.com WM: [2] <- Request redirected to captive portal WM: [3] -> clients3.google.com WM: [2] <- Request redirected to captive portal WM: [3] -> portal.fb.com WM: [2] <- Request redirected to captive portal WM: [2] <- HTTP Root WM: [3] -> 192.168.4.1 WM: [3] lastconxresult: WL_CONNECTED WM: [2] Scan is cached 19344 ms ago WM: [3] -> connect.rom.miui.com WM: [2] <- Request redirected to captive portal WM: [2] <- HTTP Root WM: [3] -> 192.168.4.1 WM: [3] lastconxresult: WL_CONNECTED WM: [2] WiFi Scan ASYNC started WM: [3] -> 192.168.4.1 WM: [2] WiFi Scan ASYNC completed in 2205 ms WM: [2] WiFi Scan ASYNC found: 4 WM: [2] <- HTTP Wifi WM: [2] Scan is cached 1036 ms ago WM: [1] 4 networks found WM: [2] DUP AP: OTWLAN WM: [2] AP: -66 OTWLAN WM: [2] AP: -66 OTpublic WM: [2] AP: -95 NET_2G034B04 WM: [3] _staShowStaticFields WM: [3] lastconxresult: WL_CONNECTED WM: [3] Sent config page WM: [3] -> connect.rom.miui.com WM: [2] <- Request redirected to captive portal WM: [3] -> connect.rom.miui.com WM: [2] <- Request redirected to captive portal WM: [3] -> connect.rom.miui.com WM: [2] <- Request redirected to captive portal WM: [2] NUM CLIENTS: 1 WM: [3] -> connect.rom.miui.com WM: [2] <- Request redirected to captive portal WM: [2] <- HTTP WiFi save
WM: [3] Method: POST WM: [2] Parameters WM: [2] -------------------- WM: [2] server: Teste.xxx.com WM: [2] port: 1883 WM: [2] blynk: YOUR_BLYNK_TOKEN WM: [2] -------------------- WM: [3] static ip: 192.168.8.88 WM: [3] static gateway: 192.168.8.1 WM: [3] static netmask: 255.255.255.0 WM: [3] Sent wifi save page WM: [2] process connect WM: [2] Connecting as wifi client... WM: [1] STA static IP: 192.168.8.88 WM: [2] Custom static IP/GW/Subnet/DNS WM: [2] Custom STA IP/GW/Subnet WM: [1] STA IP set: 192.168.8.88 WM: [3] WIFI station disconnect WM: [1] Connecting to new AP: $ecreta WM: [3] Using Password: $3nh4$3gur4 WM: [3] WiFi station enable WM: [3] enableSTA PERSISTENT ON WM: [1] connectTimeout not set, ESP waitForConnectResult... WM: [2] Connection result: WL_CONNECTED WM: [3] lastconxresult: WL_CONNECTED WM: [1] Connect to new AP [SUCCESS] WM: [1] Got IP Address: WM: [1] 192.168.8.88 Should save config WM: [2] disconnect configportal WM: [2] restoring usermode STA WM: [2] wifi status: WL_CONNECTED WM: [2] wifi mode: STA WM: [1] config portal exiting connected...yeey :) saving config failed to open config file for writing {"mqtt_server":"Teste.xxx.com","mqtt_port":"1883","blynk_token":"YOUR_BLYNK_TOKEN"}local ip 192.168.8.88 WM: [3] freeing allocated params! WM: [3] unloading

tablatronix commented 5 years ago

did you do a full flash erase?

leofuscaldi commented 5 years ago

I just found the problem... looking for my core version, I found that it was on 2.5.

I've downgraded it to 2.4 and it worked!

tablatronix commented 5 years ago

The memory might have changed you may need a full erase when switching versions

leofuscaldi commented 5 years ago

I've tried to full erase it, but made no diference. Only when I swiched back to 2.4 it worked.

Albin76 commented 5 years ago

Hi. Same problem for me and also solved by downgrading esp8266 core from 2.5.0 to 2.4.0 thanks to @leofuscaldi comment! Thanks a lot @leofuscaldi, made my day! Have been troubleshooting for some time now. :)

supriyono01 commented 5 years ago

Hi, I experienced the same thing. I have downgraded version 8266 from 2.5 to 2.4 but have not been able to solve the problem. Can help me.... After I tried it many times it turned out that it was saved but provided that when I left the portal, it was directly connected to the ap. If it is not directly connected then the parameters that we enter through the portal cannot be saved

tablatronix commented 5 years ago

have you guys tried a basic spiffs example, NOT wm?

tablatronix commented 5 years ago

2.5 release notes say that build flags changed and is a breaking change, are your arduino IDEs up to date, maybe spiffs is now disabled

supriyono01 commented 5 years ago

Ok...thankyou, I will try to update my arduino ide.

Pada tanggal Sel, 7 Mei 2019 20.20, Shawn A notifications@github.com menulis:

2.5 release notes say that build flags changed and is a breaking change, are your arduino IDEs up to date, maybe spiffs is now disabled

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tzapu/WiFiManager/issues/863#issuecomment-490076319, or mute the thread https://github.com/notifications/unsubscribe-auth/AJPCV5URQRQ337K7F7KKVQDPUF625ANCNFSM4HDCRQTQ .

DanwithArduino commented 5 years ago

Having issues with this as well. Downgrading to 2.4.x (all versions) breaks my MQTT/pubsub client as well. So, I can either have functioning FS or functioning MQTT... But I can't figure out what the changes are. ESP12F, core versions 2.4.x and 2.5.2. WifiManager development branch.

tablatronix commented 5 years ago

Hmm which default arduino examples have you tried ? i will test them

DanwithArduino commented 5 years ago

Might not be WiFiManager's problem. I just tried https://github.com/bblanchon/ArduinoJson/blob/6.x/examples/JsonConfigFile/JsonConfigFile.ino and am still unable to mount the file system.

tablatronix commented 5 years ago

That is for SD cards is it not?

DanwithArduino commented 5 years ago

Found the solution: In the Arduino IDE (or presumably the board manager in VScode but have not verified - should be same functionality) go to tools\Flash Size\choose one.... So far, I am getting positive results for 4M (1M SPIFFS).

tablatronix commented 5 years ago

Yeah that is typically the setting you use for spiffs, unless we are having a memory issue here? Development is surely larger that release, but I made sure it was not that much

DanwithArduino commented 5 years ago

I had 4M (No Spiffs) before. Still learnding. Thanks for the replies. I think I'm all good now after a few days of head banging.

carlesgutierrez commented 5 years ago

Hello, I'm not able to write or read using SPIFFS. I've succesfully checked before that SPIFFS works on a regular example without the wifiManager. How I can check if It FAILS to mount FS?

My configuration:

tablatronix commented 5 years ago

do you have spiffs enabled, I think it is now optional not default.

carlesgutierrez commented 5 years ago

Hey, If do you mean by enable: mode to Flash Size to "4M (1M SPIFFS)", yes I did

tablatronix commented 5 years ago

if (SPIFFS.begin()) should do that.

hmm Have you tried full erase?

carlesgutierrez commented 5 years ago

You are absolutelly right. I had forgotten to setup SPIFFS with SPIFFS.begin() Thanks!!

tablatronix commented 5 years ago

might also want to close spiffs after using it

carlesgutierrez commented 5 years ago

Thanks for the advise @tablatronix , I will.

cunhaeduardo commented 4 years ago

Hi guys! Today I have the same problem as you and I solved it changing configuration in tool -> flash size -> 4M(1M SPIFFS). Someone posted it but I'm doing it again to be easier to find!

Zadkiel2 commented 4 years ago

Hi guys! Please help me with the project I am building now. The project needs codes like this but I had problems dealing with ArduinoJson 6 and I am really new to these kind of things so I really have no idea about it. Hope you guys help me out. Salamat.