1

I am communicating with the Web server's database via Arduino GSM shield + GPRS connectivity

void loop() 
{
 if (client.connect(server, port)) 
 { 
 ....... 
 }
 else 
 { 
 Serial.println("Server not found"); 
 } 
 delay(1000); 
}

The first time when the loop() runs in the serial port, the code works perfectly and stores in my server's db. The second time when the loop() runs after some delay, It is unable to connect to the server and results "server not found" . I don't know what is the problem.

Please let me know if there is a way to connect continuously. Thank you

asked May 4, 2014 at 10:03
2
  • You need to provide a bit more information in your question: what is client? How has it been initialized? In which library (header file name and link to library) is it defined? Also, it might be interesting to describe what you do (not necessarily with all the code, it depends how much that is) in the the first block of the if (...) statement. Commented May 4, 2014 at 11:50
  • Welcome to Stack Exchange! I've noticed that in two of your three questions on the site are "cross posted" with Stack Overflow. Those are both closed. Next time if it's closed, flag for a moderator to "migrate" it to a different site if they feel fit. Thanks! Commented May 5, 2014 at 20:45

3 Answers 3

2

You have to disconnect before being able to connect again otherwise the controller will crash due to memory starvation. Every connect you do takes some memory to register state information. Not sure of the exact commands involved, but it'd look a bit like this:

 if (client.connect(server, port)) 
 { 
 ....... 
 client.disconnect( .... );
 }
 else 
........

Alternatively you can check if you are already connected, and only do a reconnect when necessary, but I don't know which library you are using, let alone if there is a proper check available in it.

answered May 4, 2014 at 12:55
0
1
void loop() 
{
 if (client.connect(server, port)) 
 { 
 ....... 
 client.stop(); 
 }
 else 
 { 
 Serial.println("Server not found"); 
 } 
 delay(1000); 
 }
answered May 4, 2014 at 18:09
3
  • 2
    Can you please add some explanation to this code? Thanks! Commented May 4, 2014 at 23:56
  • Are you sure about the closing } before the client.stop? Commented May 5, 2014 at 16:24
  • Sorry, Its mistake in the code. It will be inside the parenthesis .. Thanks for correction i have updated the code.. Commented May 5, 2014 at 19:58
0

If possible, do a check, if it's still connected with the server. Close the connection when work is done. You might also keep using the open connection, just try whatever works best for you.

But avoid opening a second connection, when you still have an open one. As an example:

void loop() 
{
 if(!client.connected())
 {
 if (client.connect(server, port))
 {
 ...
 client.disconnect();
 }
 else
 Serial.println("Server not found");
 }
 else
 client.disconnect();
 delay(1000);
}
answered May 5, 2014 at 9:27

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.