Esp32 need to configure / put in wifi ssid and password whithout hard coding

okay so here is the thing…need to be able to put ssid and password in my esp32 without hardcoding the same.

here is my code

#define BLYNK_HEARTBEAT   17   // must be BEFORE BlynkSimpleEsp8266.h works OK as 17s
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxx";
char ssid[] = "N"; // username or ssid of your WI-FI
char pass[] = "N"; // password of your Wi-Fi
char server[]          = "blynk-cloud.com";
unsigned int port      = 8442;

// Your WiFi credentials.
// Set password to "" for open networks.

// Set your LED and physical button pins here
const int ledPin1 = 23;
const int ledPin2 = 22;
const int ledPin3 = 21;
const int ledPin4 = 19;
const int ledPin5 = 18;
const int ledPin6 = 17;
const int ledPin7 = 16;
const int ledPin8 = 25;
const int btnPin1 = 15;
const int btnPin2 = 04;
const int btnPin3 = 13;
const int btnPin4 = 14;
const int btnPin5 = 27;
const int btnPin6 = 26;
const int btnPin7 = 33;
const int btnPin8 = 32;

void checkPhysicalButton();

int led1State = HIGH;
int btn1State = HIGH;

int led2State = HIGH;
int btn2State = HIGH;

int led3State = HIGH;
int btn3State = HIGH;

int led4State = HIGH;
int btn4State = HIGH;

int led5State = HIGH;
int btn5State = HIGH;

int led6State = HIGH;
int btn6State = HIGH;

int led7State = HIGH;
int btn7State = HIGH;

int led8State = HIGH;
int btn8State = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
 // Blynk.syncVirtual(V12);
 // Blynk.syncVirtual(V13);
 // Blynk.syncVirtual(V14);
 // Blynk.syncVirtual(V15);
 // Blynk.syncVirtual(V16);
 // Blynk.syncVirtual(V17);
 // Blynk.syncVirtual(V18);
 // Blynk.syncVirtual(V19);

  // Alternatively, you could override server state using:
  Blynk.virtualWrite(V12, led1State);
  Blynk.virtualWrite(V13, led2State);
  Blynk.virtualWrite(V14, led3State);
  Blynk.virtualWrite(V15, led4State);
  Blynk.virtualWrite(V16, led5State);
  Blynk.virtualWrite(V17, led6State);
  Blynk.virtualWrite(V18, led7State);
  Blynk.virtualWrite(V19, led8State);

}

// When App button is pushed - switch the state
BLYNK_WRITE(V12) {
  led1State = param.asInt();
  digitalWrite(ledPin1, led1State);
}
  
 BLYNK_WRITE(V13) {
  led2State = param.asInt();
  digitalWrite(ledPin2, led2State);
 }
BLYNK_WRITE(V14) {
  led3State = param.asInt();
  digitalWrite(ledPin3, led3State);
}
BLYNK_WRITE(V15) {
  led4State = param.asInt();
  digitalWrite(ledPin4, led4State);
}
BLYNK_WRITE(V16) {
  led5State = param.asInt();
  digitalWrite(ledPin5, led5State);
}
BLYNK_WRITE(V17) {
  led6State = param.asInt();
  digitalWrite(ledPin6, led6State);
}
BLYNK_WRITE(V18) {
  led7State = param.asInt();
  digitalWrite(ledPin7, led7State);
}
BLYNK_WRITE(V19) {
  led8State = param.asInt();
  digitalWrite(ledPin8, led8State);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin1) == LOW) {
    // btn1State is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      led1State = !led1State;
      digitalWrite(ledPin1, led1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, led1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      led2State = !led2State;
      digitalWrite(ledPin2, led2State);

      // Update Button Widget
      Blynk.virtualWrite(V13, led2State);
    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btnPin3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      led3State = !led3State;
      digitalWrite(ledPin3, led3State);

      // Update Button Widget
      Blynk.virtualWrite(V14, led3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btnPin4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      led4State = !led4State;
      digitalWrite(ledPin4, led4State);

      // Update Button Widget
      Blynk.virtualWrite(V15, led4State);
    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }

  if (digitalRead(btnPin5) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn5State != LOW) {

      // Toggle LED state
      led5State = !led5State;
      digitalWrite(ledPin5, led5State);

      // Update Button Widget
      Blynk.virtualWrite(V16, led5State);
    }
    btn5State = LOW;
  } else {
    btn5State = HIGH;
  }

  if (digitalRead(btnPin6) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn6State != LOW) {

      // Toggle LED state
      led6State = !led6State;
      digitalWrite(ledPin6, led6State);

      // Update Button Widget
      Blynk.virtualWrite(V17, led6State);
    }
    btn6State = LOW;
  } else {
    btn6State = HIGH;
  }

  if (digitalRead(btnPin7) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn7State != LOW) {

      // Toggle LED state
      led7State = !led7State;
      digitalWrite(ledPin7, led7State);

      // Update Button Widget
      Blynk.virtualWrite(V18, led7State);
    }
    btn7State = LOW;
  } else {
    btn7State = HIGH;
  }

  if (digitalRead(btnPin8) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn8State != LOW) {

      // Toggle LED state
      led8State = !led8State;
      digitalWrite(ledPin8, led8State);

      // Update Button Widget
      Blynk.virtualWrite(V19, led8State);
    }
    btn8State = LOW;
  } else {
    btn8State = HIGH;
  }
}

