I need a pretty simple example of tzapu wifimanager

Would you believe if I told you that there is wifi manager Blynk optimized code posted in this community?

2 Likes

yeah… I saw some screenshoots, but still can’t find a simple example… :frowning:

Sure… i can believe that… but can’t find it… :roll_eyes:

I was seeking the suggestet by @Gunner , the “WiFi provisioning” by Blynk in the MyPlant example… looks “promising” for my quest… :smiley:

Still at work, I wiil take a deeper look later…!

I like others, have spent hours looking for it, but find results of sketches with other complex stuff in them and I don’t know what parts I need and what parts I don’t need. If such exists, could someone please point me to a simple example that:
has wifimanager auto connect feature, and also gets the Blink Auth code? Thanks so much.

I know for a fact it is out there. I posted it.

1 Like

Hi. Thanks so much!!! At first it didn’t work, but I uncommented the two lines
//wifiManager.resetSettings();
//SPIFFS.format();
And ran that sketch once and then commented them out again, and it worked! wifi manger worked, and also my blynk auth code, and blynk connected!
thanks again.
This seems like such an important thing, it should be somewhere on the headings page.
Cheers

Oh NO! All of a sudden my Wemos D1 Pro Mini stopped working. I was adding in some lines in my code to save settings to EEPROM, I added lines like this:
void factoryReset() {
Serial.println(F(“Resetting EEPROM to factory settings”));
s.aggKp = 8;
s.aggKi = 0.2;
s.aggKd = 1;
s.consKp = 2;
s.consKi = 0.1;
s.consKd = .5;
EEPROM.put(0, s);
}

And I noticed it wasn’t saving the values when I powered down the unit, so I searched a little more and found this command in setup()
EEPROM.begin(512);
(I’ve never had to use that with an UNO)
but right after this change, my wemos ESP8266 quit working. So I went back and tried this basic wifimanager sketch, and it doesn’t work either. But it was working. I turned my router off, and after a bit the Wemos would go into access point mode and I could put in my ssid, etc. But now this very code that worked won’t work anymore. :frowning:
I tried uncommenting the lines
//wifiManager.resetSettings();
//SPIFFS.format();
and under the Arduino IDE /Tools/Erase Flash/ I’ve chosen just the sketch, sketch and wifi, and erase all. Nothing seems to make it work again. So I tried rebooting my Mac, and loading it on the 2nd Wemos I have, and now it won’t connect either. !
Is there something simple I’m missing?

I’m using Arduino 1.8.5, and all the libraries in the manager are updated.
this is my code that used to work.
and this is as far as the Serial monitor goes:
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld

#include <FS.h>                   //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <SimpleTimer.h>
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson


#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
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() {
  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(blynk_token, json["blynk_token"]);
        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } 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_blynk_token("blynk", "blynk token", blynk_token, 33);

  //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);

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

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

  //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("autoconnect")) {
    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(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["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());

  Blynk.config(blynk_token);
  bool result = Blynk.connect();

  if (result != true)
  {
    Serial.println("BLYNK Connection Fail");
    Serial.println(blynk_token);
    wifiManager.resetSettings();
    ESP.reset();
    delay (5000);
  }
  else
  {
    Serial.println("BLYNK Connected");
  }
}

void loop() {

  Blynk.run();

}
1 Like

I just loaded the sketch to the same type of board, and it works as it should.

Is there some kind of settings on the wemos ESP8266 that would be messed up that it doesn’t work on either of mine?
I discovered if I load a basic Blynk sketch that uses the blynk.begin to put in the WiFi credentials it will work. But then when I load yours again, it just hangs up.
Any idea why or what might be wrong with my wemos. I have 2 of them and they both act this way.
Right after I added that EEPROM.begin(512) statement.
Thanks.

You are flashing Wemos without SPIFFs, I suppose.

I’m researching this now to see what you mean, but the only option I have is seen in this photo. Is this what you mean?

I’ve discovered that my wemos is actually running, but it takes a LONG time, maybe 5 minutes. before it’ll go into auto connect AP mode.
The Serial outputs:

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld
mounting FS...

And then it hangs for about 5 minutes. Then the serial monitor updates to:

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld
mounting FS...
mounted file system
*WM: Adding parameter
*WM: blynk
*WM: 
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Already connected. Bailing out.
*WM: IP Address:
*WM: 192.168.1.4
connected...yeey :)
BLYNK Connection Fail
token
*WM: settings invalidated
*WM: THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA.

 ets Jan  8 2013,rst cause:2, boot mode:(1,6)


 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset

I’ve found someone with the exact sounding issue in the ESP8266 forum. They said the Spiffs.begin() makes it hang a long time on the Wemos D1 Pro Mini. It’s closed, but I don’t see how/if they resolved it?
https://github.com/esp8266/Arduino/issues/4237

If the Spiffs thing is making it hang, why was it working before, and now suddenly hang for 5 minutes?

Hurray! :joy:
I found my problem. I commented out my EEPROM.begin(), put() and get()
and now it’s working again. It connects to Blynk in about 10 seconds. And it goes into AP mode if I unplug my router.

