wifiMananger

@luciano0609 Close… You where just using wrong key. It is the backtick, not the comma or apostrophe.

Blynk - FTFC

I fixed it for you this time :wink:

@luciano0609 esta faltando UM MONTE de codigo do wifi manager, sugiro voce usar esse exemplo e mesclar seu codigo AutoConnectWithFSParameters


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>  
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char blynk_token[] = "";

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

// Your WiFi credentials.
// Set password to "" for open networks.
//char ssid[] = "";
//char pass[] = "";


//SimpleTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();


int estadorele1 = LOW;
int estadorele2 = LOW;
int estadorele3 = LOW;
int estadobotao1 = HIGH;
int estadobotao2 = HIGH;
int estadobotao3 = HIGH;
Blynk.connect()

  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);

 BLYNK_WRITE(V1)
{
  estadorele1 = param.asInt();
  digitalWrite(5, estadorele1); 
}

void checkPhysicalButton1()
{
  if (digitalRead(16) == LOW)
  {
    if(estadobotao1 != LOW)
    {
      estadorele1 = !estadorele1;
      digitalWrite(5,estadorele1);
      Blynk.virtualWrite(V1, estadorele1);
    }
    estadobotao1 = LOW;
  }
  else
  {
    estadobotao1 = HIGH;
  }
}

BLYNK_WRITE(V2)
{
  estadorele2 = param.asInt();
  digitalWrite(4, estadorele2); 
}

void checkPhysicalButton2()
{
  if (digitalRead(14) == LOW)
  {
    if(estadobotao2 != LOW)
    {
      estadorele2 = !estadorele2;
      digitalWrite(4,estadorele2);
      Blynk.virtualWrite(V2, estadorele2);
    }
    estadobotao2 = LOW;
  }
  else
  {
    estadobotao2 = HIGH;
  }
}

BLYNK_WRITE(V3)
{
  estadorele3 = param.asInt();
  digitalWrite(2, estadorele3); 
}

