DoubleResetDetector with WiFiManager

Hi guys,
I need help.
I can't figure out what I'm doing wrong. DoubleResetDetector didn't work in my sketch, a goal of double-clicking on button to reset WiFi settings (motherboard ESP32).

#define BLYNK_PRINT Serial
#include <FS.h>
#include "SPIFFS.h"
#include <WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <WebServer.h>
#include <SimpleTimer.h>
#include <BlynkSimpleEsp32.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <ESP_DoubleResetDetector.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define DRD_TIMEOUT 10
#define DRD_ADDRESS 0
#define ONE_WIRE_BUS 13
#define ESP_DRD_USE_SPIFFS      true
#define ESP_DRD_USE_EEPROM    true

DoubleResetDetector* drd;

char blynk_token[40] = "";
bool shouldSaveConfig = false;

void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress *sensorsUnique;
int countSensors;

void printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}


bool initialConfig = false;

void setup() {
  Serial.begin(9600);
  Serial.println();
  drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS); 
  delay(3000);

  //read configuration from FS json
  Serial.println("mounting FS...");
  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");
        }
        configFile.close();
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }

  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
  WiFiManager wifiManager;
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  wifiManager.addParameter(&custom_blynk_token);

if (drd->detectDoubleReset()) {
    Serial.println("Double Reset Detected");
    wifiManager.startConfigPortal("Blynk","password");
  } else {
    Serial.println("No Double Reset Detected");
    wifiManager.autoConnect("Blynk","password");
  }
  

 
  Serial.println("wifi connected");
  strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    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();
  }
  
  Serial.println();
  sensors.begin();
  countSensors = sensors.getDeviceCount();
  Serial.print("Found sensors: ");
  Serial.println(countSensors);
  sensorsUnique = new DeviceAddress[countSensors];

    if (sensors.isParasitePowerMode()) {
    Serial.println("Mode power is Parasite");
  } else {
    Serial.println("Mode power is Normal");
  }
  
  Serial.print("local ip : ");
  Serial.println(WiFi.localIP());
  Serial.print("Blynk Token : ");
  Serial.println(blynk_token);
  Blynk.config(blynk_token);
  bool result = Blynk.connect();

void loop() {
  
  Blynk.run();
  drd->loop();

Thanks a lot.

You have included an awful lot of libraries including at least one that you don't use in your code: wire.h

The code you posted is incomplete.

I don't use blynk. Tried it, didn't find it appealing for my purposes.

"Didn't work" is about as descriptive as your verbose comments. Does this line execute?

Serial.println("Double Reset Detected");
a,⸮z)⸮
⸮0⸮⸮
mounting FS...
mounted file system
reading config file
opened config file
{"blynk_token":"*************************"}
parsed json
*WM: [3] allocating params bytes: 20
*WM: [2] Added Parameter: blynk
No Double Reset Detected
*WM: [1] AutoConnect 
*WM: [2] ESP32 event handler enabled 
*WM: [2] Connecting as wifi client... 
*WM: [3] STA static IP:
*WM: [2] setSTAConfig static ip not set, skipping 
*WM: [1] Connecting to SAVED AP: Test
*WM: [3] Using Password: TEST123
*WM: [3] WiFi station enable 
*WM: [1] connectTimeout not set, ESP waitForConnectResult... 
*WM: [2] Connection result: WL_CONNECTED
*WM: [3] lastconxresult: WL_CONNECTED
*WM: [1] AutoConnect: SUCCESS 
*WM: [1] STA IP Address: 192.168.0.22
wifi connected

Found sensors: 0
Mode power is Normal
local ip : 192.168.0.22
Blynk Token : *****************
[4061] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP32

[4214] Connecting to blynk-cloud.com:80
[4566] Ready (ping: 127ms).
*WM: [3] freeing allocated params! 
*WM: [3] unloading

The first two lines are a double tap:
a,⸮z)⸮
⸮0⸮⸮
And I and I don't understand, why he believes there wasn't a double-pressed button:
No Double Reset Detected

Thanks

Very much need your help to finish the project.

Thanks a lot.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.