Blynk App, to set esp8266-12e (Auth,ssid, and pass)

I am trying to use this code, I want to use the Blynk app
terminal window to upload the (Auth,ssid,and pass) to
the ESP8266-12E module.

But for some reason when I try to upload it to the
ESP8266-12E, it tells me, I am missing this.
ESP8266_Lib.h.

Here is where the code is
Change auth, ssid and pass from BLYNK terminal for NANO+ESP-01 project

Maybe why it is not working is because it is just for the ESP – 01.

If something works on a 01 it will always work on a 12 as a 12 is a 1 with extra pins. Not true in reverse though.

Your problem is Nano + 01 writes the code to the Nano where you are trying to write “Nano” code to the ESP.
They use different libraries.

Study the very basic examples of the different connection types and you will understand which libraries you need to use.

It is uploading from the Arduino ID to the
ESP8266-12E tell it gets to this line.
Blynk.begin(auth, wifi, ssid, pass);
It stops uploading.

Error = ’ wi-Fi’ was not declared in this scope
Does this happen when you don’t have updated libraries?

No that compilation error is normally when you just have bugs in your sketch.

There was a few versions of the Arduino IDE that made a regressive step whereby functions had to be “in order” but recent versions of the IDE are fine.

wi-Fi looks a very bad variable name to be using. It would normally be wifi, WIFI, WiFi but not wi-Fi.

The compilation error, without seeing your sketch, indicates a variable is being declared such as wifi and then you are trying to call the variable with wi-Fi.

Maybe post your FORMATTED sketch if you can’t find the problem.

Here is the code I am trying to upload.

/*
  set new wifi

  V15 - terminal
  V17 - button SETUP - setup new wifi data
  V18 - button -View default wifi data
  V19 - button - View new setup wifi data
  V20 - button OK - save to eeprom new setup wifi data
  v21 - button RED - read from eeprom setup wifi data (test only)

*/
/*

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //valid auth
char ssid[] = "aaaaaaaa";   // default ssid
char pass[] = "bbbbbbbbbbbb"; // default pass
*/
char auth[] = "6a9442c5d30d4e83a7d5c2xxxxxxxxx"; //valid auth
char ssid[] = "aaa";
char pass[] = "12345678";

char t_auth[] = "77778888999911112222333344445555"; // declare same 32 char
char t_ssid[] = "same AP name 123456789"; // declare
char t_pass[] = "same AP password 123456789"; // declare

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <EEPROM.h>

#define EspSerial Serial // Set ESP8266 Serial object
ESP8266 wifi(&EspSerial);


WidgetTerminal terminal(V15);

const int SW_pin = 8; //Working mode switch. HIGH = default WiFi. LOW = castom WiFi

String      y_ssid = "";
String      y_pass = "";
String      y_auth = "";
int wpis = 0;
int dl_i = 0;
int dl_j = 0;
int dl_k = 0;
BLYNK_WRITE(V15)
{
  String  coscos = param.asStr(); // string from terminal
  switch (wpis)
  {
    case 1:
      y_auth = coscos;  //from terminal
      dl_i = y_auth.length() + 1;  //change string to char[]
      y_auth.toCharArray(t_auth, dl_i); //change string to char[]
      break;

    case 2:
      y_ssid = coscos;
      dl_j = y_ssid.length() + 1;
      y_ssid.toCharArray(t_ssid, dl_j);
      break;

    case 3:
      y_pass = coscos;
      dl_k = y_pass.length() + 1;
      y_pass.toCharArray(t_pass, dl_k);
      break;
    default: ;
  }
}



BLYNK_WRITE(V17) //button SETUP
{
  String coscos;
  int klawin = param.asInt(); //klaw SETUP
  if (klawin == 1) {
    wpis = wpis + 1;
    if ((wpis > 4) || (wpis < 1)) {
      wpis = 1;
    }
 switch (wpis)
  {
    case 1:
      terminal.println("print your auth: ");
      terminal.flush();
      break;

    case 2:
      terminal.println("print your ssid: ");
      terminal.flush();
      break;

    case 3:
      terminal.println("print your pass: ");
      terminal.flush();
       break;

    case 4:
      terminal.print("your auth: ");
      terminal.println(t_auth);
      terminal.print("your ssid: ");
      terminal.println(t_ssid);
      terminal.print("your pass: ");
      terminal.println(t_pass);
      terminal.println("data wifi OK ?");
      terminal.flush();
      break;
    default: ;
  } 
  }
  
}

