2016-09-20 9 views
3

Arduino ESP8266을 사용하여 SPIFSS의 구성 설정을 저장하고로드합니다. 이 ConfigFile.ino를 참조 예로 사용했습니다.Arduino ESP8266에 구성 매개 변수로드

https://github.com/esp8266/Arduino/blob/master/libraries/esp8266/examples/ConfigFile/ConfigFile.ino

이 기능은 변수에 구성 설정을로드 serverNameaccessToken.

bool loadConfig() { 
    File configFile = SPIFFS.open("/config.json", "r"); 
    if (!configFile) { 
    Serial.println("Failed to open config file"); 
    return false; 
    } 

    size_t size = configFile.size(); 
    if (size > 1024) { 
    Serial.println("Config file size is too large"); 
    return false; 
    } 

    // Allocate a buffer to store contents of the file. 
    std::unique_ptr<char[]> buf(new char[size]); 

    // We don't use String here because ArduinoJson library requires the input 
    // buffer to be mutable. If you don't use ArduinoJson, you may as well 
    // use configFile.readString instead. 
    configFile.readBytes(buf.get(), size); 

    StaticJsonBuffer<200> jsonBuffer; 
    JsonObject& json = jsonBuffer.parseObject(buf.get()); 

    if (!json.success()) { 
    Serial.println("Failed to parse config file"); 
    return false; 
    } 

    const char* serverName = json["serverName"]; 
    const char* accessToken = json["accessToken"]; 

    // Real world application would store these values in some variables for 
    // later use. 

    Serial.print("Loaded serverName: "); 
    Serial.println(serverName); 
    Serial.print("Loaded accessToken: "); 
    Serial.println(accessToken); 
    return true; 
} 

구성 설정을 구조체에로드하기 위해이 기능을 일부 수정했습니다.

struct ConfigSettingsStruct 
{ 
    String ssid; 
    String password; 
}; 

ConfigSettingsStruct ConfigSettings; 

bool loadConfig() { 
    File configFile = SPIFFS.open("/config.json", "r"); 
    if (!configFile) { 
     Serial.println("Failed to open config file"); 
     return false; 
    } 

    size_t size = configFile.size(); 
    if (size > 1024) { 
     Serial.println("Config file size is too large"); 
     return false; 
    } 

    // Allocate a buffer to store contents of the file. 
    std::unique_ptr<char[]> buf(new char[size]); 

    // We don't use String here because ArduinoJson library requires the input 
    // buffer to be mutable. If you don't use ArduinoJson, you may as well 
    // use configFile.readString instead. 
    configFile.readBytes(buf.get(), size); 

    StaticJsonBuffer<200> jsonBuffer; 
    JsonObject& json = jsonBuffer.parseObject(buf.get()); 

    if (!json.success()) { 
     Serial.println("Failed to parse config file"); 
     return false; 
    } 

    //const char* serverName = json["serverName"]; 
    //const char* accessToken = json["accessToken"]; 

    char ssid_[30]; 
    strcpy(ssid_, json["ssid"]); 
    ConfigSettings.ssid = String(ssid_); 

    char password_[30]; 
    strcpy(password_, json["password"]); 
    ConfigSettings.password = String(password_); 

    // Real world application would store these values in some variables for 
    // later use. 

    Serial.print("Loaded ssid: "); 
    Serial.println(ConfigSettings.ssid); 
    Serial.print("Loaded password: "); 
    Serial.println(ConfigSettings.password); 

    return true; 
} 

코드를 다운로드하고 ESP8266을 실행하면 WiFi 칩이 일부 스택 오류로 재설정됩니다. 내 코드에 어떤 문제가 있습니까? 구성 설정을 ConfigSettings에 올바르게로드하려면 어떻게해야합니까?

답변

2

질문에 코드에 아무런 문제가 없습니다. 그것은 작동해야합니다. 나는 스택 오류의 원인이 다른 곳에 있다고 강력히 의심한다. 코드를 다시 한 번 확인하십시오.

이것은 답변으로 계산되지 않지만 다른 곳을 찾는 데 도움이 될 수 있습니다. 잘못된 장소를보고있을 수 있습니다.

+1

당신 말이 맞습니다. 나는 틀린 장소를보고 있었다. 문제의 코드에는 아무런 문제가 없습니다. –

2

당신은 당신이하고 결국 그것을 무료 (세련된하지만 고전 아니다) malloc을 통해 일부 메모리를 할당하는 데 사용하는 것이 좋습니다

std::unique_ptr<char[]> buf(new char[size]); 

후 메모리 누수 가능성이 있습니다. 반환 전에 파일을 닫아야합니다.

또한 ssid 및 passphrase 버퍼 길이가 충분하지 않습니다. 최대 ssid 길이는 32이어야합니다. psk 기반 암호화가 있다고 가정하면 64로 전달 버퍼 길이를 늘려야합니다.

작지만; 어쩌면 당신은 네임 스페이스 내에서 정의 가능한 C++ 쓰레드에도 불구하고 struct 정의하기 전에 typedef를 추가 할 수 있다고 생각할 수 있습니다.

+0

맞습니다.'config.readBytes' 다음에'configFile.close();'가 있어야합니다. 여기에 몇 가지 예제 코드가 있습니다 : https://github.com/bblanchon/ArduinoJson/issues/421 – sMyles