Sorting a buffer and publishing data

Hi,
Just looking for any help whatsoever.
I have three different values that I am trying to extract from a buffer. Initially the buffer is compiled from data collected by a sensor that sends at 1200 baud/s. The sensor sends the data, the buffer gets compiled, the buffer then gets sorted and the results are currently being published to the cloud. I was having a very hard time with characters sorting into other buffers (even though I didn’t think it would be that difficult). At this point I just have the same data being stored in 4 separate buffers and then being published to the cloud. When the information is published the initial buffer uploads fine, but the others do not. Sometimes they are sending nothing to the cloud and sometimes the characters are unreadable.

Here are the main parts of the code:

//Constants
const size_t READ_BUF_SIZE = 320;
const unsigned long CHAR_TIMEOUT =10000;

//Global Variables
char readBuf[READ_BUF_SIZE];
size_t readBufOffset =0;
unsigned long lastCharTime = 0;
int i;

struct DataStruct
{
    char VWC[READ_BUF_SIZE];
    char EC[READ_BUF_SIZE];
    char TempCelsius[READ_BUF_SIZE];
};



void setup()
{
    pinMode(B0,OUTPUT);
    Serial.begin(9600);
    Serial1.begin(1200,SERIAL_8N1);
    delay(2000);
  
}

void loop()
{
 static uint32_t ms;
 DataStruct sensorData;
 digitalWrite(B0,HIGH);
 readBufOffset =0;
 i=1;
 
  //Reading Data from the Serial
  while(Serial1.available()){
    
    if (readBufOffset < READ_BUF_SIZE) {
          char c = Serial1.read();
          
          if (c != '\n') {
              //add character to buffer
             
              readBuf[readBufOffset++] = c;

//because I was experiencing some hardships with the sorting and publishing the right information I am just simply trying to see if there are any characters being stored in these buffers

              sensorData.VWC[readBufOffset]=c;
              sensorData.EC[readBufOffset]=c;
              sensorData.TempCelsius[readBufOffset]=c;

              lastCharTime = millis();
               }

          else {
              //end of line character found
              
              Serial.printlnf ("got: %s", readBuf);
              Particle.publish("thebuffer",readBuf);
              Serial.print("i= ");
              Serial.println(i);
              readBuf[readBufOffset]=0;
              i=1;
              
          }
}

    Particle.publish("VWC", String(sensorData.VWC));
    Particle.publish("EC", String(sensorData.EC));
    Particle.publish("TempCelsius", String(sensorData.TempCelsius));
    Particle.publish("thebuffer",String(readBuf));
  
  delay(1000);
  digitalWrite(B0,LOW);
  delay(100000);
}


here is the published data.

Any help or insight would be greatly appreciated. I do not have a computer science background, but I am trying to learn as I go along. Thank you.

If you already have a char[] you should not wrap the same string in a String object again.

    Particle.publish("VWC", String(sensorData.VWC));
    Particle.publish("EC", String(sensorData.EC));
    Particle.publish("TempCelsius", String(sensorData.TempCelsius));
    Particle.publish("thebuffer",String(readBuf));

You also seem to fail to terminate the strings.
You only do that for readBuf but not for the other three strings.

And since you never put a valid character in [0] on these three strings they are always empty.

              readBuf[readBufOffset++] = c;       // readBufOffset is incremented too early
              sensorData.VWC[readBufOffset]=c;    // so these [0] bytes will never hold anything but the initial '\0' 
              sensorData.EC[readBufOffset]=c;
              sensorData.TempCelsius[readBufOffset]=c;

Thank you that has helped. I am currently sorting everything fine; however, there are some tail end characters occurring with the last buffer being compiled and sent to the cloud. Here is what the picture looks like. Not sure why it’s doing this.

Here is the sorting part of the code:

int i=1;
int bufSize = strlen(readBuf);
struct DataStruct
{
char VWC[READ_BUF_SIZE];
char EC[READ_BUF_SIZE];
char TempCelsius[READ_BUF_SIZE];
};

Serial.printlnf(“length of the buffer: %d”, bufSize);
readBufOffset=0;
int c;

for ( int counter=0; counter <= bufSize; counter++)
{
    c = readBuf[counter];
    Serial.printlnf("The readBuf character is: %d   ", c);
    int integer = c -'0';
    Serial.printlnf("After subtracting '0' the result is: %i   ", integer);
    if ( (integer >= (0)) && (integer  <= (9)))
    {
        switch(i) {
        case 1:
            sensorData.VWC[readBufOffset]=c;
            readBufOffset++;
            break;
        
        case 2:
            sensorData.EC[readBufOffset]=c;
            readBufOffset++;
            break;
        
        case 3:
            sensorData.TempCelsius[readBufOffset]=c;
            readBufOffset++;
            break;
        
        case 4:
            char letterchar=c;
            Serial.printf("case 4 reached: %s", letterchar);
            break;
            }
    }
    else{
        Serial.printlnf("In the else statement changing i++");
        i++;
        readBufOffset=0;
    }
}

Since there isn’t really a workspace for the online development environment I print out most things to the monitor and the cloud to check it…Here is what is printing to the serial monitor:

I am trying to just cut it off after the three characters are stored in the temperature array. Based on the monitor it actually isn’t going back into that case so I am not sure what is going on or what the fix is. All/ Any help is appreciated. Thank you.

I think I have to repeat myself on this

You only terminate readBuf here

              readBuf[readBufOffset]=0;

but fail to do the same for the other strings.

Ok sorry about that I didn’t understand what you meant. Just looked it up and we will see how it goes. Thank you for looking at this, much appreciated.