0

I use the Ethernet shield with an Uno and create a web client. If I put a delay like one second for initialization it works fine. However if I wait for it in a while loop (like waiting for the serial, and as mentioned in Arduino docs) it never gets out of the loop that means the client instance is null.

I found this method on Arduino website in an example code:

while(!client) { ; } // waiting to get ready 

Here's the code; if I uncomment it will stay there. Also the same with providing the MAC argument only.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
char server[] = "www.google.com";    // name address for Google (using DNS)

IPAddress ip(192, 168, 1, 213);

EthernetClient client;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Ethernet.begin(mac, ip);
  delay(1000);
  //while(!client){ ; }
  Serial.println(Ethernet.localIP());
}
8
  • 1
    Show the rest of your particular code. Jan 16, 2018 at 21:28
  • @josecanuc #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; char server[] = "www.google.com"; // name address for Google (using DNS) IPAddress ip(192, 168, 1, 213); EthernetClient client; void setup() { Serial.begin(9600); while (!Serial) { ; } Ethernet.begin(mac, ip); delay(1000); //while(!client){ ; } Serial.println(Ethernet.localIP()); }
    – user174174
    Jan 16, 2018 at 22:31
  • 2
    Code in comments is unreadable. Edit it into the post.
    – gre_gor
    Jan 16, 2018 at 22:32
  • What is the commented line supposed to achieve...?
    – Majenko
    Jan 17, 2018 at 9:56
  • @majenko I want to check that if the hardware is functional and ready and my client object has created successfully so I can start communication. Instead of putting a blind delay(n) and hope for the best.
    – user174174
    Jan 17, 2018 at 15:58

2 Answers 2

0

Your while(!client){ ; } line can never do anything useful.

It is used to wait for a client socket to connect to a remote port. You aren't connecting to a remote port (that client object is doing nothing), so how it can never succeed.

It's like saying "Do nothing, and tell me when you're done".

I don't know what you hope to achieve with that line of code...

3
  • So what does it serve here
    – user174174
    Jan 17, 2018 at 16:05
  • is the linked example wrong?
    – user174174
    Jan 17, 2018 at 16:15
  • Most likely. I can't see what they think they are doing there. That test can never pass unless a connection has been attempted, and no connection has been attempted.
    – Majenko
    Jan 17, 2018 at 16:23
0

What you write is not a function but a variable. And the variable will not change its value (unless you have some interrupt and it is a global variable).

so you need to change 'client' to some function you want to call, probably 'client.xxx()' where xxx is the name of the function you want to call (possibly with parameters).

2
  • Actually it's an instance of an object with an int operator. It returns whether the socket is connected or not. That may be interrupt driven, or it may be done through yield(), which may explain it working with delay(), since delay() calls yeild().
    – Majenko
    Jan 16, 2018 at 22:06
  • I guess you're right but the code is available at link
    – user174174
    Jan 16, 2018 at 22:08

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.