void checkPhysicalButton3()
{
  if (digitalRead(12) == LOW)
  {
    if(estadobotao3 != LOW)
    {
      estadorele3 = !estadorele3;
      digitalWrite(2,estadorele3);
      Blynk.virtualWrite(V3, estadorele3);
    }
    estadobotao3 = LOW;
  }
  else
  {
    estadobotao3 = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(115200);
// wifi
  WiFiManager wifiManager;
  wifiManager.autoConnect("AutoConnectAP", "ronin");
  Serial.println("baglanti tamam :)");
   WiFiManagerParameter custom_blynk_token("Blynk", "blynk token", blynk_token, 34);
   wifiManager.addParameter(&custom_blynk_token);
   wifiManager.autoConnect("Blynk");
   Blynk.config(custom_blynk_token.getValue());
  pinMode(16, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(115200);
Blynk.config(blynk_token);
bool result = Blynk.connect(180);

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

  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);
  timer.setInterval(100L, checkPhysicalButton3);

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

Tentei mandar formatado de novo, o exemplo que você me indicou é com MQTT como altero para funcionar com a plataforma do blynk?

Tentei compilar o código que você me indicou e está da este erro

na imagem abaixo.

Aqui funciona normal, veja se você inclui todas as bibliotecas necessárias.

1 Like

porque está dando este erro?

Como eu disse anteriormente, cheque se você adicionou todas as biblioteca requeridas por esse código que esta tentando usar.

Se você procurar aqui vai encontrar vários exemplos de códigos usando o WiFi Manager, porem eh uma funcao mais avancada.

Se voce apenas quer colocar para ajudar na hora de mudar sua senha do Wifi acho que nao vale a pena visto que voce nao muda sua senha tao frequentemente.

1 Like

problem comes from the version of ArduinoJson used. (v6 beta)
you can not use this version with wifimanager
you need to install version 5.13
:wink:

Olá Alexis, tudo bem!
Obrigado esta parte consegui resolver, agora e o meu código que estou tentando incluir neste código, não estou conseguindo.
Poderia me ajudar?

Olá Luciano,

Yes I can try to help you,
please send us the code to insert , don’t forget backtips ```

#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

//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>
#include <SimpleTimer.h>
SimpleTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();
void tick()
{
  //toggle state
int estadorele1 = LOW;
int estadorele2 = LOW;
int estadorele3 = LOW;
int estadobotao1 = HIGH;
int estadobotao2 = HIGH;
int estadobotao3 = HIGH;
//int state = digitalRead(BUILTIN_LED);  // get the current state of GPIO1 pin
 // digitalWrite(BUILTIN_LED, !state);     // set pin to the opposite state
}
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
 }

BLYNK_WRITE(V1)
{
  estadorele1 = param.asInt();
  digitalWrite(5, estadorele1); 

}
void checkPhysicalButton1()
{
  if (digitalRead(12) == LOW)
  {
    if(estadobotao1 != LOW)
    {
      estadorele1 = !estadorele1;
      digitalWrite(5,estadorele1);
      Blynk.virtualWrite(V1, estadorele1);
    }
    estadobotao1 = LOW;
  }
  else
  {
    estadobotao1 = HIGH;
  }
}


BLYNK_WRITE(V2)
{
  estadorele2 = param.asInt();
  digitalWrite(4, estadorele2); 
}

void checkPhysicalButton2()
{
  if (digitalRead(14) == LOW)
  {
    if(estadobotao2 != LOW)
    {
      estadorele2 = !estadorele2;
      digitalWrite(4,estadorele2);
      Blynk.virtualWrite(V2, estadorele2);
    }
    estadobotao2 = LOW;
  }
  else
  {
    estadobotao2 = HIGH;
  }
}

BLYNK_WRITE(V3)
{
  estadorele3 = param.asInt();
  digitalWrite(2, estadorele3); 
}

void checkPhysicalButton3()
{
  if (digitalRead(16) == LOW)
  {
    if(estadobotao3 != LOW)
    {
      estadorele3 = !estadorele3;
      digitalWrite(2,estadorele3);
      Blynk.virtualWrite(V3, estadorele3);
    }
    estadobotao3 = LOW;
  }
  else
  {
    estadobotao3 = HIGH;
  }
}


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);
  pinMode(12, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(115200);
  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);
  timer.setInterval(100L, checkPhysicalButton3);
  
  // 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("CentralHeatingAP", "MY123PWD")) {
    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();

}

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

Me confirma se foi formatado, por favor.

sim está bem formatado !

but where is your problem ?
did you resolved wifimanager issue ?

Se eu fecho a chave que está na linha 36 da este erro:

Se eu retiro a chave da linha 36 da este erro:

I thing there is a missing bracket in your code
I will edit your code to see where it’s missing

ok.

Alexis, este é o código antes de incluir o meu código.

#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

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

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
}

//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
  Serial.println("Entered config mode");
  Serial.println(WiFi.softAPIP());
  //if you used auto generated SSID, print it
  Serial.println(myWiFiManager->getConfigPortalSSID());
  //entered config mode, make led toggle faster
  ticker.attach(0.2, tick);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  
  //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);

  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //reset settings - for testing
  //wifiManager.resetSettings();

  //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
  wifiManager.setAPCallback(configModeCallback);

  //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()) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
  ticker.detach();
  //keep LED on
  digitalWrite(BUILTIN_LED, LOW);
}

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

}

Desculpa, o exemplo errado.
O certo é este aqui.

#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 <ArduinoJson.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>
#include <SimpleTimer.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("CentralHeatingAP", "MY123PWD")) {
    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();

}

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

Não sei se vai ajudar, e este é meu código sem o wifiManager que está funcionando perfeitamente.

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




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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";


SimpleTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();


int estadorele1 = LOW;
int estadorele2 = LOW;
int estadorele3 = LOW;
int estadobotao1 = HIGH;
int estadobotao2 = HIGH;
int estadobotao3 = HIGH;
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
 }

BLYNK_WRITE(V0)
{
  estadorele1 = param.asInt();
  digitalWrite(5, estadorele1); 
}

void checkPhysicalButton1()
{
  if (digitalRead(13) == LOW)
  {
    if(estadobotao1 != LOW)
    {
      estadorele1 = !estadorele1;
      digitalWrite(5,estadorele1);
      Blynk.virtualWrite(V0, estadorele1);
    }
    estadobotao1 = LOW;
  }
  else
  {
    estadobotao1 = HIGH;
  }
}

BLYNK_WRITE(V1)
{
  estadorele2 = param.asInt();
  digitalWrite(4, estadorele2); 
}

void checkPhysicalButton2()
{
  if (digitalRead(14) == LOW)
  {
    if(estadobotao2 != LOW)
    {
      estadorele2 = !estadorele2;
      digitalWrite(4,estadorele2);
      Blynk.virtualWrite(V1, estadorele2);
    }
    estadobotao2 = LOW;
  }
  else
  {
    estadobotao2 = HIGH;
  }
}

BLYNK_WRITE(V2)
{
  estadorele3 = param.asInt();
  digitalWrite(2, estadorele3); 
}

void checkPhysicalButton3()
{
  if (digitalRead(12) == LOW)
  {
    if(estadobotao3 != LOW)
    {
      estadorele3 = !estadorele3;
      digitalWrite(2,estadorele3);
      Blynk.virtualWrite(V2, estadorele3);
    }
    estadobotao3 = LOW;
  }
  else
  {
    estadobotao3 = HIGH;
  }
}

void setup()
{
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);
  timer.setInterval(100L, checkPhysicalButton3);
}

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