Arduino--ESP8266--SmartConfig+EEPROM

39022 단어 ArduinoESP8266
/************************************************************************************/
/*
                               SmartConfig+EEPROM
                            :    APP          EEPROM      WIFI  
                 SmartConfig    :
                   1, 8266    station,  smartconfig,               。
                   2,                UDP  ;
                   3, 8266          smartconfig  ,    ;
                 EEPROM    
                   1.     
                   2.   
                   3.               flash       sector     ,         begin(1)       4096    。  size                     ,           size。
                   4.          

*/
/************************************************************************************/


/*
    SmartConfig     ESP8266WiFiSTA  
          
           bool beginSmartConfig(); //         :          
           bool stopSmartConfig();  //         :          
           bool smartConfigDone();  //         :        


     EEPROM    
          void begin(size_t size);                           //       ram    。size(4   )
          uint8_t read(int const address);                   //        address  0-4096
          void write(int const address, uint8_t const val);  //             8    
          bool commit();                                     //                       
          void end();                                        //     flash,        
*/
//#include 
#include    //WIFI    
#include        //    
#include           //I2C    
#include         //EEPROM



#define LED D9
long debouncdDelay = 500;  //    
long lastDebounceTime = 0; //         
//        
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ D6, /* data=*/ D5, /* reset=*/ U8X8_PIN_NONE);   // ESP32 Thing, pure SW emulated I2C
//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ D6, /* data=*/ D5);   // ESP32 Thing, HW I2C with pin remapping

//          (IP,  ,   )
//           
#define DebugBegin(baud_rate)    Serial.begin(baud_rate)
#define DebugPrintln(message)    Serial.println(message)
#define DebugPrint(message)      Serial.print(message)


struct config_type
{
  char stassid[32];
  char stapsw[64];
};
config_type config;

bool flag = 0;                           //  EEPROM  ,       
uint8_t WifiEEPROM_Buff[50] = {0};
uint8_t Time_val = 0;
/*
        EEPROM
*/
void saveConfig()
{
  Serial.println("Save config!");
  Serial.print("stassid:");
  Serial.println(config.stassid);
  Serial.print("stapsw:");
  Serial.println(config.stapsw);
  EEPROM.begin(1024);
  uint8_t *p = (uint8_t*)(&config);
  for (int i = 0; i < sizeof(config); i++)
  {
    EEPROM.write(i, *(p + i));
  }
  EEPROM.commit();
}

/*
    EEPROM    
*/
void loadConfig()
{
  EEPROM.begin(1024);
  uint8_t *p = (uint8_t*)(&config);
  for (int i = 0; i < sizeof(config); i++)
  {
    *(p + i) = EEPROM.read(i);
  }
  EEPROM.commit();
  Serial.println("-----Read config-----");
  Serial.print("stassid:");
  Serial.println(config.stassid);
  Serial.print("stapsw:");
  Serial.println(config.stapsw);
}
void Connect_STA(const char *wifiname, const char *psw)
{
  WiFi.begin(wifiname, psw);
  //           ,       500ms,       ,      
  //    :                   ,       
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Time_val++;
    DebugPrint(".");
    DebugPrint(WiFi.status());
    if (Time_val > 20)
    {
      Time_val = 0;
      flag = 0;
      return;
    }
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    flag = 1;
    //  mac  
    DebugPrintln(String("Connected, mac address: ") + WiFi.macAddress().c_str());

    //  station IP  ,   IP   DHCP  
    DebugPrintln(String("Connected, IP address: ") + WiFi.localIP().toString());

    //        
    DebugPrintln(String("Subnet mask: ") + WiFi.subnetMask().toString());

    //     IP  
    DebugPrintln(String("Gataway IP: ") + WiFi.gatewayIP().toString());

    //  hostname
    DebugPrintln(String("Default hostname: ") + WiFi.hostname());
    //    hostname
    WiFi.hostname("Station_host_1");
    DebugPrintln(String("New hostname: ") + WiFi.hostname());

    //  SSID
    DebugPrintln(String("SSID: ") + WiFi.SSID());

    //  psk
    DebugPrintln(String("psk: ") + WiFi.psk());

    //  BSSID
    DebugPrintln(String("BSSID: ") + WiFi.BSSIDstr());

    //  RSSI
    DebugPrintln(String("RSSI: ") + WiFi.RSSI() + " dBm");
  }
}
void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r
Wait for Smartconfig"
); delay(2000); // WiFi.beginSmartConfig(); while (1) { Serial.print("."); delay(500); Time_val++; if (Time_val > 20) { Time_val = 0; flag = 0; break; } if (WiFi.smartConfigDone()) { Serial.println("SmartConfig Success"); Serial.printf("SSID:%s\r
"
, WiFi.SSID().c_str()); Serial.printf("PSW:%s\r
"
, WiFi.psk().c_str()); WiFi.setAutoConnect(true); // strcpy(config.stassid, WiFi.SSID().c_str()); strcpy(config.stapsw, WiFi.psk().c_str()); saveConfig(); break; } } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP().toString()); } void setup() { // put your setup code here, to run once: pinMode(LED, OUTPUT); // led io digitalWrite(LED, LOW); // io pinMode(D5, OUTPUT); // SSD1306 SDA pinMode(D6, OUTPUT); // SSD1306 SCL digitalWrite(D5, 0); digitalWrite(D6, 0); DebugBegin(115200); // loadConfig(); // Connect_STA(config.stassid, config.stapsw); if(!flag) { DebugPrint("**************** , *************************" ); DebugPrint("/n"); smartConfig(); } u8g2.begin(); // u8g2.enableUTF8Print(); // print UTF8 ESP.wdtEnable(5000); // } void loop() { // put your main code here, to run repeatedly: ESP.wdtFeed(); // if (millis() - lastDebounceTime > debouncdDelay) { lastDebounceTime = millis(); // u8g2.firstPage(); do { u8g2.setFont(u8g2_font_5x8_tf); u8g2.setFontDirection(0); u8g2.drawStr(0, 8, " AP_SSID:"); u8g2.drawStr(0, 18, " AP_PSW:"); } while ( u8g2.nextPage() ); digitalWrite(D9, !digitalRead(D9)); // io IO } }

좋은 웹페이지 즐겨찾기