ช่วยผมดูหน่อยครับ ESP8266 WifiManager

เคยใช้งานได้ปกติ แต่อัพกับบอร์ดใหม่ ไม่ทำงานครับ มันขึ้น
Invalid settings in EEPROM, trying with defaults

[code]
/*
   1MB flash sizee

   sonoff header
   1 - vcc 3v3
   2 - rx
   3 - tx
   4 - gnd
   5 - gpio 14

   esp8266 connections
   gpio  0 - button
   gpio 12 - relay
   gpio 13 - green led - active low
   gpio 14 - pin 5 on header

*/

#define SONOFF_BUTTON    D3
#define SONOFF_RELAY    12
#define SONOFF_LED      2//13
//#define SONOFF_INPUT    14

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

static bool BLYNK_ENABLED = true;

#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

#include <EEPROM.h>

#define EEPROM_SALT 12663
typedef struct {
  int   salt = EEPROM_SALT;
  char  blynkToken[33]  = "";
  char  blynkServer[33] = "blynk-cloud.com";
  char  blynkPort[6]    = "8442";
} WMSettings;

WMSettings settings;

#include <ArduinoOTA.h>


//for LED status
#include <Ticker.h>
#define D0 5//1
#define D2 4//2
#define D4 14//5
#define D5 13//7


Ticker ticker;
WidgetLED led1(10); //virtual pin 10
WidgetLED led2(11); //virtual pin 11
WidgetLED led3(12); //virtual pin 12
WidgetLED led4(13); //virtual pin 13
WidgetLED led5(14); //Status Online

const int CMD_WAIT = 0;
const int CMD_BUTTON_CHANGE = 1;

int cmd = CMD_WAIT;
int relayState = HIGH;

//inverted button state
int buttonState = HIGH;
int buttonState1 = HIGH;
static long startPress = 0;
static long startPress1 = 0;

void Status()
{
  int v1=digitalRead(D0);
  int v2=digitalRead(D2);
  int v3=digitalRead(D4);
  int v4=digitalRead(D5);
  if (v1==1){ Blynk.virtualWrite(10, 0); }else{ Blynk.virtualWrite(10, 255); }
  if (v2==1){ Blynk.virtualWrite(11, 0); }else{ Blynk.virtualWrite(11, 255); }
  if (v3==1){ Blynk.virtualWrite(12, 0); }else{ Blynk.virtualWrite(12, 255); }
  if (v4==1){ Blynk.virtualWrite(13, 0); }else{ Blynk.virtualWrite(13, 255); }  
}
void tick()
{
  //toggle state
  int state = digitalRead(SONOFF_LED);  // get the current state of GPIO1 pin
  digitalWrite(SONOFF_LED, !state);     // set pin to the opposite state
}

//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
  Serial.println("Entered config mode");
  Serial.println(WiFi.softAPIP());
  //if you used auto generated SSID, print it
  Serial.println(myWiFiManager->getConfigPortalSSID());
  //entered config mode, make led toggle faster
  ticker.attach(0.2, tick);
}



void setState(int s) {
  digitalWrite(SONOFF_RELAY, s);
  digitalWrite(SONOFF_LED, (s + 1) % 2); // led is active low
  Blynk.virtualWrite(6, s*255);
}

void turnOn() {
  relayState = LOW;
  setState(relayState);
}

void turnOff() {
  relayState = HIGH;
  setState(relayState);
}

void toggleState() {
  cmd = CMD_BUTTON_CHANGE;
}

//flag for saving data
bool shouldSaveConfig = false;

//callback notifying us of the need to save config
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}


void toggle() {
  Serial.println("toggle state");
  Serial.println(relayState);
  relayState = relayState == HIGH ? LOW : HIGH;
  setState(relayState);
}

void restart() {
  ESP.reset();
  delay(1000);
}

void reset() {
  //reset settings to defaults
  /*
    WMSettings defaults;
    settings = defaults;
    EEPROM.begin(512);
    EEPROM.put(0, settings);
    EEPROM.end();
  */
  //reset wifi credentials
  WiFi.disconnect();
  delay(1000);
  ESP.reset();
  delay(1000);
}


//off - button
BLYNK_WRITE(0) {
  int a = param.asInt();
  if (a != 0) {
   turnOff();
  }
}

//on - button
BLYNK_WRITE(1) {
  int a = param.asInt();
  if (a != 0) {
    turnOn();
  }
}

//toggle - button
BLYNK_WRITE(2) {
  int a = param.asInt();
  if (a != 0) {
    toggle();
  }
}

//restart - button
BLYNK_WRITE(3) {
  int a = param.asInt();
  if (a != 0) {
    restart();
  }
}

//restart - button
BLYNK_WRITE(4) {
  int a = param.asInt();
  if (a != 0) {
    reset();
  }
}

//status - display
BLYNK_READ(5) {
  Blynk.virtualWrite(5, relayState);
 
}

BLYNK_WRITE(6) {
//EEPROM.begin(100);
  int a = param.asInt();
  if (a != 0) {
   digitalWrite(D0,LOW);
 // EEPROM.write(0, 0);
  }
  else
  {
     digitalWrite(D0,HIGH);
  //  EEPROM.write(0, 1);
  }
 //EEPROM.commit();

  if (digitalRead(D0)==LOW)
  {
      led1.on();
  }
  else
  {
    led1.off();
  }
}

BLYNK_WRITE(7) {
//EEPROM.begin(100);
  int a = param.asInt();
  if (a != 0) {
  digitalWrite(D2,LOW);
 //EEPROM.write(1, 0);
  }
  else
  {
      digitalWrite(D2,HIGH);
    //  EEPROM.write(1, 1);
  }
// EEPROM.commit();
 
  if (digitalRead(D2)==LOW)
  {
      led2.on();
  }
  else
  {
    led2.off();
  }
}

