0

I modified the SD sketch for ESP8266 to read WIFI setting from a file named CONFIG.TXT. The text file's content format is SETTING=VALUE. Ex.

SSID=AP001
KEY=AP001

I manage to separate the SETTING and VALUE My code:

#include <SPI.h>
#include <SD.h>

File myFile;

void readWifiConfig() {
  Serial.println("Initializing SD card...");
  char temp;
  bool isSetting = true;
  if (!SD.begin(4)) {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("Initialization done.");
  myFile = SD.open("CONFIG.TXT");
  if (myFile) {
    Serial.println("CONFIG.TXT");
    while (myFile.available()) {
      temp = myFile.read();
      if (isSetting == true) {
        if (temp == '=') {
          Serial.print("="); //Writes '=' to console
          isSetting  = false;
        }
        else {
          Serial.print(temp); //Writes SETTING part to console
        }
      }
      else {
        if (temp == '\n' || temp == '\r') {
          isSetting = true;
          }
        else {
          Serial.print(temp); //Writes VALUE part to console
          }
        }
    }
    myFile.close();
  } else {
    Serial.println("Can't open CONFIG.TXT");
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10);
  readWifiConfig();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Output:

Initializing SD card...
Initialization done.
CONFIG.TXT
SSID=AP001
KEY=AP001

I want to write the values to their corresponding variables and I don't know how.

char* ssid;
char* key;
2
  • 3
    I don't see a question Feb 3 '18 at 14:32
  • You can't just write to a pointer, you need to allocate a buffer and copy the data there. Feb 3 '18 at 18:38
0

Somehow I managed to do it but my code is un-optimized. Please let me now if I've done something that isn't good.

I changed the content format to this so it's much easier for me. (CONFIG.TXT)

SSID%&WIFIAP001&%KEY%&PASS1234&

New Code.

#include <SPI.h>
#include <SD.h>

File myFile;
char wifiSsid[63];
char wifiKey[63];
char temp;
char settingSsid[5] = "SSID";
char settingKey[4] = "KEY";
char settingTemp[10];
char valueTemp[63];
int settingTempLastPos = 0;
int valueTempLastPos = 0;
int percentCount = 0;
int andCount = 0;
bool isSetting = true;
bool ssidValueFound = false;
bool keyValueFound = false;

void readWifiConfig() {
  Serial.println("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("Initialization done.");
  myFile = SD.open("CONFIG.TXT");
  if (myFile) {
    Serial.println("CONFIG.TXT");
    while (myFile.available()) {
      temp = myFile.read();
      if (temp == '%') {
        percentCount++;
        isSetting = true;
        if (percentCount == 2) {
          //Serial.println(settingTemp);
          if (strcmp(settingSsid,settingTemp) == 0) {
            Serial.println("SSID SETTING FOUND");
            //IF FOUND RAISE FLAG FOR ADDING VALUE TO SSID CHAR ARRAY
            ssidValueFound = true;
            }
          else if (strcmp(settingKey,settingTemp) == 0) {
            Serial.println("KEY SETTING FOUND");
            //IF FOUND RAISE FLAG FOR ADDING VALUE TO SSID CHAR ARRAY
            keyValueFound = true;
            }
          memset(&settingTemp[0], 0, sizeof(settingTemp));
          settingTempLastPos = 0;
          percentCount = 0;
        }
      }
      else if (temp == '&') {
        andCount++;
        isSetting = false;
        if (andCount == 2) {
          //Serial.println(valueTemp);
          if (ssidValueFound) {
            //Serial.println(valueTemp);
            memcpy(wifiSsid,valueTemp,63);
            //Serial.println(wifiSsid);
            ssidValueFound = false;
            }
          else if (keyValueFound) {
            //Serial.println(valueTemp);
            memcpy(wifiKey,valueTemp,63);
            //Serial.println(wifiKey);
            keyValueFound = false;
            }
          memset(&valueTemp[0], 0, sizeof(valueTemp));
          valueTempLastPos = 0;
          andCount = 0;
        }
      }
      else {
        if (isSetting == true) { //setting
          settingTemp[settingTempLastPos] = temp;
          settingTempLastPos++;
        }
        else if (isSetting == false) { //value
          valueTemp[valueTempLastPos] = temp;
          valueTempLastPos++;
        }
      }
    }
    //Serial.println(settingTemp);
    myFile.close();
  } else {
    Serial.println("Can't open CONFIG.TXT");
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10);
  readWifiConfig();
  Serial.println(wifiSsid);
  Serial.println(wifiKey);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Output:

Initializing SD card...
Initialization done.
CONFIG.TXT
SSID SETTING FOUND
KEY SETTING FOUND
WIFIAP001
PASS1234
0

New and improved code.

File format:

$SETTINGNAME=SETTINGVALUE;

New code:

#include <SPI.h>
#include <SD.h>

File configFile;
uint8_t filePosition;
uint8_t workingIndex = 0;
uint8_t settingNameValuePositions[16][3];
uint8_t temporaryBufferPosition = 0;
char temporaryBuffer[32];

void setPosition(uint8_t mrk) {
  settingNameValuePositions[workingIndex][mrk] = filePosition;
}

uint8_t getPosition(uint8_t idx, uint8_t mrk) {
  return settingNameValuePositions[idx][mrk];
}

void addToBuffer(char inp) {
  temporaryBuffer[temporaryBufferPosition] = inp;
  temporaryBufferPosition++;
}

void resetBuffer() {
  temporaryBufferPosition = 0;
  memset(temporaryBuffer, 0, sizeof(temporaryBuffer));
}

char* requestSetting(char *settingName) {
  for (int i = 0; i < workingIndex; i++) {
    uint8_t nS = getPosition(i,0); //$
    uint8_t eQ = getPosition(i,1); //=
    uint8_t vE = getPosition(i,2); //;
    bool isFound = false;
    resetBuffer();
    for (int g = nS; g < eQ - 1; g++) {
      configFile.seek(g);
      addToBuffer(configFile.read());
    }
    if (strcmp(temporaryBuffer,settingName) == 0) {
      isFound = true;
      resetBuffer();
    }
    for (int g = eQ; g < vE - 1; g++) {
      configFile.seek(g);
      addToBuffer(configFile.read());
    }
    if (isFound) {
      isFound = false;
      return temporaryBuffer;
    }
  }
  return "0";
}

bool readConfigFile(char* fileName) {
  //Serial.println("INITIALIZING SD CARD");
  if (!SD.begin()) {
    //Serial.println("INITIALIZING SD CARD FAILED");
    return false;
  }
  else {
    //Serial.println("INITIALIZING SD CARD COMPLETED");
    //Serial.println("OPENING FILE: " + String(fileName));
    configFile = SD.open(fileName);
    if (!configFile) {
      //Serial.println("ERROR OPENING FILE: " + String(fileName));
      return false;
    }
    else {
      //Serial.println("FILE " + fileName + " OPENED");
      //Serial.println("SEARCHING FOR MARKER POSITIONS");
      while (configFile.available()) {
        char fileChar = configFile.read();
        filePosition = configFile.position();
        switch (fileChar) {
          case '$':
            setPosition(0);
            break;
          case '=':
            setPosition(1);
            break;
          case ';':
            setPosition(2);
            workingIndex++;
        }
      }
    }
    return true;
  }
}

void setup() {
  Serial.begin(115200);
  readConfigFile("CONFIG.TXT"); //FILE NAME
  Serial.println(requestSetting("SETTINGNAME"));
}
void loop() {
}
0
uint8_t settingNameValuePositions[16][3];

If you have more than 16 variables it's going to crash your webserver.

If your config file has more than 256 characters, then you should use short instead of uint8_t.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.