Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to store & read back multiple char* values #18

Open
titusece opened this issue Jul 11, 2022 · 2 comments
Open

Unable to store & read back multiple char* values #18

titusece opened this issue Jul 11, 2022 · 2 comments

Comments

@titusece
Copy link

My intention is that getting WiFi SSID & password (currently hardcoded in code itself) and store it in filesystem and should use the same from next boot onwards. You can see the log that getting last PASSWORD updated value for SSID & PASSWORD too as 'test_passwdtest_passwd12@123'

_fileSystem.openFromFile("/ssid.txt", wifi_ssid);//Issue: getting WiFi password here
fileSystem.openFromFile("/password.txt", wifi_passwd);//getting WiFi password here
Serial.println("FS WiFi details after update");
Serial.println(wifi_ssid);
Serial.println(wifi_passwd);
setup_wifi(wifi_ssid, wifi_passwd);_

Code:

#include <ESP8266WiFi.h>
#include <Effortless_SPIFFS.h>

char *ssid_sta;
char *password_sta;

char* wifi_ssid;
char* wifi_passwd;

// Create fileSystem
eSPIFFS fileSystem;

bool writeToFlash = false;                                   // default value will be overriden by opening file

int run_loop = 0;

void setup() {
  delay(3000);
  Serial.begin(115200);
  Serial.println();

  // Check Flash Size - Always try to incorrperate a check when not debugging to know if you have set the SPIFFS correctly
  if (!fileSystem.checkFlashConfig()) {
    Serial.println("Flash size was not correct! Please check your SPIFFS config and try again");
    delay(1000);
    ESP.restart();
  }  

  // This will change value through reboots
  fileSystem.openFromFile("/writeToFlash.txt", writeToFlash);  // This will open the value of writeToFlash

  password_sta = (char *) malloc(100);
  ssid_sta = (char *) malloc(100);

//Actual: Get it from user webpage; hardcode for now
  String input_message;
  input_message = "test12 wifi detected!!!";
  strcpy(ssid_sta, input_message.c_str());
  input_message = "test_passwdtest_passwd12@123";
  strcpy(password_sta, input_message.c_str());
  
}
void setup_wifi(char *ssid_sta, char *password_sta) {

  static int count = 0;
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid_sta);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid_sta, password_sta);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.print(".");
    count++;
    if (count == 20)
      return;
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  if (writeToFlash == false) {
    Serial.println("Looks like WiFi credentials are entered! or already stored in flash");
    fileSystem.openFromFile("/ssid.txt", wifi_ssid);
    fileSystem.openFromFile("/password.txt", wifi_passwd);
    Serial.println("Local WiFi details");
    Serial.println(ssid_sta);
    Serial.println(password_sta);

    Serial.println("FS WiFi details");
    Serial.println(wifi_ssid);
    Serial.println(wifi_passwd);

    setup_wifi(ssid_sta, password_sta);
    
    {
      IPAddress ipAddr = WiFi.localIP();
      if (ipAddr[0] != 0) {
        Serial.println("IP address is detected!");
        
        writeToFlash = true;
        if (fileSystem.saveToFile("/writeToFlash.txt", writeToFlash))
          Serial.println("Successfully wrote writeToFlash data to file");
          
        wifi_ssid = ssid_sta;
        wifi_passwd = password_sta;

        Serial.println("FS WiFi details before reboot");
        Serial.println(wifi_ssid);
        Serial.println(wifi_passwd);

        
        if (fileSystem.saveToFile("/ssid.txt", wifi_ssid))
          Serial.println("Successfully wrote wifi_ssid data to file");
          
        if (fileSystem.saveToFile("/password.txt", wifi_passwd))
          Serial.println("Successfully wrote wifi_passwd data to file");
          
        ESP.restart();
      }
    }
    
  } else if (run_loop == 0) {
    fileSystem.openFromFile("/ssid.txt", wifi_ssid);
    fileSystem.openFromFile("/password.txt", wifi_passwd);
    Serial.println("FS WiFi details after update");
    Serial.println(wifi_ssid);
    Serial.println(wifi_passwd);
    setup_wifi(wifi_ssid, wifi_passwd);
    run_loop = 1;
  }
}

Log:

Looks like WiFi credentials are entered! or already stored in flash
Local WiFi details
test12 wifi detected!!!
test_passwdtest_passwd12@123
FS WiFi details



Connecting to test12 wifi detected!!!
WiFi connected
IP address: 
192.168.0.184
IP address is detected!
Successfully wrote writeToFlash data to file
FS WiFi details before reboot
test12 wifi detected!!!
test_passwdtest_passwd12@123
Successfully wrote wifi_ssid data to file
Successfully wrote wifi_passwd data to file

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v0004a6a0
~ld