BLYNK_WRITE(8) {
// EEPROM.begin(100);
  int a = param.asInt();
  if (a != 0) {
   digitalWrite(D4,LOW);
  // EEPROM.write(2, 0);
  }
  else
  {
     digitalWrite(D4,HIGH); 
   //  EEPROM.write(2, 1);
  }
//EEPROM.commit();

  if (digitalRead(D4)==LOW)
  {
      led3.on();
  }
  else
  {
    led3.off();
  }
}

BLYNK_WRITE(9) { 
 // EEPROM.begin(100);
  int a = param.asInt();
  if (a != 0) {
   digitalWrite(D5,LOW);
  //EEPROM.write(3, 0);
    led4.on();
  }
  else
  {
     digitalWrite(D5,HIGH); 
    //EEPROM.write(3, 1);
     led4.off();
  }
 //EEPROM.commit();
 }


void setup()
{
  Serial.begin(115200);

  //set led pin as output

  pinMode(SONOFF_LED, OUTPUT);
   
    pinMode(D0, OUTPUT);
    pinMode(D2, OUTPUT);
    pinMode(D4, OUTPUT);
    pinMode(D5, OUTPUT);
       digitalWrite(D0,HIGH);
       digitalWrite(D2,HIGH);
       digitalWrite(D4,HIGH);
       digitalWrite(D5,HIGH);  
       led1.off();
       led2.off();
       led3.off();
       led4.off();
  
  // start ticker with 0.5 because we start in AP mode and try to connect
  ticker.attach(0.6, tick);


  const char *hostname = "SMILE SERVER";

  WiFiManager wifiManager;
  //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
  wifiManager.setAPCallback(configModeCallback);

  //timeout - this will quit WiFiManager if it's not configured in 3 minutes, causing a restart
  wifiManager.setConfigPortalTimeout(180);

  //custom params
  EEPROM.begin(512);
  EEPROM.get(0, settings);
  EEPROM.end();

  if (settings.salt != EEPROM_SALT) {
    Serial.println("Invalid settings in EEPROM, trying with defaults");
    WMSettings defaults;
    settings = defaults;
  }

  Serial.println(settings.blynkToken);
  Serial.println(settings.blynkServer);
  Serial.println(settings.blynkPort);

  WiFiManagerParameter custom_blynk_text("Blynk config. <br/> No token to disable.");
  wifiManager.addParameter(&custom_blynk_text);

  WiFiManagerParameter custom_blynk_token("blynk-token", "blynk token", settings.blynkToken, 33);
  wifiManager.addParameter(&custom_blynk_token);

  WiFiManagerParameter custom_blynk_server("blynk-server", "blynk server", settings.blynkServer, 33);
  wifiManager.addParameter(&custom_blynk_server);

  WiFiManagerParameter custom_blynk_port("blynk-port", "blynk port", settings.blynkPort, 6);
  wifiManager.addParameter(&custom_blynk_port);

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  if (!wifiManager.autoConnect(hostname)) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  }

  //Serial.println(custom_blynk_token.getValue());
  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("Saving config");

    strcpy(settings.blynkToken, custom_blynk_token.getValue());
    strcpy(settings.blynkServer, custom_blynk_server.getValue());
    strcpy(settings.blynkPort, custom_blynk_port.getValue());

    Serial.println(settings.blynkToken);
    Serial.println(settings.blynkServer);
    Serial.println(settings.blynkPort);

    EEPROM.begin(512);
    EEPROM.put(0, settings);
    EEPROM.end();
  }

  //config blynk
  if (strlen(settings.blynkToken) == 0) {
    BLYNK_ENABLED = false;
  }
  if (BLYNK_ENABLED) {
    Blynk.config(settings.blynkToken, settings.blynkServer, atoi(settings.blynkPort));
  }

  //OTA
  ArduinoOTA.onStart([]() {
    Serial.println("Start OTA");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.setHostname(hostname);
  ArduinoOTA.begin();

  //if you get here you have connected to the WiFi
  Serial.println("connected...Smile :)");
  ticker.detach();

  //setup button
  pinMode(SONOFF_BUTTON, INPUT);
  attachInterrupt(SONOFF_BUTTON, toggleState, CHANGE);

  //setup relay
  pinMode(SONOFF_RELAY, OUTPUT);

  turnOn();
  
  Serial.println("done setup");
}


void loop()
{

 


  //ota loop
  ArduinoOTA.handle();
  //blynk connect and run loop
  if (BLYNK_ENABLED) {
    Blynk.run();
  }

    long duration1 = millis() - startPress1;
      if (duration1 > 2000)
      {
       if (buttonState1==LOW)
           {
             led5.on();
             buttonState1=HIGH;
           }
           else
           {
           led5.off();
            buttonState1=LOW;
           }
        
        Status();
        startPress1= millis();
      }
    

 int currentState = digitalRead(SONOFF_BUTTON);
 if (currentState == LOW)
 {
  long duration = millis() - startPress;
  if (duration > 5000)
      {
        reset();
      }
 }
 else
 {
   startPress = millis();
 }

}

อธิบายเพิ่มหน่อยว่าอัพโหลดไม่ได้หรือว่า อัพได้แต่ไม่ทำงาน

อัพได้ครับ แต่ไม่ทำงาน

แคป serial มาดูครับ

อะไรผิดพลาดไปหรือครับ

เรียบร้อยแล้วครับ ลงไลบารี่ ใหม่ เวอร์ชั่นต่ำลง

1 Like

เยี่ยมมากครับ อยากทราบว่าดาวน์เกรดลงมาเป็น version ไหน