Splitting a variable

Hi all,

I am creating a Arduino project to turn a spa pool pump on and off based on temperature and the output of my solar PV system.

I have been able to pull the following string in from my PV system, using a MQTT client and storing he output in a char* variable called PVraw.

PVraw contains data in the following format, captured straight from the PV meter [1462009949,1200,"W"] (Where 1200 is the current PV output in kw - this can range from 0 to 5000)

I need to be able to extract the 1200 part and store this in a interger (?) called PVgen.

I have researched various methods to split strings etc, however don't seem to be finding an easy way to do this.

Any help would be greatly appreciated.

Thanks,
Mark

strtok()

This would be one way to go:

  • locate the first ','
  • get the number behind it
char pvBuffer[] = "[1462009949,1200,\"W\"]";
int PVraw;

void setup() {
  Serial.begin(115200);
  char* ptr = strchr(pvBuffer, ',');
  if (ptr) {
    PVraw = atoi(ptr + 1);
  }
  Serial.print(F("PVraw = "));
  Serial.println(PVraw);
}
void loop() {}
PVraw = 1200