splitting an array definition across several lines

Hey.
I have this definition:

static const char ACTIVE_STATUS[] PROGMEM = "{\"activestep\":{{ActiveStep}},\"amsi\":{{ActiveMashStepIndex}},\"masn\":\"{{ActiveMashStepName}}\",\"amssn\":\"{{ActiveMashStepSufixName}}\",\"absi\":\"{{ActiveBoilStepIndex}}\",\"absn\":\"{{ActiveBoilStepName}}\",\"btt\":{{BoilTargetTemperature}},\"ttp\":{{TargetTemperature}},\"st\":{{StartTime}},\"et\":{{EndTime}},\"tn\":{{TimeNow}},\"bs\":{{BrewStarted}},\"tp\":{{Temperature}},\"stp\":{{SpargeTemperature}},\"btp\":{{BoilTemperature}},\"atp\":{{AuxOneTemperature}},\"attp\":{{AuxTwoTemperature}},\"atttp\":{{AuxThreeTemperature}},\"axs\": \"{{AuxOneSensor}}\",\"axss\": \"{{AuxTwoSensor}}\",\"axsss\": \"{{AuxThreeSensor}}\",\"pp\":{{PWMPercentage}},\"spp\":{{SpargePWMPercentage}},\"bppt\":{{BoilPWMPercentage}},\"es\":{{EnableSparge}},\"eb\":{{EnableBoilKettle}},\"stt\":{{SpargeTargetTemperature}},\"sl\":{{StepLocked}},\"pumpOn\":{{PumpOn}},\"mashPumpOn\":{{MashPumpOn}},\"boilPumpOn\":{{BoilPumpOn}},\"pir\":{{PumpIsResting}},\"tns\":{{TimeNotSet}},\"tu\": \"{{TempUnit}}\",\"v\": \"{{Version}}\",\"c\": \"{{Count}}\",\"bpp\":{{BoilPowerPercentage}} }";

That is written out in one line. I'd like to split it into several lines for easier overview - but I can't figure out how to do that - help?

Regards
Anders

can't even guess at what you're trying to do. can you explain?

With the "s it looks like you're trying to define one long string that has quotes.

That is a very ugly string. How are you using it in the program ?

Continuation lines can be preceded by . Make sure that no blanks or other characters follow the \ at the end of the line.

void setup() {
  // put your setup code here, to run once:
  char* test = "this is one line \
this is the second \
this is the third";
  Serial.begin(9600);
  Serial.println(test);  //prints this is one line this is the second this is the third
}
void loop() {
  // put your main code here, to run repeatedly:

}

You can put a closing quote then continue the text on the next line with an opening quote, the text will get concatenated into a single long string. Another method would be to but a backslash as the very last character on the line, then continue on the next line at the very beginning of the line (do not add any spaces or tabs at the beginning of the line or it will get added to the text).

static const char ACTIVE_STATUS[] PROGMEM =
  "{\"activestep\":{{ActiveStep}},"
  "\"amsi\":{{ActiveMashStepIndex}},"
  "\"masn\":\"{{ActiveMashStepName}}\","
  "\"amssn\":\"{{ActiveMashStepSufixName}}\","
  "\"absi\":\"{{ActiveBoilStepIndex}}\","
  "\"absn\":\"{{ActiveBoilStepName}}\","
  "\"btt\":{{BoilTargetTemperature}},"
  "\"ttp\":{{TargetTemperature}},"
  "\"st\":{{StartTime}},"
  "\"et\":{{EndTime}},"
  "\"tn\":{{TimeNow}},"
  "\"bs\":{{BrewStarted}},"
  "\"tp\":{{Temperature}},"
  "\"stp\":{{SpargeTemperature}},"
  "\"btp\":{{BoilTemperature}},"
  "\"atp\":{{AuxOneTemperature}},"
  "\"attp\":{{AuxTwoTemperature}},"
  "\"atttp\":{{AuxThreeTemperature}},"
  "\"axs\": \"{{AuxOneSensor}}\","
  "\"axss\": \"{{AuxTwoSensor}}\","
  "\"axsss\": \"{{AuxThreeSensor}}\","
  "\"pp\":{{PWMPercentage}},"
  "\"spp\":{{SpargePWMPercentage}},"
  "\"bppt\":{{BoilPWMPercentage}},"
  "\"es\":{{EnableSparge}},"
  "\"eb\":{{EnableBoilKettle}},"
  "\"stt\":{{SpargeTargetTemperature}},"
  "\"sl\":{{StepLocked}},"
  "\"pumpOn\":{{PumpOn}},"
  "\"mashPumpOn\":{{MashPumpOn}},"
  "\"boilPumpOn\":{{BoilPumpOn}},"
  "\"pir\":{{PumpIsResting}},"
  "\"tns\":{{TimeNotSet}},"
  "\"tu\": \"{{TempUnit}}\","
  "\"v\": \"{{Version}}\","
  "\"c\": \"{{Count}}\","
  "\"bpp\":{{BoilPowerPercentage}} }";