BLYNK_WRITE(V18)
{
  int klawplus = param.asInt();
  if (klawplus == 1) {
    terminal.println("**** DEF ****");
    terminal.print("auth: ");
    terminal.println(auth);
    terminal.print("ssid: ");
    terminal.println(ssid);
    terminal.print("pass: ");
    terminal.println(pass);
    terminal.println("********");
    terminal.flush();
  }
}

BLYNK_WRITE(V19)
{
  int klawminus = param.asInt();
  if (klawminus == 1) {
    terminal.println("**** SET ****");
    terminal.print("your_auth: ");
    terminal.println(t_auth);
    terminal.print("your_ssid: ");
    terminal.println(t_ssid);
    terminal.print("your_pass: ");
    terminal.println(t_pass);
    terminal.println("********");
    terminal.flush();
  }

}

BLYNK_WRITE(V20) //button OK
{
  int klawok = param.asInt();
  if (klawok == 1) {

    int eeAddr = 0;
    EEPROM.put(eeAddr, auth); //................................auth not change

    eeAddr = 50;
    EEPROM.put(eeAddr, t_ssid);

    eeAddr = 100;
    EEPROM.put(eeAddr, t_pass);

    terminal.println("save new WiFi data");
    terminal.flush();

  }
}


BLYNK_WRITE(V21) // button READ for test only
{
  int  klawread = param.asInt();
  if (klawread == 1) {

    int eeAddr = 0;
    EEPROM.get(eeAddr, t_auth);

    eeAddr = 50;
    EEPROM.get(eeAddr, t_ssid);

    eeAddr = 100;
    EEPROM.get(eeAddr, t_pass);

    terminal.print("your data from ee ");
    terminal.println(t_auth);
    terminal.println(t_ssid);
    terminal.println(t_pass);
    terminal.flush();
  }
}

void wyborwifi() // choice wifi setup data after reset - SW_pin = HIGH > default wifi, LOW > new setup wifi
{
  pinMode(SW_pin, INPUT_PULLUP);

  int m = digitalRead(SW_pin);
  if (m == 0) {

    int eeAddr = 0;
    EEPROM.get(eeAddr, t_auth);

    eeAddr = 50;
    EEPROM.get(eeAddr, t_ssid);

    eeAddr = 100;
    EEPROM.get(eeAddr, t_pass);

    Blynk.begin(t_auth, wifi, t_ssid, t_pass);
  }
  else
  {
    Blynk.begin(auth, wifi, ssid, pass);
  }

}

//+++++++++++++++++++++++++++++++++++++++++++++++++++   setup

void setup() {

  Serial.begin(115200);  // Set console baud rate
  EspSerial.begin(115200);  // Set ESP8266 baud rate

  wyborwifi();

  Serial.println("Arduino.cc 1.6.9"); //  Arduino.org  doesn't have  EEPROM.get i EEPROM.put functions
}

void loop() {
  Blynk.run();
}`Preformatted text`

FORMATTED being the operative word.

@Costas
I have readed this arduino nano+esp 01 thread.
But there is one problem your esp need to connected to access point.& Also connected to Blynk.

This is not a problem. You have an AP on your Smartphone. Job done.

Does it have option of changing ip for local server???

Yes.

Still will now upload sketch to esp8266-12e.

Here is all my settings.
Arduino 1.6.7
Board: NodeMCU 1.0 (esp-12e Module)
Port “dev/cu.SLAB_USBtoUart”
esp8266-12e

What am I doing wrong?

Here is my code and still, it is telling me “Wifi” was not declared in this scope,
ARDUINO SKETCH… I have the Arduino 1.6.12
I guess this is not for me…


