BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

User Defined Wifi Setting for nodeMCU using Webpage and Wiping Network Credential Using Push Button for blynk platform

Hello Friends,
This is my first post and I am new learner on BLYNK platform

I worked on a reference code of WiFi credential, and developed code for setting WiFi credential using onboard push button and webpage as well as wiping WiFi credential using long press same push button for nodeMCU (esp devices) and blynk platform

/*Procedure

  • Load attached code with proper blynk generated Authentication token
  • LED at D0 starts blinking, which indicates nodeMCU is not connected to WiFi
  • Connect D3 for 5 sec to ground for wiping old SSID and password (optional for first time)
  • Short press(2sec) D3 pin to ground and then make it open again or HIGH
  • You will see OnDemandAP device in available wifi devices list in smartphone
  • Select “OnDemandAP” device
  • You will get IP address of 192.168.4.1 in serial monitor or you can check in smartphone also
  • Open 192.168.4.1 webpage and set your wifi credential
  • Then restart nodemcu again(Optional)
  • If nodeMCU connect to WiFI then D0 led will continuously glow
  • observe serial monitor for these above mentioned step
    */
//needed library for on Demand WiFi setting
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define TRIGGER_PIN 0  //D3 pin for user defined wifi setting, active HIGH at beginning of the program


 /*
Do not use for onDemandwifi setting 
char ssid[] = "User";
char pass[] = "Pass";
*/

int onDemandIndicator= D0; // wifi indicator blinking LED
char auth[] = "Auth";// You should get Auth Token in the Blynk App.

void wifiMysetting(){
   WiFiManager wifiManager;
   if ( digitalRead(TRIGGER_PIN) == LOW ) {
   delay(5000); //Long press D3 button for wiping WiFi setting 
     if ( digitalRead(TRIGGER_PIN) == LOW ) {
         wifiManager.resetSettings();    
       }
     }
     while (WiFi.status() != WL_CONNECTED){
      Serial.println(".");
      Serial.println("On demand wifi setting");
      digitalWrite(onDemandIndicator,LOW); //blinking led
      delay(200);
      digitalWrite(onDemandIndicator,HIGH); // blinking led
      delay(500);
      // is configuration portal requested?
      if ( digitalRead(TRIGGER_PIN) == LOW ) {
        //WiFiManager
        //Local initialization. Once its business is done, there is no need to keep it around
       //WiFiManager wifiManager;   //uncomment this if it is not declaring at other place
          if (!wifiManager.startConfigPortal("OnDemandAP")) {
            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 :)");
            delay(200);
          }
        }
        digitalWrite(onDemandIndicator,LOW);
        delay(1000);
      }
 //////////////////////////////////end ondemand code////////////

void setup(){
  pinMode(onDemandIndicator,OUTPUT);
  pinMode(TRIGGER_PIN, INPUT);   //user defined wifi pi active high during power on
  Serial.begin(115200);   //need for OnDemandWifi_for good result
  digitalWrite(onDemandIndicator,HIGH);
  wifiMysetting();    //check wifi status and call on demand wifi setting function
  //Blynk.begin(auth, ssid, pass);  //do not use for OnDemandWifi setting
  // WiFi.begin(ssid, pass); //do not use for OnDemandWifi setting
  Blynk.config(auth);  // in place of Blynk.begin(auth, ssid, pass);
  Blynk.connect(10000);  // timeout set to 10 seconds and then continue without Blynk
  while (Blynk.connect() == false) {
    // Wait until connected
    }
 }

void loop(){
  Blynk.run();
  wifiMysetting(); //check wifi status and call on demand wifi setting function
  }

BLYNK_WRITE(V1){
  //int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  int inX=param[0].asInt(); // joystick on V1 pin with merge mode
  int inY=param[1].asInt(); // joystick on V1 pin with merge mode
  Serial.print("incoming X =");
  Serial.println(inX);
  Serial.print("incoming Y =");
  Serial.println(inY);
}

1 Like

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:
```

Pete.

1 Like

Done,
Thank you so much sir