[solved]Insertion of unwanted carriage returns

Fixed, but I don't know what is causing the problem, hence the topic.

Parsing a text file to create a JSON object. The text file is of the form:

VERSION=Oil Sensor V10.2, 02 Feb 23
LEVEL=369
SUCCESS=255
TOTAL=264
DATE=Sat 04 Feb 2023, 16:37
UNIXT=1675528654
PERCENT=96.59
VOLT=5.02
TEMP=19.50
FULL=0

And the output should be of the form:

String Result = {"version":"text string","level":value, etc...}
                                        ^             ^

except CR (0x0D) are being inserted after all the values as indicated before the comma delimiter. Checked and rechecked the source file to ensure that no carriage returns are inserted, each line in the input file is only terminated with a line feed.

The code parsing the text file:

  const String VersionNo = "VERSION=";
  ...repeat definitions for other lines

  while (ReadingsF.available()) {
        aline = ReadingsF.readStringUntil(LF); //read a line from file
        //check for VERSION
        if (aline.indexOf(VersionNo) == 0) {
            aline.replace(VersionNo,"");
            Result += qt + "version" + qt + delim;
            Result += qt + aline + qt + comma;
            continue; //go to next iteration
           }
       ...repeat for other variables in text file

qt (quote) is a double quote (") and delim is a colon. Somehow aline is acquiring a CR at the end of the string?

Fixed by searching for and replacing all occurrences of CR in the Result string, but how did the CR get there in the first place??

Background - this is an oil level sensor for my central heating oil tank using a JSN-SRN04T (also caused me problems). ESP8266 on the tank takes readings, ftp's readings every few hours (format as above) then goes to sleep. Files are ftped to another ESP8266 acting as a webserver that delivers a webpage showing how much I've got.
Development environment - running linux (mint) with Arduino 1.8.19.

Every "aline" in that text file ends in a < CR >/newline/0D.

No, the file is created with:

  const String LF = "\n"; //line feed
  //write version number
  f.print(VersionNo); 
  f.print(Version); 
  f.print(LF);
  //write current readings to file
  //level in litres
  f.print(OilLevel); 
  f.print(Current.level);
  f.print(LF);
etc...

Explicitly writing line feeds. And this is what it looks like in a hex editor, not a CR (0x0D) to be seen, just line feeds (0x0A) as the terminator:


Hence my why/how?

Maybe because

According to the FTP specification, ASCII files are always transferred using a CR+LF pair as line ending

Might be something in that.

Still at the testing stage. The file displayed in my previous is after being ftped from the esp8266 to my NAS. That file is okay and the transfer is done in binary mode. I have then ftped that file to the second esp8266 using filezilla. I haven't checked what mode filezilla is using to transfer the file.

In the final version the first esp8266 will ftp the file direct to the second esp8266, just haven't got that far yet.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.