hello, following your book, i’m playing with files. i want to simulate the reading of a file that will contain the ssid and password of a wifi network, reads them and then it will connect to the wifi.
i store the data inside a file in two different lines, my problem is, when i pass the credentials to the WIFI.begin funtion it expects two char*, but reading with .readStringUntil(‘\n’); it gives me a String and the build fails. i’m trying the conversion with .c_str, or .toCharArray, but but with no success. have some suggestions? here is my code:
#include "FS.h"
#include "SPIFFS.h"
#include
File myFile;
const char* myFilePath = "/new_file.txt";
char* ssid[30];
char* password[30];
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
// Initialize SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("Error while mounting SPIFFS");
return;
}
// Open file and write data to it
myFile = SPIFFS.open(myFilePath, FILE_WRITE);
if (myFile.print("NETWORKSSID\n")){
Serial.println("Message successfully written");
}
else{
Serial.print("Writting message failled!!");
}
myFile.close();
// Append data to file
myFile = SPIFFS.open(myFilePath, FILE_APPEND);
if(myFile.print("NETWORKPASSWORD\n")){
Serial.println("Message successfully appended");
}
else{
Serial.print("Appending failled!");
}
myFile.close();
// Read file content
myFile = SPIFFS.open(myFilePath, FILE_READ);
Serial.print("File content: ");
while(myFile.available()) {
Serial.write(myFile.read());
}
Serial.println("");
// Check file size
Serial.print("File size: ");
Serial.println(myFile.size());
myFile.close();
int line=0;
int index=0;
myFile = SPIFFS.open(myFilePath, FILE_READ);
while (myFile.available()) {
if(line==0){
String ssidString=myFile.readStringUntil('\n');
ssid=ssidString.c_str();
}
if(line==1){
String passwordString=myFile.readStringUntil('\n');
password=passwordString.c_str();
}
line++;
}
Serial.println(ssid);
Serial.println(password);
wifiSetup();
}
void loop() {
// put your main code here, to run repeatedly:
}
void wifiSetup(){
WiFi.mode(WIFI_STA);
WiFi.disconnect();
WiFi.begin(ssid, password);
int maxRepeat=0;
while (WiFi.status() != WL_CONNECTED && maxRepeat<20) {
delay(500);
Serial.println("Connecting to WiFi..");
maxRepeat++;
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
}
}
thank you in advance!
Hi.
Take a look at this example and see if you can make it work for you: https://www.techiedelight.com/convert-string-char-array-cpp/#:~:text=strcpy%20function,string%20into%20a%20char%20array.
I’m not sure if this works, but you need to create a char array with the string size plus one. For example, for the ssid:
char ssidChar[ssid.length +1];
Then, call strcpy() to copy the string into the char array:
strcpy(ssidChar, ssid.c_str());
Then, use the ssidChar variable in the WiFi.begin() method.
Do the same thing for the password variable.
I haven’t experimented this, so I’m not sure it will work.
Then, tell me how it went.
Regards,
Sara
hello, thank you, i have modified like this:
while (myFile.available()) {
if(line==0){
String ssidString=myFile.readStringUntil('\n');
char ssidChar[ssidString.length()+1];
strcpy(ssidChar, ssidString.c_str());
ssid=ssidChar;
}
if(line==1){
String passwordString=myFile.readStringUntil('\n');
char passwordChar[passwordString.length()+1];
strcpy(passwordChar, passwordString.c_str());
password=passwordChar;
}
line++;
if(line>1) break;
}
but it reads always the password, it’s like it doesn’t see the first “\n”
i’m trying also with this code, it uses file.seek() function:
myFile.seek(0);
int charnumber=0;
String passwordString;
String ssidString;
while(true){
cr = myFile.read();
Serial.write(cr);
ssidString+=cr;
if(cr == '\n'){
Serial.println(ssid);
char ssidChar[ssidString.length()+1];
strcpy(ssidChar, ssidString.c_str());
ssid=ssidChar;
break;
}
charnumber++;
}
myFile.seek(charnumber+1);
while(true){
cr = myFile.read();
Serial.write(cr);
passwordString+=cr;
if(cr == '\n'){
char passwordChar[passwordString.length()+1];
strcpy(passwordChar, passwordString.c_str());
password=passwordChar;
break;
}
}
Serial.println("ssid %s",ssid);
Serial.println(password);
myFile.close();
wifiSetup();
i know it’s very bad c, thank you
Hi.
Your first approach doesn’t work, because you’re reading the same thing in both if statements,
In what parts of the code do you use the “line” variable?
Are you sure you have the ssid and password on different lines?
What if you put the ssid and password on the same line but separated by a special character like &, for example? Then, you can split the string before and after that character.
Regards,
Sara
hello, i think after the first \n character it goes to the second line eand take the other string, if it’s not, so i’m wrong.
the one line with the custom separator could be a good idea, but it means that the password can’t contain that character, i will try, maybe with more characters, like &; or something like that
hello, i’m sorry but….qhere can i find the File class functions? because basically my problem is the String variable that the method readstringuntl returns, what i need is a char * that i can manipulate in pure c, String is an arduino class that i don’t know how manipulate to pass to the wifi.begin function.
i’m trying with strstr() function to find the special character between ssid and password index and so to separate the two variables that i need
hello, i’m sorry but….qhere can i find the File class functions? because basically my problem is the String variable that the method readstringuntl returns, what i need is a char * that i can manipulate in pure c, String is an arduino class that i don’t know how manipulate to pass to the wifi.begin function.
i’m trying with strstr() function to find the special character between ssid and password index and so to separate the two variables that i need
anyway, i found this:
https://esp32.com/viewtopic.php?t=12153
and i solved, but there is something in the strings that doesn’t make the esp cnnect to the wifi, maybe there is some hidden character like a \n or that i can’t see in the serial monitor
edit:
sorry for the many posts, here is my final code if could help someone else:
#include "FS.h"
#include "SPIFFS.h"
#include
File myFile;
const char* myFilePath = "/new_file.txt";
String s,p;
void setup() {
Serial.begin(115200);
if(!SPIFFS.begin(true)){
Serial.println("Error while mounting SPIFFS");
return;
}
myFile = SPIFFS.open(myFilePath, FILE_WRITE);
if (myFile.println("SSIDNAME")){
Serial.println("Message successfully written");
}
else{
Serial.print("Writting message failled!!");
}
myFile.close();
myFile = SPIFFS.open(myFilePath, FILE_APPEND);
if(myFile.print("WIFIPASSWORD")){
Serial.println("Message successfully appended");
}
else{
Serial.print("Appending failled!");
}
myFile.close();
myFile = SPIFFS.open(myFilePath, FILE_READ);
int i=0;
while(myFile.available())
{
if(i == 0)
s = myFile.readStringUntil('\n');
else
p = myFile.readStringUntil('\n');
i++;
}
myFile.close();
s.trim();
p.trim();
wifiSetup();
}
void loop() {
// put your main code here, to run repeatedly:
}
void wifiSetup(){
WiFi.mode(WIFI_STA);
WiFi.disconnect();
const char* ssid=s.c_str();
const char* pass=p.c_str();
WiFi.begin(ssid,pass);
int maxRepeat=0;
while (WiFi.status() != WL_CONNECTED && maxRepeat<20) {
delay(1000);
Serial.println("Connecting to WiFi..");
maxRepeat++;
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
}
}
Hello,thanks for your answer,with the code after the edit I can connect, the problem was a space after the SSID,maybe the println left this space,or is the append function.
anyway,I checked with string length and seen that it counts a character more than the real letters. So I found the trim function that cleans the strings,an now it works very good.
Maybe the code is not the best of the efficiency for a real c coder,but it works!
Thanks again
Hi peppeg85, Could you please share the working code for me try out. Thanks in advance.
sure, it is two posts above this….but anyway, here it is, it should be formatted with indentation
#include "FS.h"
#include "SPIFFS.h"
#include <WiFi.h>
File myFile;
const char* myFilePath = "/new_file.txt";
String s,p;
void setup() { // Serial port for debugging purposes
Serial.begin(115200);
// Initialize SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("Error while mounting SPIFFS");
return;
}
// Open file and write data to it
myFile = SPIFFS.open(myFilePath, FILE_WRITE);
if (myFile.println("ssidxxxxx")){
Serial.println("Message successfully written");
}
else{
Serial.print("Writting message failled!!");
}
myFile.close();
// Append data to file
myFile = SPIFFS.open(myFilePath, FILE_APPEND);
if(myFile.print("passwordxxxx")){
Serial.println("Message successfully appended");
}
else{
Serial.print("Appending failled!");
}
myFile.close();
// Read file content
myFile = SPIFFS.open(myFilePath, FILE_READ);
Serial.print("File content: ");
while(myFile.available()) {
Serial.write(myFile.read());
}
Serial.println("");
// Check file size
Serial.print("File size: ");
Serial.println(myFile.size());
myFile.close(); myFile = SPIFFS.open(myFilePath, FILE_READ);
int i=0;
while(myFile.available())
{
if(i == 0)
s = myFile.readStringUntil('\n');
else
p = myFile.readStringUntil('\n');
i++;
}
myFile.close();
s.trim();
p.trim();
Serial.println(s.length());
Serial.println(s.charAt(14));
Serial.println(p.length());
wifiSetup();
} void loop() {
// put your main code here, to run repeatedly: } void wifiSetup(){
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.println(s.c_str());
Serial.println(p.c_str());
const char* ssid=s.c_str();
const char* pass=p.c_str();
WiFi.begin(ssid,pass);
int maxRepeat=0;
while (WiFi.status() != WL_CONNECTED && maxRepeat<20) {
delay(1000);
Serial.println("Connecting to WiFi..");
maxRepeat++;
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
}
}