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

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

1 Answer 1

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.

answered Jan 5, 2015 at 16:26
1
  • 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. Commented Jan 6, 2015 at 13:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.