/*
  set new wifi

  V15 - terminal
  V17 - button SETUP - setup new wifi data
  V18 - button -View default wifi data
  V19 - button - View new setup wifi data
  V20 - button OK - save to eeprom new setup wifi data
  v21 - button RED - read from eeprom setup wifi data (test only)

*/
/*

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //valid auth
char ssid[] = "aaaaaaaa";   // default ssid
char pass[] = "bbbbbbbbbbbb"; // default pass
*/
char auth[] = "6a9442c5d30d4e83a7d5c2xxxxxxxxx"; //valid auth
char ssid[] = "aaa";
char pass[] = "12345678";

char t_auth[] = "77778888999911112222333344445555"; // declare same 32 char
char t_ssid[] = "same AP name 123456789"; // declare
char t_pass[] = "same AP password 123456789"; // declare


#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>

//#include <ESP8266_Lib.h>
//#include <BlynkSimpleShieldEsp8266.h>
#include <EEPROM.h>


 
//#define EspSerial Serial // Set ESP8266 Serial object
ESP8266 Wifi(&EspSerial);


WidgetTerminal terminal(V15);

const int SW_pin = 8; //Working mode switch. HIGH = default WiFi. LOW = castom WiFi

String      y_ssid = "";
String      y_pass = "";
String      y_auth = "";
int wpis = 0;
int dl_i = 0;
int dl_j = 0;
int dl_k = 0;
BLYNK_WRITE(V15)
{
  String  coscos = param.asStr(); // string from terminal
  switch (wpis)
  {
    case 1:
      y_auth = coscos;  //from terminal
      dl_i = y_auth.length() + 1;  //change string to char[]
      y_auth.toCharArray(t_auth, dl_i); //change string to char[]
      break;

    case 2:
      y_ssid = coscos;
      dl_j = y_ssid.length() + 1;
      y_ssid.toCharArray(t_ssid, dl_j);
      break;

    case 3:
      y_pass = coscos;
      dl_k = y_pass.length() + 1;
      y_pass.toCharArray(t_pass, dl_k);
      break;
    default: ;
  }
}



BLYNK_WRITE(V17) //button SETUP
{
  String coscos;
  int klawin = param.asInt(); //klaw SETUP
  if (klawin == 1) {
    wpis = wpis + 1;
    if ((wpis > 4) || (wpis < 1)) {
      wpis = 1;
    }
 switch (wpis)
  {
    case 1:
      terminal.println("print your auth: ");
      terminal.flush();
      break;

    case 2:
      terminal.println("print your ssid: ");
      terminal.flush();
      break;

    case 3:
      terminal.println("print your pass: ");
      terminal.flush();
       break;

    case 4:
      terminal.print("your auth: ");
      terminal.println(t_auth);
      terminal.print("your ssid: ");
      terminal.println(t_ssid);
      terminal.print("your pass: ");
      terminal.println(t_pass);
      terminal.println("data wifi OK ?");
      terminal.flush();
      break;
    default: ;
  } 
  }
  
}

BLYNK_WRITE(V18)
{
  int klawplus = param.asInt();
  if (klawplus == 1) {
    terminal.println("**** DEF ****");
    terminal.print("auth: ");
    terminal.println(auth);
    terminal.print("ssid: ");
    terminal.println(ssid);
    terminal.print("pass: ");
    terminal.println(pass);
    terminal.println("********");
    terminal.flush();
  }
}

BLYNK_WRITE(V19)
{
  int klawminus = param.asInt();
  if (klawminus == 1) {
    terminal.println("**** SET ****");
    terminal.print("your_auth: ");
    terminal.println(t_auth);
    terminal.print("your_ssid: ");
    terminal.println(t_ssid);
    terminal.print("your_pass: ");
    terminal.println(t_pass);
    terminal.println("********");
    terminal.flush();
  }

}

BLYNK_WRITE(V20) //button OK
{
  int klawok = param.asInt();
  if (klawok == 1) {

    int eeAddr = 0;
    EEPROM.put(eeAddr, auth); //................................auth not change

    eeAddr = 50;
    EEPROM.put(eeAddr, t_ssid);

    eeAddr = 100;
    EEPROM.put(eeAddr, t_pass);

    terminal.println("save new WiFi data");
    terminal.flush();

  }
}


