Hi there,
After hours of trials, I am still not able to know how to test my characters obtained from a client.read() function.
In the code below, I have proposed to test the if loop I am using later:
if (thisString[i]!= '\0')
{DO SOMETHING;}
else
{STOP;}
So here is the code. It works perfectly well:
void setup() {
Serial.begin(115200);
Serial.println(" ");
}
void loop() {
// put your main code here, to run repeatedly:
String thisString = "Hello World!\0";
Serial.print("this is a \0 string: "); Serial.print("\0"); Serial.print(" this is a \0 char: ");Serial.print('\0');
Serial.println("Did you check ?");
Serial.print(thisString);Serial.print("length:");Serial.println(thisString.length());
delay(1000);
for(int i=0;thisString.length()+1;i++){
if (thisString[i]!= '\0'){
Serial.print("Character:");Serial.print(i);Serial.print("-->");Serial.print(thisString[i]);Serial.println(" Continue");
}
else{
Serial.print("Character:");Serial.print(i);Serial.print("-->");Serial.print(thisString[i]);Serial.println("STOP!") ;
break;
}
}
}
if you test the code, the string "\0" or char '\0' are not printed.
The char by char reading works pefectly fine: it extract from the string "Hello World!" H, then e, etc. till it reach the \0.
Now a bit more complexe: I am using a station ESP8266, named STATION [1] and a softAP ESP8266 named SERVER.
My STATION is writting messages with the following format:
SOM
STATION [1] : : random(1000) : random(1000)
STATION [1] : : random(1000) : random(1000)
...
STATION [1] : : random(1000) : random(1000)
EOM\0
note I put the \0 for the "End Of Message". here is the code:
if(client.connected()){
unsigned long startMillis = millis();
timeout_send_message = 0;
client.println("***SOM***");
while (client.connected() && (timeout_send_message < 50) ) {
unsigned long currentMillis = millis();
timeout_send_message = currentMillis - startMillis;
float A = random(1000);float B=random(1000);
client.print("STATION [1] :");client.print(" : ");client.print(A);client.print(" : ");client.println(B);//client.print("\n");
Serial.print("STATION [1] :");Serial.print(" : ");Serial.print(A);Serial.print(" : ");Serial.println(B);
}
client.println("***EOM***\0");
Serial.println(">>> Message Timeout !");
}
in the SERVER void LOOP. I create the connexion with the client, if it is available, I wait a bit so that client.available()>0, then i do a char c = client.read(); and do the comparison test with the '\0'
here is the code:
if (client) {
delay(100); //allow time to populate the client with data
if (client.available() ){
while (client.available()>0 || stop_condition !=0){
char c = client.read();
if (c!='\0'){//if (c!='E'){
stop_condition = 1;
Serial.print(c);
// TRACE();
// Serial.println("CONTINUE");
}
else{
stop_condition = 0;
Serial.println("STOP !!!");
break;
delay(5000);
}
}
}
}
The code is running till it reads the "EOM" but then keep printing the NULL character: [][][][][][]. The "else" is not executed, so the stop_condition do not change to "1".
As you may see, I also tried to compare with the character 'E'. The code works with 'E', but not with the '\0'.
I suspect the code is comparing '' with '\0'. am I right ?
I am looking for a clean way to force the right operating . I would like later to remove the EOM