Hardware Reference
In-Depth Information
Programmers like
efficiency, so they
will often combine several commands
into one line of code. Sometimes the
problem is in the combination. For
example, this line attempts to connect
using client.connect() , and then it checks
the result in a conditional statement.
It gave me all kinds of trouble until I
separated the connection attempt
from the check of the connection.
Separate It
if (client.connect()) {
// if you get a connection, report back via serial:
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
8
Here's the less efficient code that
solved the problem. Checking the con-
nection immediately after connecting
was apparently too soon. The delay
stabilized the whole program.
client.connect(); // connect
delay(1); // wait a millisecond
if (client.connected()) {
// if you get a connection, report back via serial:
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Just Watch It
Sometimes it
helps to step
back from your complicated program
and just watch what you're receiving
from the other end. One technique
I returned to again and again while
developing this program was to simply
print out what the Ethernet module
was receiving. Several times it revealed
the problem to me. Sometimes it was
because I had logic problems in my
code, and other times it was because
I'd neglected some character that
the server was sending as part of the
protocol. Familiarity can blind you
to what you need to see. So, remind
yourself of what you are actually
receiving, as opposed to what you think
you are receiving.
// if you're connected, save any incoming bytes
// to the input string:
if (client.connected()) {
if (client.available()) {
char inChar = client.read();
Serial.write(inChar);
}
}
 
Search WWH ::




Custom Search