I am trying to communicate with a client(PC) from my arduino (set up as a server with a sim900 module connected via UART). I have successfully got it to register its IP address with a dynamic DNS provider (no-ip.com) and set up a TCP server using the AT+CIPSERVER command for the sim900. All this is done in setup(). After that in loop() it waits for a client to connect. The moment a client connects the problem arises. The code is:-
void loop()
{
while(Serial.available() <= 0);
int x=0;
char server_recv_string[30];
while(Serial.available()>0)
{
server_recv_string[x] = Serial.read();
delay(10);
}
Serial.println(server_recv_string);
sendATcommand2("AT+CIPSEND",">","ERROR",1000);
sendATcommand2("I hear you\x1A","SEND OK","ERROR",5000);
while(Serial.available()>0) // I dropped this in to clear out the input buffer.
{ // Doesn't help
Serial.read();
delay(10);
}
Serial.flush();
}
The sendATcommand2 is a function as given below. I don't think that has any fault in it because it seems to work fine for all the other commands in setup. However this is the code:-
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the answer
if(Serial.available() != 0 && x < 100){
response[x] = Serial.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
// Waits for the asnwer with time out
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
The when I monitor it on the serial terminal I get infinite sends to my client. Its like this:
AT+CIPSEND
I hear you
AT+CIPSEND
I hear you
AT+CIPSEND
I hear you
AT+CIPSEND
I hear you
// And on and on and on.
Any idea why this is happening? If you guys need any more information, please do tell me.
Thanks
Taz