void setup()
{
  // Debug Console
  Serial.begin(115200);
  Serial.println();

  WiFi;
  WiFi.begin(ssid, pass);

   // line below is blocking code, consider regular WiFi connection with timeout if you have router problems
  Blynk.connectWiFi(ssid, pass); // used with Blynk.connect() in place of Blynk.begin(auth, ssid, pass, server, port);
 
// line below needs to be BEFORE Blynk.connect()
  timer.setInterval(11000L,CheckConnection); // check if still connected every 11s  

  Blynk.config(auth, server, port);
  Blynk.connect();    
   
//Serial.println("");
 //Serial.println("wifi connected...yeey :");
//Serial.println("IP address:");
//Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
 
  // Debug console
  //Serial.begin(9600);

  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, led1State);
  

  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin2, led2State);
  

  pinMode(ledPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(ledPin3, led3State);
  

  pinMode(ledPin4, OUTPUT);
  pinMode(btnPin4, INPUT_PULLUP);
  digitalWrite(ledPin4, led4State);


  pinMode(ledPin5, OUTPUT);
  pinMode(btnPin5, INPUT_PULLUP);
  digitalWrite(ledPin5, led5State);


  pinMode(ledPin6, OUTPUT);
  pinMode(btnPin6, INPUT_PULLUP);
  digitalWrite(ledPin6, led6State);


  pinMode(ledPin7, OUTPUT);
  pinMode(btnPin7, INPUT_PULLUP);
  digitalWrite(ledPin7, led7State);


  pinMode(ledPin8, OUTPUT);
  pinMode(btnPin8, INPUT_PULLUP);
  digitalWrite(ledPin8, led8State);

  // Setup a function to be called every 100 ms
  timer.setInterval(500L, checkPhysicalButton);
}

void CheckConnection(){    // check every 11s if connected to Blynk server
  if(!Blynk.connected()){
    Serial.println("Not connected to Blynk server"); 
    Blynk.connect();  // try to connect to server with default timeout
  }
  else{
    Serial.println("Connected to Blynk server");     
  }
}

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

same like wifimanager with esp8266…

WiFiManager wifiManager;
  wifiManager.autoConnect("MCU1", "1397");

tried this code but it isnt working (guessing it only supports esp8266 ) .

any help would be apprecaited…thank you…!

any help guys…? @Gunner, @PeteKnight.

thanks :slight_smile:

I don’t really use ESP32’s, or have many situations where i don’t want to hard-code the credentials.
I guess Blynk dynamic provisioning may be your answer, but once again it’s not something I’ve tried.

Pete.


// Roger Yacobucci 10/05/2019

//

// This code avoids hard coding the SSID and password when using Blynk.

// Of course it may be applicable in other situations.

// Have your Serial Monitor open to view the feedback during the

// connection process.

// I have tested it with the thermostat project I posted earlier

// and it works great. No more hard coding the SSID and password.

// This enables me to share my code and hardware without knowing

// a person's WiFi credentials.

//Rui and our group have provided a lot of this code; appreciated.

//I think the additional code I have incorporated will be of some value

//to our members especially when using Blynk.

// Import required libraries

[#include](https://www.facebook.com/hashtag/include?source=feed_text&epa=HASHTAG) <Arduino.h>

#include <ESP8266WiFi.h>

#include <WiFiManager.h>

#include <Hash.h>

#include <Adafruit_Sensor.h>

#include <BlynkSimpleEsp8266.h>

#include "blynk.h"

#include <DHT.h>

//Blynk Authorization

//This code is provided by Blynk and is specific to your Blynk application.

char auth[] = "Your Blynk Aurthorization Code";

// Network credentials

const char* ssid;

const char* pass;

// In the event that the hardware looses internet connection restore all Widget

// values based on the last values stored on the server.

BLYNK_CONNECTED()

{

Blynk.syncAll();

}

//Add your additional Blynk commands

void setup() {

// Serial port for debugging purposes

Serial.begin(115200);

// WiFiManager local initialization.

WiFiManager wifiManager;

// Uncomment and run it once, if you want to erase all the stored information

// wifiManager.resetSettings();

// Reads the ssid and password from ESP8266 EEPROM and tries to connect to WiFi

// If a valid ssid and password exits it will connect automatically and start an

// access point.

//

// If no valid ssid and password exist the "AutoConnectAP" application waits

// for the user to configure the WiFi connection. The user needs to go to

// the WiFi connection settings on their phone or computer to configure

// the connection.

//

// Once the WiFi connection is configured correctly the ESP8266 will connect.

wifiManager.autoConnect("AutoConnectAP");

// Store the network credentials as a const char* and then initialize Blynk

ssid = WiFi.SSID().c_str();

pass = WiFi.psk().c_str();

Blynk.begin(auth, ssid, pass);

//Initialize the temperature sensor.

dht.begin();

}//End of void setup()

}

void loop() {

// put your main code here, to run repeatedly:

}```

@Roger_Yacobucci how does this ESP8266 code help @swapnilkute2777 with his ESP32 query?

The WiFiManager GitHub site says that ESP32 support is on the wish list, but isn’t yet implemented:

Pete.

Hey Pete,

The ESP32 has additional capabilities over the ESP8266. I plan on getting one in the future so I did some research and I believe they are all part of the same family. Swapnilkute have you tried the code yet? Did it solve your problem?

Try this new development branch of Tzapu WifiManager:

https://github.com/tzapu/WiFiManager/tree/development

Although I haven’t tried this version yet, I’ve modified / fixed bugs from Tzapu’s WiFiManager version and used it reliably for more than a year.