I'm connecting a client (java program) to an arduino (server). What I want to know is, how high the CPU usage is, and what to do if there are no clients that are trying to connect to arduino? Should I make a delay?

My code looks like this:

EthernetClient client = server.available();


    //check if client is still connected
    while (client.connected()){
        Serial.println("Still there");
       //check if client received any data
           //read message from client
} else {
     ???????????? delay(2000); ??????????????

Thank you for your help.


You don't need to do anything. If the else clause has a delay (e.g. of 2 seconds), then it will take up to that long before the arduino accepts the connection.

The "server.write" in the "else" clause doesn't make any sense - who are you sendin it to? server.write will normally write to all available connections.

If there's nothing else you want to do, you might as well retry straight away.

Note that, if you are doing a serial.println statement, and you have no delay, then, while waiting for a connection, it will print the "Nope" message, as fast as it can.

  • 1
    Thank you for your answer! Oh okay, I understand, I'm quite new at arduino and connections, and I know that my code is nonsense :) so I just put a delay before that "Nope" and it will connect before throwing that "Nope"? And few more things: Is there a way to send a String to arduino (server) over TCP conection in one piece? I'm currently receiving byte by byte. Thank you for your time! – Rok Dolinar Nov 25 '15 at 22:33
  • 1
    Does it make sense to print "Nope" every two seconds that you don't get a connection? For testing purposes, it may be entirely acceptable to do so, in which case a 2 second delay establishing a connection may also be ok. Yes, you should be able to send a string to an arduino in one piece (up to about 1kb); if it's giving it back one character at a time, it's likely an issue in your code - I suggest you post this as a separate question, as it's quite a different question from this one. Include both the sending and receiving code. – AMADANON Inc. Nov 26 '15 at 4:12
  • Normally, I would not have an "else" clause in code like this - if there is no incoming connection, there is nothing to be done (unless debugging). Just let it go around the loop again, as fast as it can. – AMADANON Inc. Nov 26 '15 at 4:13
  • So it has to loop over server.available() as fast as it can? Okay I'll ask another question. – Rok Dolinar Nov 26 '15 at 10:37
  • Oh one more thing.. while client.connected() will return true for ever once it connects right? – Rok Dolinar Nov 26 '15 at 10:52

As the Arduino typically has no demand-based clock scaling, or alternate threads of comparable priority you can schedule, there's really little point in using a delay (which is implemented as a busy-wait) rather than a tight loop - really, the primary appropriate reason would be to reduce the frequency of status output.

However, if you want to design a power-efficient system, you could consider sleeping the CPU between checking if there is anything worthwhile to do, or until an interrupt indicates that there is. This is not something which delay() will take care of for you, so it is a fair amount of extra work and testing to get right, but it can be very worthwhile if running from batteries.

If your application is not particularly intensive you can also configure the clock divider fuse and slow the chip down generally.

  • Thank you for your answer! I'm not worried about power consumption, I'm more worried about overheating. This arduino will handle quite few thing (quite important too) and I don't want any problems. Few more things: Is there a way to send a string over TCP connection in one piece? I'm currently reading byte by byte, but it would be great if there is any other more efficient way. Oh and about TCP connection. Java program will have proper GUI and I want to know how to check if there is no connection? Try/Catch block, and program the catch block so it tells the user? Thank you for your time! – Rok Dolinar Nov 25 '15 at 22:27
  • Overheating and power consumption are very nearly two sides of the same coin, as it is power consumption which creates heat. Effectively an ATmega won't get substantially busier than it already is running a typical busy-wait type of sketch as it typically does. And unless you have other sources of heat, or very atypical thermal insulation of the package, that shouldn't be a problem. Your regulator may produce some heat in normal operation though. As for your other questions, SO rules are one question per post. – Chris Stratton Nov 29 '15 at 18:15

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.