void setup() {
  Serial.begin(115200);
  Serial.println((__FlashStringHelper*)ACTIVE_STATUS);

}

void loop() {
  // put your main code here, to run repeatedly:

}

Looks like a cumbersome way to store the text, but would need to see the code to know how you are using it.

does he want a single string or an array of strings?

static const char * ACTIVE_STATUS[] PROGMEM =
  "{\"activestep\":{{ActiveStep}}", 
  "\"amsi\":{{ActiveMashStepIndex}}",
  "\"masn\":\"{{ActiveMashStepName}}\"",
  "\"amssn\":\"{{ActiveMashStepSufixName}}\"",

UKHeliBob:
That is a very ugly string. How are you using it in the program ?

Yup - it's not very easy to read, hence why I'm trying to make a better order of it.

I'm trying to program an esp32 to control my beer brewing machine.
I found an existing project:

(this is in the ActiveStatus.cpp file)

And I'm trying to modify it to work with my machine.

So I'd like to split it up a bit like this:

static const char ACTIVE_STATUS PROGMEM = "{
"activestep":{{ActiveStep}},
"amsi":{{ActiveMashStepIndex}},
"masn":..... etc

but I don't know how to do this line breaks within that array initialization.

gcjr:
does he want a single string or an array of strings?

static const char * ACTIVE_STATUS[] PROGMEM =

"{"activestep":{{ActiveStep}}",
 ""amsi":{{ActiveMashStepIndex}}",
 ""masn":"{{ActiveMashStepName}}"",
 ""amssn":"{{ActiveMashStepSufixName}}"",

That will not work properly with PROGMEM, the array of char * will be stored in PROGMEM, but the strings will be stored in ram.

still not clear if you want one string or multiple strings

the following is valid in C

char *s = "now"  " is the"
" time"

" for all";

its the equivalent of

char *s = "now is the time for all";

if you want on long string

Here's a link to the specific file I'm messing with:

Odd that the project is named BrewUNO when it runs on an esp8266. I know nothing about json so can't comment on the code itself.

A raw string literal is going to be much more readable:

static const char ACTIVE_STATUS[] PROGMEM = R"====({
"activestep":{{ActiveStep}},
"amsi":{{ActiveMashStepIndex}},
"masn":"{{ActiveMashStepName}}",
"amssn":"{{ActiveMashStepSufixName}}",
"absi":"{{ActiveBoilStepIndex}}",
"absn":"{{ActiveBoilStepName}}",
"btt":{{BoilTargetTemperature}},
"ttp":{{TargetTemperature}},
"st":{{StartTime}},
"et":{{EndTime}},
"tn":{{TimeNow}},
"bs":{{BrewStarted}},
"tp":{{Temperature}},
"stp":{{SpargeTemperature}},
"btp":{{BoilTemperature}},
"atp":{{AuxOneTemperature}},
"attp":{{AuxTwoTemperature}},
"atttp":{{AuxThreeTemperature}},
"axs": "{{AuxOneSensor}}",
"axss": "{{AuxTwoSensor}}",
"axsss": "{{AuxThreeSensor}}",
"pp":{{PWMPercentage}},
"spp":{{SpargePWMPercentage}},
"bppt":{{BoilPWMPercentage}},
"es":{{EnableSparge}},
"eb":{{EnableBoilKettle}},
"stt":{{SpargeTargetTemperature}},
"sl":{{StepLocked}},
"pumpOn":{{PumpOn}},
"mashPumpOn":{{MashPumpOn}},
"boilPumpOn":{{BoilPumpOn}},
"pir":{{PumpIsResting}},
"tns":{{TimeNotSet}},
"tu": "{{TempUnit}}",
"v": "{{Version}}",
"c": "{{Count}}",
"bpp":{{BoilPowerPercentage}} 
})====";

Pieter

Thanks. Unfortunately that didn't work :confused:

arjepsen:
Thanks. Unfortunately that didn't work :confused:

What didn't work?

The string you started with is invalid JSON, you cannot have two nested pairs of braces like that. Should there be quotes around them?

arjepsen:
Unfortunately that didn't work

Piss-poor problem description.

PieterP:
A raw string literal is going to be much more readable:

static const char ACTIVE_STATUS[] PROGMEM = R"====({