FS WiFi details after update
test_passwdtest_passwd12@123
test_passwdtest_passwd12@123

Connecting to test_passwdtest_passwd12@123
@titusece
Copy link
Author

Modified the code by replacing char* with int, then both variables are able to read back properly in next reboot.
So looks like to me its a bug. Can you pls look into it ?

Modified code:

#include <ESP8266WiFi.h>
#include <Effortless_SPIFFS.h>

char *ssid_sta;
char *password_sta;

char* wifi_ssid;
//char* wifi_passwd;
int wifi_passwd;//use as int and see


// Create fileSystem
eSPIFFS fileSystem;

bool writeToFlash = false;                                   // default value will be overriden by opening file

int run_loop = 0;

void setup() {
  delay(3000);
  Serial.begin(115200);
  Serial.println();

  // Check Flash Size - Always try to incorrperate a check when not debugging to know if you have set the SPIFFS correctly
  if (!fileSystem.checkFlashConfig()) {
    Serial.println("Flash size was not correct! Please check your SPIFFS config and try again");
    delay(1000);
    ESP.restart();
  }  

  // This will change value through reboots
  fileSystem.openFromFile("/writeToFlash.txt", writeToFlash);  // This will open the value of writeToFlash

  password_sta = (char *) malloc(100);
  ssid_sta = (char *) malloc(100);

//Actual: Get it from user webpage
  String input_message;
  input_message = "test12 wifi detected!!!";
  strcpy(ssid_sta, input_message.c_str());
  input_message = "test_passwdtest_passwd12@123";
  strcpy(password_sta, input_message.c_str());
  
}



void setup_wifi(char *ssid_sta, char *password_sta) {

  static int count = 0;
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid_sta);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid_sta, password_sta);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.print(".");
    count++;
    if (count == 20)
      return;
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  if (writeToFlash == false) {
    Serial.println("Looks like WiFi credentials are entered! or already stored in flash");
    fileSystem.openFromFile("/ssid.txt", wifi_ssid);
    fileSystem.openFromFile("/password.txt", wifi_passwd);
    Serial.println("Local WiFi details");
    Serial.println(ssid_sta);
    Serial.println(password_sta);

    Serial.println("FS WiFi details");
    Serial.println(wifi_ssid);
    Serial.println(wifi_passwd);

    setup_wifi(ssid_sta, password_sta);
    
    {
      IPAddress ipAddr = WiFi.localIP();
      if (ipAddr[0] != 0) {
        Serial.println("IP address is detected!");
        
        writeToFlash = true;
        if (fileSystem.saveToFile("/writeToFlash.txt", writeToFlash))
          Serial.println("Successfully wrote writeToFlash data to file");
          
        wifi_ssid = ssid_sta;
        //wifi_passwd = password_sta;
        wifi_passwd = 0x1234;
        

        Serial.println("FS WiFi details before reboot");
        Serial.println(wifi_ssid);
        Serial.println(wifi_passwd);

        
        if (fileSystem.saveToFile("/ssid.txt", wifi_ssid))
          Serial.println("Successfully wrote wifi_ssid data to file");
          
        if (fileSystem.saveToFile("/password.txt", wifi_passwd))
          Serial.println("Successfully wrote wifi_passwd data to file");
          
        ESP.restart();
      }
    }
    
  } else if (run_loop == 0) {
    fileSystem.openFromFile("/ssid.txt", wifi_ssid);
    fileSystem.openFromFile("/password.txt", wifi_passwd);
    Serial.println("FS WiFi details after update");
    Serial.println(wifi_ssid);
    Serial.println(wifi_passwd);
    //setup_wifi(wifi_ssid, wifi_passwd);
    run_loop = 1;
  }
}

Log:

Looks like WiFi credentials are entered! or already stored in flash
Local WiFi details
test12 wifi detected!!!
test_passwdtest_passwd12@123
FS WiFi details

0

Connecting to test12 wifi detected!!!
WiFi connected
IP address: 
192.168.0.184
IP address is detected!
Successfully wrote writeToFlash data to file
FS WiFi details before reboot
test12 wifi detected!!!
4660
Successfully wrote wifi_ssid data to file
Successfully wrote wifi_passwd data to file

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v0004a790
~ld

FS WiFi details after update
test12 wifi detected!!!
4660

@titusece titusece changed the title Unable to store & read back multiple String values Unable to store & read back multiple char* values Jul 12, 2022
@thebigpotatoe
Copy link
Owner

Looks like a potential bug.

The code uses a static buffer which I assume without any testing is clobbering pointers, hence the return value you see.

An easy fix would be to just use string, albeit that it is not as memory efficient, it will probably solve the issue.

Of course if you have the time and knowledge to fix the issue as well, pull requests are most welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants