Arduino Ethernet - TCP IP Questions

Hello All,

I just started playing around with the Ethernet Shield W5100 for Arduino.
I found an example for receiving TCP IP information. I am using VVVV TCP Client for sending data.

Question 1:
I am not sure why or if this is needed, but it seems the example is setting a client = server.available().

Does this do anything or could server be used in the place of client everywhere in the example?
I'm just not sure if changing to an EthernetClient variable from an EthernetServer type has some benefit.

Question 2:
When I try to send data to the Arduino, it seems to take a long time to connect, and I am never sure when the connection has been made until the Serial.print starts showing.

Is there a way to know how long the connection should take or to verify that there is a connection before sending data that will be lost?

Question 3:
When I send a message ( example <255,000,100> ) I have the char array set to size 11 so it can get the RGB information. When this information prints, I randomly receive an extra character at the end of the message. I don't think this is coming from VVVV and I am not sure how the message can have 12 characters when the array is set to only hold 11.

#include <Ethernet.h>

byte mac[] = {0x54, 0x52, 0x49, 0x41, 0x44, 0x00};
byte ip[] = {192, 168, 2, 99};
byte gateway[] = {192, 168, 0, 1};
byte subnet[] = {255, 255, 0, 0};
EthernetServer server(4444);
EthernetClient client;

int sizeMessage = 11;

void setup() {
  Serial.begin(9600);
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin(); 
  client = server.available();
}

void loop() {
  char inByte;
  char message[sizeMessage];
  int bytes;
  
  while(!client){
    client = server.available();
  }
  
  if(client.available()){
      inByte = client.read();
      if(inByte == '<'){
        bytes = client.readBytesUntil('>', message, sizeMessage);
        Serial.println(message);
        //Serial.print("Number of Bytes = ");
        //Serial.println(bytes);
      }
    }
    
  }

Thank you all in advance for the help!
Let me know if you need anymore information.

Kind regards,
Jake

You may want to read Ethernet sheild(W5100) not working with Uno - Networking, Protocols, and Devices - Arduino Forum

@ieee488 I am reading through the forum link you sent, but it seems this is all about an issue with poorly made boards. Mine is able to connect, send and receive data.

I think I just need help understanding how to properly use the Stream class.

Do you think my issues may be caused by the physical condition of my shield?

My board does have the 511 resistor that was mentioned in the linked thread. I can add the 2x 100ohm resistors as suggested by k1ggis.

@ieee488 - I added the 2 resistors as described, but I am still receiving an extra character at the end of my message when printing from my code in the original post.

I send the message <255,255,255> and it prints 255,255,255ÿ

I have it set to start recording the message when '<' is received and to stop when '>' is received.
The printed message is 12 characters long, so I am not sure how this is working.

TIA for all the help!

I re-uploaded the code and ran again.

This time when I send <255,255,255>, it prints 255,255,2555. It is an extra '5' at the end and I still can't figure out why.

Seems the only way I can get the message to print correctly is by adding in a delay(); after printing. I don't understand how or why this would work... but it does. I hate these kinds of solutions, so I welcome any other suggestions. Although, it seems like I am just posting to myself here.

I haven't used my Ethernet shield in a while.
I was going to try it again, but I had no problems a few months ago.
I mostly tried the WebServer example.
And I did not make the modifications described in the link.
I was told I was just lucky. LOL.

        bytes = client.readBytesUntil('>', message, sizeMessage);
        Serial.println(message);

message is NOT a string. Do not pass char arrays that are NOT strings to functions that expect strings.

@ieee488 - Thank you for pointing me to the other forum link. It didn't change my problem, but it may have helped with other functionality I am not using.

I am not very familiar with how to use the Streaming Class properly.
It is working, but I just don't understand why flush() doesn't actually flush and data seems to be carried over from previous messages.

If I sent a message like ABCXYZ, then sent a message 1234, it would print 1234YZ...
I can even reset the arduino and send 1234 and it will still print the remaining characters from previous messages.

PaulS:
message is NOT a string. Do not pass char arrays that are NOT strings to functions that expect strings.