"activestep":{{ActiveStep}},
"amsi":{{ActiveMashStepIndex}},
"masn":"{{ActiveMashStepName}}",
"amssn":"{{ActiveMashStepSufixName}}",
"absi":"{{ActiveBoilStepIndex}}",
"absn":"{{ActiveBoilStepName}}",
"btt":{{BoilTargetTemperature}},
"ttp":{{TargetTemperature}},
"st":{{StartTime}},
"et":{{EndTime}},
"tn":{{TimeNow}},
"bs":{{BrewStarted}},
"tp":{{Temperature}},
"stp":{{SpargeTemperature}},
"btp":{{BoilTemperature}},
"atp":{{AuxOneTemperature}},
"attp":{{AuxTwoTemperature}},
"atttp":{{AuxThreeTemperature}},
"axs": "{{AuxOneSensor}}",
"axss": "{{AuxTwoSensor}}",
"axsss": "{{AuxThreeSensor}}",
"pp":{{PWMPercentage}},
"spp":{{SpargePWMPercentage}},
"bppt":{{BoilPWMPercentage}},
"es":{{EnableSparge}},
"eb":{{EnableBoilKettle}},
"stt":{{SpargeTargetTemperature}},
"sl":{{StepLocked}},
"pumpOn":{{PumpOn}},
"mashPumpOn":{{MashPumpOn}},
"boilPumpOn":{{BoilPumpOn}},
"pir":{{PumpIsResting}},
"tns":{{TimeNotSet}},
"tu": "{{TempUnit}}",
"v": "{{Version}}",
"c": "{{Count}}",
"bpp":{{BoilPowerPercentage}}
})====";



Pieter

Doesn't that insert a newline at the end of each line?

gfvalvo:
Piss-poor problem description.

:smiley:

You are right, although your comment could probably also be described as being likewise in terms of whether it added anything constructive to the thread :wink:

What I meant with that, is when I used the raw string litteral suggestion, the website wasn't responding correctly.

I managed to solve it by closing the quote on each line, followed by backslash, and then new quote on the next line. Thanks to David for pointing me in the right direction!

PieterP:
The string you started with is invalid JSON, you cannot have two nested pairs of braces like that. Should there be quotes around them?

The original code appears to be using the double braces as part of a substitution routine:

static const char ACTIVE_STATUS[] PROGMEM = "{\"as\":{{ActiveStep}},\"amsi\":{{ActiveMashStepIndex}},\"masn\":\"{{ActiveMashStepName}}\",\"amssn\":\"{{ActiveMashStepSufixName}}\",\"absi\":\"{{ActiveBoilStepIndex}}\",\"absn\":\"{{ActiveBoilStepName}}\",\"btt\":{{BoilTargetTemperature}},\"ttp\":{{TargetTemperature}},\"st\":{{StartTime}},\"et\":{{EndTime}},\"tn\":{{TimeNow}},\"bs\":{{BrewStarted}},\"tp\":{{Temperature}},\"stp\":{{SpargeTemperature}},\"btp\":{{BoilTemperature}},\"atp\":{{AuxOneTemperature}},\"attp\":{{AuxTwoTemperature}},\"atttp\":{{AuxThreeTemperature}},\"axs\": \"{{AuxOneSensor}}\",\"axss\": \"{{AuxTwoSensor}}\",\"axsss\": \"{{AuxThreeSensor}}\",\"pp\":{{PWMPercentage}},\"spp\":{{SpargePWMPercentage}},\"bppt\":{{BoilPWMPercentage}},\"es\":{{EnableSparge}},\"eb\":{{EnableBoilKettle}},\"stt\":{{SpargeTargetTemperature}},\"sl\":{{StepLocked}},\"po\":{{PumpOn}},\"pir\":{{PumpIsResting}},\"tns\":{{TimeNotSet}},\"tu\": \"{{TempUnit}}\",\"v\": \"{{Version}}\",\"c\": \"{{Count}}\",\"bpp\":{{BoilPowerPercentage}} }";

String ActiveStatus::GetJson()
{
    String active_status = FPSTR(ACTIVE_STATUS);
    active_status.replace("{{ActiveStep}}", String(ActiveStep));
    active_status.replace("{{ActiveMashStepIndex}}", String(ActiveMashStepIndex));
    active_status.replace("{{ActiveMashStepName}}", String(ActiveMashStepName));
    active_status.replace("{{ActiveMashStepSufixName}}", String(ActiveMashStepSufixName));
    active_status.replace("{{ActiveBoilStepIndex}}", String(ActiveBoilStepIndex));
    active_status.replace("{{ActiveBoilStepName}}", String(ActiveBoilStepName));
    active_status.replace("{{BoilTargetTemperature}}", String(BoilTargetTemperature));
    active_status.replace("{{TargetTemperature}}", String(TargetTemperature));
    active_status.replace("{{StartTime}}", String(StartTime));
    active_status.replace("{{EndTime}}", String(EndTime));
    active_status.replace("{{TimeNow}}", String(TimeNow));

david_2018:
Doesn't that insert a newline at the end of each line?

Yes, but a decent JSON parser shouldn't care.