But I really need to save about 6 integers to memory, that are configuration values for my project.
I will try deleting my EEPROM library and adding it back (I use the library manager for my libraries). Maybe I’m using the wrong library? I’m using
#include <EEPROM.h>
Or maybe I can store my config settings in the Spiff thing? I’ll keep researching…
Thanks.

Use SPIFFS:
https://www.esp8266.com/viewtopic.php?f=29&t=8194

Pete.

I tried adding this to my project, but it looks like messed up the spiff data in the wifi config settings because I got a lot of error stuff on the monitor.

int myFirstInt = 123;
int mySecondInt = 234;
void saveSettings() {
  //This will save the variables (integers) anytime the user wants to update them

  //I already called this once getting the wifi config file, do I need it again?
  SPIFFS.begin();

  // open file for writing
  File f = SPIFFS.open("/f.txt", "w");
  if (!f) {
    Serial.println("file open failed");
  }
  f.println(myFirstInt); //my val I need saved
  f.println(mySecondInt);

  //I'll have about 8 values to save
  f.close();
}

I called getSettings() in the setup()
Maybe I just need to add my variables to the same config file that the wifi settings are in. I’ll try that now.

Oh, I was calling getSettings() the first thing in my setup(). But I didn’t have SPIFFS.begin() in the getSettings() function. I added it in and it worked. It read my integers (though I I have to figure out how to get them from a string to an int), and then it got the wifi info and connected. I guess it doesn’t matter that I have SPIFF.begin() more than once. I have it in the wifi manager section, and then in these two functions I just previously posted getSettings() and saveSettings()

Is there a way to save a small struct array to the SPIFFS? These are my settings I need stored. I got the above simple code working but not sure how to save this whole thing

struct settings {
  uint16_t TC_M1_OFFSET;
  uint16_t TC_M2_OFFSET;
  uint16_t TC_FB_OFFSET;
  int fireboxOffsetTemp; //allow temp to drop this much below setpoint before sending alarm
  double aggKp;
  double aggKi;
  double aggKd;
  double consKp;
  double consKi;
  double consKd;
} s;

This is rather bulky, but I got it working. It saves 10 integers and floats to SPIFFS, and can read them back. If it doesn’t find the file (first time users), it will create one with default values.

void saveSettings() {
  SPIFFS.begin();

  // open file for writing
  File settings = SPIFFS.open("/settings.txt", "w");
  if (!settings) {
    Serial.println("file open failed");
  }
  settings.println(s.TC_M1_OFFSET);
  settings.println(s.TC_M2_OFFSET);
  settings.println(s.TC_FB_OFFSET);
  settings.println(s.fireboxOffsetTemp);
  settings.println(s.aggKp);
  settings.println(s.aggKi);
  settings.println(s.aggKd);
  settings.println(s.consKp);
  settings.println(s.consKi);
  settings.println(s.consKd);
  settings.close();
}


void getSettings() {
  SPIFFS.begin();
  // open file for reading
  File settings = SPIFFS.open("/settings.txt", "r");
  if (!settings) {
    Serial.println("file open failed");
    //makeSettings(); //create default values
  }
  for (int i = 1; i < 10; i++) { //I have 10 values stored
    String val = settings.readStringUntil('\n');
    s.TC_M1_OFFSET = val.toInt();
    val = settings.readStringUntil('\n');
    s.TC_M2_OFFSET = val.toInt();
    val = settings.readStringUntil('\n');
    s.TC_FB_OFFSET = val.toInt();
    val = settings.readStringUntil('\n');
    s.fireboxOffsetTemp = val.toInt();
    val = settings.readStringUntil('\n');
    s.aggKp = val.toFloat();
    val = settings.readStringUntil('\n');
    s.aggKi = val.toFloat();
    val = settings.readStringUntil('\n');
    s.aggKd = val.toFloat();
    val = settings.readStringUntil('\n');
    s.consKp = val.toFloat();
    val = settings.readStringUntil('\n');
    s.consKi = val.toFloat();
    val = settings.readStringUntil('\n');
    s.consKd = val.toFloat();
     settings.close();

    //Check that it worked now
    Serial.print("s.TC_M1_OFFSET is: ");
    Serial.println(s.TC_M1_OFFSET);
    Serial.print("s.consKd is: ");
    Serial.println(s.consKd);
    Serial.print("s.aggKd is: ");
    Serial.println(s.aggKd);
    Serial.print("s.fireboxOffsetTemp is: ");
    Serial.println(s.fireboxOffsetTemp);
  }
}

void makeSettings() { //create default values if the file doesn't exist
  Serial.println(F("First time run - No file found, so we'll create default values"));
  s.TC_M1_OFFSET = 0;
  s.TC_M2_OFFSET = 0;
  s.TC_FB_OFFSET = 0;
  s.fireboxOffsetTemp = 15;
  s.aggKp = 8;
  s.aggKi = 0.2;
  s.aggKd = 1;
  s.consKp = 2;
  s.consKi = 0.1;
  s.consKd = .5;
  saveSettings();
}