Are you saying that my Serial.println() is the problem because message is not a string, or that I should be using a string variable in the readBytesUntil()?

TIA I am just beginning with bytes and char types.

mjnewsum:
Are you saying that my Serial.println() is the problem because message is not a string, or that I should be using a string variable in the readBytesUntil()?

TIA I am just beginning with bytes and char types.

You could try replacing that bit of code with

if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

That is used in the example sketch included in the IDE.

See what you get.

@ieee48 - that would get the Serialprint to be correct, but I am trying to collect the whole message so I can use it for controlling some other functions in the arduino.

This example is basically to set and RGB LED from the ethernet TCP IP message. That's why I was trying to use the readByteUntil() so it would collect the message.

I was trying to set a loop to add each character to a message, but it seemed that was what readByteUntil() was made for.

Thank you for the suggestion.

mjnewsum:
@ieee48 - that would get the Serialprint to be correct, but I am trying to collect the whole message so I can use it for controlling some other functions in the arduino.

This example is basically to set and RGB LED from the ethernet TCP IP message. That's why I was trying to use the readByteUntil() so it would collect the message.

I was trying to set a loop to add each character to a message, but it seemed that was what readByteUntil() was made for.

Thank you for the suggestion.

Baby steps.

We're trying to figure out if you receiving correctly by printing to the Serial Monitor.

Your reading is not quite working, so this is to help you troubleshoot and see what you are receiving.

If I hand you a stick, when you expect a stick, you can handle that, right?

If I hand you a rattlesnake when you expect a stick, that's a bit harder to handle, right?

If you NULL-terminate your array, making it a string, you can then hand it to functions that expect a string, and get the expected behavior. You are handing a rattlesnake to a function that expects a stick, and acting surprised when the function does something unexpected.

@PaulS & @ieee488 That makes a lot of sense.
I just wanted to make sure you knew that I am not just trying to print the characters.

I made the modification suggested.
This is how I started out initially.
It prints the correct message.

void setup() {
  Serial.begin(9600);
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin(); 
  client = server.available();
}

void loop() {
  char inByte;
  char message[sizeMessage];
  int bytes;
  
  while(!client){
    client = server.available();
  }
  
  if(client.available()){
    char c = client.read();
    Serial.print(c);
    }
  }

The code that I posted previously was my attempt to collect the characters into a message that could be used later for things like analog output.

Thank you for the help.

I am still trying to figure out how to make these incoming message into an int, but I can't find anything on the forum.

It seems like it is in here somewhere, but I only find reference to how this is done with Serial.read whereas I am using Ethernet Stream class.

Here is a post that seemed to relate in some ways:
https://forum.arduino.cc/index.php?topic=126138.0

And this tutorial makes sense (for Serial.read()):
https://www.gammon.com.au/serial

I just don't see any example using the Ethernet Shield.

It would help if you post your entire sketch.
I have no idea what you have at this moment.

You need to show us what is in "these incoming message". For all we know, the incoming message looks like:

"It is going to rain on Tuesday".

Now, what should the integer value of that message be?

That last bit of code is NOT collecting the data in an array. It is NOT NULL-terminating the array.

Until you do BOTH of those steps, AND the array actually contains a representation of an int, then passing anything to atoi() is a waste of effort.

Here is the code as is when printing the received message one character at a time.
The message prints correctly.
Message sent is in this format:

<###,###,###>

example message:

<255,255,255>

I am adding the '<' and '>' to identify the start and end of messages.
This is not required and can be modified if needed.

#include <Ethernet.h>

byte mac[] = {0x54, 0x52, 0x49, 0x41, 0x44, 0x00};
byte ip[] = {192, 168, 2, 99};
byte gateway[] = {192, 168, 0, 1};
byte subnet[] = {255, 255, 0, 0};
EthernetServer server(4444);
EthernetClient client;

int sizeMessage = 11;


void setup() {
  Serial.begin(9600);
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin(); 
  client = server.available();
}

void loop() {
  char inByte;
  char message[sizeMessage];
  int bytes;
  
  while(!client){
    client = server.available();
  }
  
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}