BLYNK_WRITE(V21) // button READ for test only
{
  int  klawread = param.asInt();
  if (klawread == 1) {

    int eeAddr = 0;
    EEPROM.get(eeAddr, t_auth);

    eeAddr = 50;
    EEPROM.get(eeAddr, t_ssid);

    eeAddr = 100;
    EEPROM.get(eeAddr, t_pass);

    terminal.print("your data from ee ");
    terminal.println(t_auth);
    terminal.println(t_ssid);
    terminal.println(t_pass);
    terminal.flush();
  }
}

void wyborwifi() // choice wifi setup data after reset - SW_pin = HIGH > default wifi, LOW > new setup wifi
{
  pinMode(SW_pin, INPUT_PULLUP);

  int m = digitalRead(SW_pin);
  if (m == 0) {

    int eeAddr = 0;
    EEPROM.get(eeAddr, t_auth);

    eeAddr = 50;
    EEPROM.get(eeAddr, t_ssid);

    eeAddr = 100;
    EEPROM.get(eeAddr, t_pass);

    Blynk.begin(t_auth, Wifi, t_ssid, t_pass);
  }
  else
  {
    Blynk.begin(auth, Wifi, ssid, pass);
  }

}

//+++++++++++++++++++++++++++++++++++++++++++++++++++   setup

void setup() {

  Serial.begin(115200);  // Set console baud rate
  EspSerial.begin(115200);  // Set ESP8266 baud rate

  wyborWifi();

  Serial.println("Arduino.cc 1.6.9"); //  Arduino.org  doesn't have  EEPROM.get i EEPROM.put functions
}

void loop() {
  Blynk.run();
}

Thanks for the help

1 Like

ESP8266 Wifi(&EspSerial);

I don’t see a define for &EspSerial, so it won’t know it’s on a different port.

Did you hook up both the USB for serial monitoring and the ESP to the Tx/Rx pins of the Nano? Because that will not work since it’s the same serial port. Are you using software serial or not? Because that would make a difference anyhow :slight_smile:

@Ben can you please describe your hardware setup as there is some confusion over which libraries you should be using. You made reference to ESP12 and ESP01 but it’s not clear if the ESP is a WiFI shield or if you are using the ESP in standalone mode.

The thread that you initially linked to started as a Nano with an ESP01 as a WiFi shield but it also included sketches for ESP’s in standalone mode.

When I look through your sketch I see you are mixing up the required libraries.

When I modified @krzyspx’s sketch from Nano with ESP to ESP standalone I commented out the following 2 lines that appear in your sketch as rows 31 and 32.

//#include <ESP8266_Lib.h>
//#include <BlynkSimpleShieldEsp8266.h>

and the following line that appears at row 37 in your sketch.

//#define EspSerial Serial // Set ESP8266 Serial object

So you need to reinstate them if you are Nano with ESP and comment out the ESP standalone libraries
(rows 27 and 29 in your sketch).

#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>

You will still get one further compilation error because at line 198 you have:

void wyborwifi()

and at line 230 you have

  wyborWifi();

Change line 230 to:

  wyborwifi();

The sketch will now compile for Nano with ESP.
If you hardware is ESP in standalone mode let me know and I will give you the required changes.

1 Like

Here’s the end result what I am shooting for, a way to upload the (auth,ssid,and pass),
other than using the My computer, to upload the code using serial TX and RX.
So I don’t want to have to change my sketch and upload every time I want to change the ssid, pass, and auth.
In retrospect I want to use the Blynk app to be able to save and erase this data to the ESP.

Serial data from my computer to the ESP8266-12e.

I am using the same set up as in this video.
(Arduino IDE at the end of the movie)

As of right now this code will upload to the NodeMCU 1.0 (esp8266-12e).

//PinMode(5) = D3 esp12e
//Switch Pull up Resister 3.3 Volts
//Switch to Ground

#define BLYNK_PRINT Serial // Comment this out to disable prints and save 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxx";

int buttonStatePrev;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, "xxxxxxxx", "xxxxxx");
  while (Blynk.connect() == false) {
    // Wait until connected
  }
  pinMode(5, INPUT);
  buttonStatePrev = HIGH;
}

