Hello to everyone
I have some problem
I want to store esp8266 hostname in EEPROM, and load it on startup. But i need to check it for special characters to avoid reboots.
I found some function in internet, but i get instant reboots.
What am i doing wrong?
#include <EEPROM.h>
String sw_name;
char* hostname;
bool isAlphaNumerAndPeriod(char* instr){
Serial.println("starting func");
Serial.println(strlen(instr));
Serial.println("starting func2");
//for(int i = 0; i < strlen(instr); i++){
for(int i = 0; i < sizeof(instr); i++){
Serial.println("loop"+i);
if(isalpha(instr[i])){
Serial.println("loop1"+i);
continue;
}
if(isdigit(instr[i])){
Serial.println("loop2"+i);
continue;
}
if(instr[i] == '.'){
Serial.println("loop3"+i);
continue;
}
return false;
}
return true;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(50);
EEPROM.begin(1536);
delay(50);
read_eeprom_boot();
Serial.println("Begin test");
sw_name.toCharArray(hostname, sw_name.length() + 1);
Serial.println("try func");
if (isAlphaNumerAndPeriod(hostname)) {
Serial.println("true");
} else {
Serial.println("false");
}
delay(50);
Serial.println("Starting system...");
}
void loop() {
// put your main code here, to run repeatedly:
}
void read_eeprom_boot() {
Serial.println("Reading EEPROM parameters...");
sw_name = read_eeprom(126, 145).c_str();
}
String read_eeprom(int start_adress, int end_adress) {
String str;
for (int i = start_adress; i < end_adress; ++i) {
str += char(EEPROM.read(i));
}
return str;
}
I moved your topic to an appropriate forum category.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
you need to have enough bytes reserved into hostname (it needs to be an array) for the copy to happen.
that won't work either
sizeof(instr) is going to be 4 - that is the size of pointer.
it's not the length of your string.
best is to call your function directly with sw_name.c_str() so that you don't have to duplicate anything in memory and in the function iterate over each character until you find a null char which will denote the end of the string.
bool isAlphaNumerAndPeriod(const char* instr) {
...
if (instr == nullptr) return false;
while (* instr) { // as long as the character pointed is not null (ie we have not reached the end of the string)
...
instr++; // go to next char
}
...
}
These functions take the int equivalent of one character as parameter and return an int that can either be another character or a value representing a boolean value: an int value of 0 means false, and an int value different from 0 represents true.
There are two sets of functions:
Character classification functions
They check whether the character passed as parameter belongs to a certain category: