Reset Nodemcu WiFi credentials with flash button?

Hi friends. is there any way to resetting Nodemcu WiFi credentials configuration with nodemcu flash button ?
is any example for this ?

thanks

Double Reset Detector was mentioned a few days ago, see https://github.com/datacute/DoubleResetDetector

1 Like

thanks for reply. do you know how to define nodemcu flash button to this library ? maybe in DRD_ADDRESS ?

#include <DoubleResetDetector.h>

// Number of seconds after reset during which a 
// subseqent reset will be considered a double reset.
#define DRD_TIMEOUT 10

// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0

DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  
  Serial.begin(9600);
  Serial.println();
  Serial.println("DoubleResetDetector Example Program");
  Serial.println("-----------------------------------");

  if (drd.detectDoubleReset()) {
    Serial.println("Double Reset Detected");
    digitalWrite(LED_BUILTIN, LOW);
  } else {
    Serial.println("No Double Reset Detected");
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

void loop()
{
  // Call the double reset detector loop method every so often,
  // so that it can recognise when the timeout expires.
  // You can also call drd.stop() when you wish to no longer
  // consider the next reset as a double reset.
  drd.loop();
}

DRD_ADDRESS is simply the storage address (0) in RTC memory area to check if a reset was done within the DRD_TIMEOUT duration. Doesn’t look like you need to define the reset button as a reset is any reset, hardware or software.

1 Like

So I understand nodemcu or esp8266 DRD_ADDRESS is 0. how to adding the button in this library ? Can you guidance me ?

thanks

Does the sketch not work i.e. different LED states depending on interval between resets?

1 Like

I dont understand it :cry:
I just adding this library to my sketch and not worked

#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 "DHT.h"
#include <I2CSoilMoistureSensor.h>
#include <DoubleResetDetector.h>
// subseqent reset will be considered a double reset.
#define DRD_TIMEOUT 10
// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0
DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);
#define DHTPIN 2 // The pin you connect to
#define DHTTYPE DHT11   // DHT 11 Change this if you have a DHT22
DHT dht(DHTPIN, DHTTYPE,16); // Change this to 
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

int x = analogRead(A0);

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

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

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

char blynk_token[34] = "";

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();
  timer.setInterval(2000, sendDHT);

  //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");
      pinMode(LED_BUILTIN, OUTPUT);
  
  Serial.begin(9600);
  Serial.println();
  Serial.println("DoubleResetDetector Example Program");
  Serial.println("-----------------------------------");

  if (drd.detectDoubleReset()) {
    Serial.println("Double Reset Detected");
    digitalWrite(LED_BUILTIN, LOW);
  } else {
    Serial.println("No Double Reset Detected");
    digitalWrite(LED_BUILTIN, HIGH);
    }
  }
  //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("LabkhandGiah", "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("Motasel shod :)");   //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, server);
  Blynk.connect();
}

void sendDHT()
{
//Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  int hum = (int) h;
  int tem = (int) t;
//Write values to V04 and V05
  Blynk.virtualWrite(4, hum);
  Blynk.virtualWrite(5, tem);
    timer.setInterval(2000, moisture);
}
  void moisture(){

  int x = analogRead(A0);
  int y = map(x, 600, 230, 0, 100);
  if (y > 100)
{
  y = 100;
}
   Blynk.virtualWrite(V1, y);
}
  void loop()
{
  Blynk.run();
  timer.run();
  drd.loop();
}

You shouldn’t add “none Blynk code” to a Blynk sketch until you have tested the code without Blynk.

Just work through the basic sketch from GitHub until it works for you.

1 Like

Checked the library here with a WeMos and it works fine. Simple and effective.

1 Like

worked with flash button or reset button ?

Ah sorry, the reset button as there is no flash button on a WeMos.

1 Like

its worked without blynk code. I chekcked it with nodemcu by double tapping Reset button everything erased but I dont know how to add this to blynk code.

it erased your WiFi credentials?

WiFIManager uses SPIFFS to store parameters so RTC address 0 would have no effect on this.

I looked at your merged code and Arduino basics says you don’t duplicate entries into the merged sketch i.e. you have 2 Serial.begin(). Just keep the proper ESP rate of 115200.

The GitHub is just a basic example which you have to expand upon. Do you know the process of clearing parameters with WiFiManager?