Catching server response in char array?

Hey

I'm working on some jSON incoming string from my server. Currently my problem is only that I can't seem to find out how to catch the reply from the server in a char array.

I'm using plain simple Arduino IDE Ethernet Library and the loop function looks like this:

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

I wan't to catch the list of chars (var c just prints everything into the s-monitor), instead of printing it. And I need to do that into a char array, because aJson library use that for parsing.

I'm fairly new to c++ syntax, but it seems creating a new char* array and resizing(create/delete) it every time, is not a very efficient process. Any ideas? :slight_smile:

Anything would be helpfull!

Look for examples of reading and buffering serial input - it’s essentially the same problem.

This is the code where you current read a single byte:

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

You would need to change this so that instead of printing the character to the serial port, it appends it to a buffer:

buffer[length++] = c;

If you’re holding the message as a null-terminated string (the sensible way for buffering text) then append a null terminator to the buffer to mark the end of the string:

buffer[length] = 0;

Make sure you don’t overflow the bounds of the buffer, by checking the number of characters already stored against the size of the buffer before appending. In this size check, make sure to account for the extra byte needed to store the null terminator.

After you’ve received a complete message and dealt with it, clear the buffer by setting length back to zero.

I have written such a subroutine, maybe helpful

#define BUFFERSIZE 80
static char buffer[BUFFERSIZE+1]; // string + terminator

...

String ReadResponse() //reads server responses - return String
{
  bool lastreadeol = false;
  int counter = 0;
  while(counter++ != 50 ){//if server does not respond immediatelly, we will try it ask next 5000 miliseconds
    if (client.available())
    {
      int i = 0;
      char c;
      while (client.available() and (i < BUFFERSIZE))
      {
        c = client.read();
        buffer[i++] = c;
        if ( ((String(c) == "\n") or (String(c) == "\r")) and  (lastreadeol) ) {
          break;
        };
        if ( ((String(c) == "\r") or (String(c) == "\n")) and !(lastreadeol) ) {
          lastreadeol = true;
        };
      }
      buffer[i] = '\0';
      return String(buffer);
    } 
    else
    {
      delay(100);
    };
  };
  return String("");
};

Thanks :slight_smile:

After googling a little on char buffers, i ended up with a solution like this:

if (client.available()) {
    
    char inChar = client.read();
    
    if(inChar == '{') {
        readingJson = true;
      }

    if(readingJson) {
         if(index < 254) 
          {
            inData[index] = inChar; // Store it
            index++; 
          }
          if(inChar == '}') {
            readingJson = false;
            inData[index] = '\0'; 
          }
      }
  }