Hello guys!
I've got a problem with my verify funtion that runs more then once and I can't figure out the problem. The only thing I know is that it's running correctly ( returning false or true once ) when the funtion is inside the setup() and not in the loop().
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x50, 0x35 };
IPAddress arduino(192,168,0,12);
IPAddress server(192,168,0,15);
EthernetClient client;
String username[] = {"Admin","Skut","Arbek","Kubda"};
String password[] = {"12344","stra","124ssgra","!#¤%"};
String readString;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac,arduino);
Serial.println("Connecting...");
if (client.connect(server, 8888)){
Serial.println("Connected to Server");
} else {
Serial.println("Connecting failed");
}
}
boolean verify(String firstText, String secondText){
for(int i = 0; i <= 3 ; i++){
if((username[i] == firstText ) && (password[i] == secondText)){
return true;
}
}
return false;
}
void clientRead() {
while (client.available()) {
if (client.available() >0) {
char c = client.read();
readString += c;
}
}
}
void loop(){
if (client.available() > 0){
readString = "";
clientRead();
Serial.println(readString);
int n = readString.length();
int commaIndex = readString.indexOf(';');
int lastIndex = readString.lastIndexOf(n);
String firstText = readString.substring(0, commaIndex);
String secondText = readString.substring(commaIndex + 1 , lastIndex);
Serial.println(firstText);
Serial.println(secondText);
Serial.println(verify(firstText, secondText));
}
if (!client.connected()){
Serial.println("Server disconnected!");
client.stop();
client.flush();
while(true);
}
}
This is what I get when sending ( Admin;12344 ) from the server to the Arduino:
Connecting...
Connected to Server
Admin;12344
Admin
12344
1
0