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());
}
}
Great!
Thanks for sharing.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara
I’m having a problem compiling. Trying to read a txt file.Which I think is similar to the original poster.
Excerpt from your example code at the top with minor mods
————————–
char* abc[40];
String ssidString=file1.readStringUntil(‘\n’);
abc=ssidString.c_str();
compiler says: incompatible types in assignment of ‘const char*’ to ‘char* [40]’
It seems the compiler thinks that ssidString.c_str() is a call to a function for type conversion? I do not understand this line.
———————- full error msg follows
Arduino: 1.8.13 (Windows 10), Board: “ESP32 Dev Module, Enabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 115200, None”
C:\Users\iabar\Documents\ESP32\4_WayServer_010\4_WayServer_010.ino: In function ‘void setup()’:
4_WayServer_010:253:4: error: incompatible types in assignment of ‘const char*’ to ‘char* [40]’
abc=ssidString.c_str();
^
4_WayServer_010:256:13: error: incompatible types in assignment of ‘const char*’ to ‘char* [20]’
antstr1 = txtline.c_str();
^
4_WayServer_010:257:12: error: ‘class HardwareSerial’ has no member named ‘writeln’
Serial.writeln(antstr1);
^
4_WayServer_010:278:23: error: ‘class WiFiClass’ has no member named ‘softIP’
Serial.println(WiFi.softIP());
^
C:\Users\iabar\Documents\ESP32\4_WayServer_010\4_WayServer_010.ino: At global scope:
4_WayServer_010:352:4: error: expected unqualified-id before ‘if’
if(!SPIFFS.begin(true)){ // call SPIFFS first time
^
4_WayServer_010:358:5: error: expected unqualified-id before ‘if’
if(!file1){
^
4_WayServer_010:363:5: error: expected unqualified-id before ‘while’
while(file1.available()){ // read the file and send to serial
^
4_WayServer_010:370:5: error: ‘ssid’ does not name a type
ssid=str1.c_str();
^
4_WayServer_010:371:5: error: ‘Serial’ does not name a type
Serial.writeln(str1);
^
4_WayServer_010:373:5: error: ‘file1’ does not name a type
file1.close(); // close file
^
Multiple libraries were found for “WiFi.h”
Used: C:\Users\iabar\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi
Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1
incompatible types in assignment of ‘const char*’ to ‘char* [40]’
You need to understand C++ pointers and arrays. First off you are declaring an array to hold pointers to char’s – char* abc[40]; Basically forty items to hold memory locations. .c_str(); returns a pointer to char (ie. a location in memory) and you are trying to force that pointer into the array (abc is basically a pointer to an array). What you could do (But is probably not what you want) is abc[0] = ssidString.c_str();
Yes – I read the .txt file into a buffer, then did a bytewise copy from there to different string vars. Probably the old fashioned way, but it works!
I’ve purchased the full ESP tutorials and am now looking at Ch 2.5 with 4 buttons.
Thank you for asking.