void loop() {
  Blynk.run();
  
  int buttonStateCurr = digitalRead(5);
  if (buttonStateCurr == LOW && buttonStatePrev == HIGH) {
    Serial.println("Button is held down but activated the function only ONCE.");
    Blynk.notify("Device started");
    delay(50);
    
  }
  buttonStatePrev = buttonStateCurr;
}

@Ben OK so you are using the ESP in standalone mode i.e. without an Arduino.

So you can ignore most of my last post that was ensuring the sketch worked with an Arduino using ESP as a WiFi shield.

The problem you was having regarding “wifi” is that is only required when using the ESP as a WiFi shield for an Arduino.

The format for ESP in standalone mode is:

Blynk.begin(auth, ssid, pass, server);

Below is my last sketch in the thread you linked to and this is for ESP in standalone mode.
I have just checked and it compiles for the nodemcu.

/* EEPROMhandler.ino  ESP EEPROM Read and write for Blynk by Costas 12th Sept 2016
 * Remember char myChar = 'A' and char myChar = 65 are equivalent   */
  #include <EEPROM.h>             // ESP8266 EEPROM library
  #define BLYNK_PRINT Serial      // Comment this out to disable prints and save space
  #include <ArduinoOTA.h>         // for local OTA updates
  #include <ESP8266WiFi.h>
  #include <BlynkSimpleEsp8266.h>
  #include <SimpleTimer.h>        // Essential for almost all sketches
  SimpleTimer timer;

  //********************* SECTION FOR YOU TO COMPLETE WITH YOUR DETAILS ***************************
  // You should get Auth Token in the Blynk App.
  char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Go to the Project Settings (nut icon).
  char cloudserver[16] = "blynk-cloud.com";
  char localserver[16] = "192.168.10.229";         // Enter your IP details for the local server
  char ssid[] = "MTN WIFI 19996";                  // Your WiFi credentials.
  char pass[] = "xxxxxxxxxx";                      // Set password to "" for open networks.
  char OTAhost[] = "EEPROMhandler";                // Optional, but very useful
  //***********************************************************************************************
          
  String theData = "BLYNK  Token size 32  characters";     // e.g Your String to be stored in EEPROM
  char DataRetrieved[33];  // char array of data retrieved from EEPROM
  int DataLocation;  // e.g. address 0 for bootflag, 1 to 33 for token, 34 to 66 for SSID and 67 to 99 for PWD

void setup() {
  Serial.begin(115200);
  Serial.println("\Starting");  
  EEPROM.begin(100); // max 512 bytes of EEPROM starting at address 0
  DataLocation = 1; // e.g. for Blynk token
  EEPROMputE(DataLocation, theData);  // save the TOKEN in EEPROM
  delay(50);
  EEPROMgetE(DataLocation, theData);
  delay(50);
  // ****************************Now check EEPROM write and read******************************
  if(DataRetrieved[18] == '3'){ 
    Serial.println("Smartphone AP boot - row 36");
  }
  if(DataRetrieved[19] == '2'){  
    Serial.println("EEPROM boot - row 39");
  }
  // ****************************End of checking EEPROM write and read************************ 
  Blynk.begin(auth, ssid, pass, localserver);   // change localserver to cloudserver for Blynk cloud server         
  int mytimeout = millis() / 1000;
  while (Blynk.connect(1000) == false) {        // wait here until connected to the server
    if((millis() / 1000) > mytimeout + 8){      // try to connect to the server for less than 9 seconds
      break;                                    // continue with the sketch regardless of connection to the server
    }
  }    
  ArduinoOTA.setHostname(OTAhost);   
  ArduinoOTA.begin();                           // for local OTA updates   
}

void EEPROMputE(int DataLocation, String theData){  // Store DATA in EEPROM
  int lenofstr = theData.length() + 1;  // holds length of the String PLUS 1 to cover null terminator of array
  char Data2bStored[lenofstr];  // e.g 32 characters + 1 null terminator for Blynk TOKEN field, also max for SSID and PWD       
  theData.toCharArray(Data2bStored, lenofstr); 
  for(int j = 0; j < (theData.length()); j++){
    EEPROM.write(DataLocation + j, Data2bStored[j]);
    EEPROM.commit();
    delay(5);  
  } 
  Serial.println("DATA stored OK"); 
}

