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.