No More Hard Coding of SSID and Password: ESP8266 and Blynk

// 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 thermostate 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 (RandomNerd)  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 in conjunction with ESPxxxx.

// Import required libraries
#include <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);
  
  //
}//End of void setup()
 

}

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

}```

Please edit your post to add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

This is straightforward WiFiManager code and doesn’t even allow inputting of Blynk Auth codes via the captive portal, so I’m not sure of its value to forum members; but who knows.

Anyone looking for more info on using WiFiManager with Blynk should take a look here:

Pete.

The problem I was trying to resolve was connecting to Blynk server without hardcoding the SSID and Password into the program. I had to pass that information and this was my solution.

Yes, I realise that.
A quick search of the forum would have revealed hundreds of other forum users who faced the same issue, and that of not wanting to hard-code the Blynk Auth code as well, who are also using either WiFiManager or Blynk dynamic provisioning.

Pete.

Hey Pete,

I appreciate your responses but I did search the forums and examples. To tell you the truth it was very frustrating. I could not find any examples as eloquent and simple as the one I posted.

I just also went to the Blynk docs and could not find anything about dynamic provisioning. Yes it may be in forums or examples but where is a structured document on it?

Again thanks for taking the time to respond.

Regards,

Roger

Dear Roger
Many thanks for your support and help
many users searching for this solution, and you deserved to thank you for this effort
keep going, i will try your solution and feed you back
i am interested for this issues not to make ssid and password instead the code for ESP8266
Thanks

You can try blynk edgent.

not working for esp8266 specially for
BLYNK_PARAM_KV(“ssid” , BLYNK_WIFI_SSID)
BLYNK_PARAM_KV(“pass” , BLYNK_WIFI_PASS)
BLYNK_PARAM_KV(“auth” , BLYNK_AUTH_TOKEN)
have errors !!

and i need also to add my simple (on-off) code and don’t know how and where to put it ??

I’d suggest that you start a new "need help with my project " topic and provide as much details as possible.