I have this test code for concept.
I have a writeWiFi function that simulates user input data and writes it to a file.
I have a readWiFi function that reads the data, then updates 2 global char arrays.
My issue is that before I exit the function, the global variable is correct. When the function exits the main loop then prints the same global variable, and it has a different value than expected.
MAIN FILE
#include "FS.h"
#include "SD.h"
#include "SPI.h"
// ***SD CARD INFO
//
// sd card pins
#define SD_MISO 12
#define SD_MOSI 11
#define SD_SCLK 13
#define SD_CS 10
int writeonce =0;
char *SSID = "HAYNES_635";
char *password = "635Haynes";
char *stored_ssid = "";
char *stored_ssidpassword = "";
char buf[100];
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("starting");
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
Serial.println("SPI and AD started");
if(!SD.begin(SD_CS)){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
}
void loop() {
// put your main code here, to run repeatedly:
//readFile(SD, "/WiFi.txt");
Serial.println("");
delay(2000);
writeWiFi(SD, SSID, password);
Serial.println("");
delay(2000);
Serial.println("");
readWiFi(SD);
Serial.print("SSID is - ");
Serial.println(stored_ssid);
Serial.print("password is - ");
Serial.println(stored_ssidpassword);
delay(10000);
}
FILEHANDLE.INO
void writeWiFi(fs::FS &fs, char * SSID, char * password){
fs.remove("/WiFi.txt"); //delete file first
File file = fs.open("/WiFi.txt", FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
file.print(SSID);
file.print("\n");
file.print(password);
file.print("\n");
Serial.println("FILE WROTE");
file.close();
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.println("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
file.close();
}
void readWiFi(fs::FS &fs){
File file;
file = fs.open("/WiFi.txt");
Serial.println("FILE OPEN");
if(file){
int i = 0;
while(file.available()){
Serial.println("READING FILE");
//clear the buffer
memset(buf, 0, sizeof(buf));
file.readBytesUntil('\n', buf, 100);
Serial.print("Iternation = ");
Serial.println(i);
Serial.print("Value of buf = ");
Serial.print("\"");
Serial.print(buf);
Serial.print("\"\n");
if(i == 0){
Serial.print("loop 0 value = ");
Serial.println(buf);
stored_ssid = buf;
Serial.print("Value of ssid after update - ");
Serial.println(stored_ssid);
}
if(i == 1){
Serial.print("loop 1 value = ");
Serial.println(buf);
stored_ssidpassword = buf;
Serial.print("Checking SSID value again - ");
Serial.println(stored_ssid);
Serial.print("Value of ssidpassword after update - ");
Serial.println(stored_ssidpassword);
}
i = i+1;
}
Serial.println("closing file");
file.close();
}
else{
Serial.println("SD Card: Error opening WiFi.txt");
}
}
OUTPUT OF PRINT STATEMENTS
FILE OPEN
READING FILE
Iternation = 0
Value of buf = "HAYNES_635"
loop 0 value = HAYNES_635
Value of ssid after update - HAYNES_635
READING FILE
Iternation = 1
Value of buf = "635Haynes"
loop 1 value = 635Haynes
Checking SSID value again - 635Haynes
Value of ssidpassword after update - 635Haynes
closing file
SSID is - 635Haynes
password is - 635Haynes
The value of stored_ssid = stored_ssidpassword in the main loop. but in the function it is correct, I don't even know how it is getting different values into the global seeing the globals are correct before function exits.