0

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

  • I don't see what your problem is really, canyou explainfurther what you would expect from your code? – jfpoilpret Jan 5 '15 at 14:49
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 Jan 6 '15 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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