Go Down

Topic: Arduino Ethernet - TCP IP Questions (Read 4707 times) previous topic - next topic

mjnewsum

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.



Code: [Select]


#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


mjnewsum

@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?

mjnewsum

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.

mjnewsum

@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!

mjnewsum

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.

mjnewsum

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.

ieee488

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.

PaulS

Code: [Select]
        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.
The art of getting good answers lies in asking good questions.

mjnewsum

@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.


mjnewsum

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.

ieee488

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
Code: [Select]

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.


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.

ieee488

@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.


PaulS

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.
The art of getting good answers lies in asking good questions.

Go Up