Arduino Ethernet - TCP IP Questions

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 

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);
  }
}

@PaulS I am reading one of you other responses in this forum post: http://forum.arduino.cc/index.php?topic=45629.0

I will try to apply these ideas and see if I can get it to work.

"255,255,255" is 11 characters. With a terminating NULL, you need space in the array for TWELVE characters, not 11.

@PaulS the code below is from this link.

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void loop()
{
  while(Serial.available() > 0) // Don't read unless
                                                 // there you know there is data
  {
      if(index < 19) // One less than the size of the array
      {
          inChar = Serial.read(); // Read a character
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string
      }
  }
  // Now do something with the string (but not using ==)
}

Would this produce the array size + 1 for the NULL terminating character? It seems that you are NULL terminating the string for each character in the message.

I made the following modifications to work with Ethernet. I am getting the correct message printing. Now, I just need to turn each comma separated value into an int.

#include 

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;

const int sizeMessage = 12;
char inData[sizeMessage];
char inChar;
byte index = 0;

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

void loop() {
  char inByte;
  char message[sizeMessage];
  int bytes;

  while(!client){
    client = server.available();
    Serial.println("No connection");
  }
  
  while(client.available() > 0){
    if (index < sizeMessage -1){
      inChar = client.read();
      inData[index] = inChar;
      index++;
      inData[index] = '\0'; //Null terminate the string
    }else{
      Serial.println(inData);
      index = 0;
    }
  }
  Serial.println("no data");
}

Thanks again for the help!

It seems that you are NULL terminating the string for each character in the message.

The string is being kept NULL-terminated. In my mind, that is a far better practice than NULL terminating it after all the data is written to the array. This way, you never end up with a situation where there is NOT a NULL in the array.

I am getting the correct message printing.

Great.

Now, I just need to turn each comma separated value into an int.

strtok() and atoi(). I think I've mentioned them before. Maybe it wasn't to you, though.

@PaulS - thank you for the details. I am still getting used to the idea of Null Terminating the string.

In this post , the string was only Null Terminated after the collection of all chars. I wasn't sure if Null terminating each char would add to the string each time.

I think I will change my messages format so I don't collect information with comma space variables so I can convert the string to int without splitting the sting at each comma.

I still am a bit unsure why it takes so long to connect sometimes (original question 1). I am going to try to have the arduino send out a signal to let VVVV know when the connection has been made so I don't start sending before the arduino is ready.

I figured out that I do need the client = serial.available() for some functionality (original question 2) but I am not entirely sure what it is doing.

Thank you for all the help. I should be able to get things working from here.

I wasn't sure if Null terminating each char would add to the string each time.

I'm not sure what you mean by "add to the string each time".

Suppose you have a bookshelf that can hold 100 books. But, the books fall over if you don't use a book end, when the shelf isn't full.

Now, you need to unwrap and put 10 books on the shelf. You can unwrap a book, and put it on the shelf, and let if fall over while you get the next book ready. When it is ready, you pick up the books that have fallen over, and put the new book at the end, and let it fall over...

Or, you can unwrap the first book, put it on the shelf, and put the book end in place. Then, you unwrap the next book, put it in place of the book end, and put the book end back at the end.

THAT is what is happening when you put a character in place of the NULL and put another NULL at the end.

I figured out that I do need the client = serial.available() for some functionality (original question 2) but I am not entirely sure what it is doing.

That line of code is not what you actually have. You have either

   byte numBytes = Serial.available();

or

   client = server.available();

In the first case, you are asking the Serial instance how much unread data there is. In the second case, you are asking the server if there are any clients that need attention.

@PaulS - Thank you for clarifying.

I'm not sure what you mean by "add to the string each time".

Earlier when you stated that the char array would need to be 12 (my message size +1) I thought the Null Termination added the +1 each time it was called. I see in the book end description that I am simply moving the null terminator to the end of the char array each time it is called.