void EEPROMgetE(int DataLocation, String theData){     // Read DATA from EEPROM
  char value;                                    // Each retrieved char from EEPROM
  for(int k = 0; k < (theData.length()); k++){   // Token, SSID and PWD 32 character fields
    value = EEPROM.read(DataLocation + k);
    DataRetrieved[k] = value;  // place characters retrieved from EEPROM in char array
                        // ****Comment out this Serial print section******* 
    if((DataLocation + k)< 10){
      Serial.print(" ");                         // pad out address by 1 character for display purposes
    }
    Serial.print(DataLocation + k);
    Serial.print("   ");
    Serial.println(value);
                       //  ****End of Serial print section*****************
    delay(5); 
  } 
  Serial.print("DATA read OK of: \""); 
  Serial.print(DataRetrieved);
  Serial.println("\"");  
}

void reconnectBlynk() {                         // reconnect to server if disconnected, timer checks every 60 seconds
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void loop() {
  if (Blynk.connected()) {   // to ensure that Blynk.run() function is only called if we are still connected to the server
    Blynk.run();
  }
  timer.run();
  ArduinoOTA.handle();       // for local OTA updates  
  yield();
}
1 Like

check out this library - I have used it before and it is great https://github.com/tzapu/WiFiManager

Thank you all for your help but I guess I’m really bad at communicating, what I really want the ESP8266-12e.
Is to be a standalone web socket that blank app can change the ssid,pass,and auth.
Like they are doing in this web page but not for ifttt,
for a Blynk.

Or like this but with, Without all the other stuff added to it just simple.

But you people,have helped me out a lot I am happy.

i understand what you want. i think you need wiffi manager.
GitHub - tzapu/WiFiManager: ESP8266 WiFi Connection manager with web captive portal.
install wiffi manager library.
then try this code.

/*AP name-ESP password-12345678 */
#include <FS.h>                   //this needs to be first, or it all crashes and burns...
//#define BLYNK_DEBUG           // Comment this out to disable debug and save space
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <SimpleTimer.h>

//for LED status
#include <Ticker.h>
Ticker ticker;


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

char blynk_token[34] = "BLYNK_TOKEN";

bool shouldSaveConfig = false; //flag for saving data

#include <BlynkSimpleEsp8266.h>

SimpleTimer timer;

void tick()
{
  //toggle state
  int state = digitalRead(BUILTIN_LED);  // get the current state of GPIO1 pin
  digitalWrite(BUILTIN_LED, !state);     // set pin to the opposite state
}

void saveConfigCallback () {  //callback notifying us of the need to save config
  Serial.println("Should save config");
  shouldSaveConfig = true;
  ticker.attach(0.2, tick);  // led toggle faster
}

void setup()
{

  Serial.begin(115200);
  Serial.println();

  //set led pin as output
  pinMode(BUILTIN_LED, OUTPUT);
  // start ticker with 0.5 because we start in AP mode and try to connect
  ticker.attach(0.6, tick);

  //SPIFFS.format();    //clean FS, for testing
  Serial.println("Mounting FS...");    //read configuration from FS json

  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);   // was 32 length
  
  Serial.println(blynk_token);

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

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

  //set static ip
  // this is for connecting to Office router not GargoyleTest but it can be changed in AP mode at 192.168.4.1
  //wifiManager.setSTAStaticIPConfig(IPAddress(192,168,10,111), IPAddress(192,168,10,90), IPAddress(255,255,255,0));
  
  wifiManager.addParameter(&custom_blynk_token);   //add all your parameters here

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

  //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(600);   // 10 minutes to enter data and then Wemos resets to try again.

  //fetches ssid and pass and tries to connect, if it does not connect it starts an access point with the specified name
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect("ESP", "12345678")) {
    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);
  }
  Serial.println("Connected Central Heating System :)");   //if you get here you have connected to the WiFi
  ticker.detach();
  //turn LED off
  digitalWrite(BUILTIN_LED, HIGH);

  strcpy(blynk_token, custom_blynk_token.getValue());    //read updated parameters

  if (shouldSaveConfig) {      //save the custom parameters to FS
    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);
  Blynk.connect();
//------------------------------------------------------your setup------------------------------//
}




  void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer  
}
1 Like