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];
    server_recv_string[x] =  Serial.read();
  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

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


    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
        // 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();
            // 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:

 I hear you

 I hear you

 I hear you

 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.



  • I don't see what your problem is really, canyou explainfurther what you would expect from your code?
    – jfpoilpret
    Commented Jan 5, 2015 at 14:49

1 Answer 1


You can use a boolean variable to decide if you are going to execute the send command. You can set the variable to true in the setup and to false after the command is executed.

If you introduce the send call inside an if (your boolean variable == true), it will only be executed in one loop iteration.

You can execute it again anytime you like setting the variable to true at any moment.

  • Thanks! Unfortunately that itself didnt help me. For some reason even when I add a flag, the flag sets over and over again when serial.available() is greater than zero. Couldn't figure out why. So i combined setting the flag and after finishing all my tasks forcefully cleared the input buffer with Serial.end() and Serial.begin(9600). I guess this isn't neat but it works for now. I'll investigate further though.
    – taz
    Commented Jan 6, 2015 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.