Sending an Array to one Photon from Another

Hello! I’m looking to send an array from one Photon to another. Does anyone have any guidance on how to do so? Can I use a particle.variable()?

Particle.variable() and Particle.publish() are meant for strings only.
So in order to send a binary struct you need to serialisation/deserialisation implemented (e.g. base 64/base85 encoding).
If you are transfering locally and/or don’t care about security you can go with TCP/UDP transfer too.

Is there a way to turn an array into a string where each value is separated by commas? I have code to parse that information out based on the commas, but I can’t figure out how to get it into that format.

Sure there is. What type are the values in your array? Are they Strings, char arrays, numbers? Basically, you just need to create a char array big enough to hold the whole array, loop through the array and use strcat (if using chars) to concatenate the values and the commas. If the values in the array are not already chars, you would want to convert them first using sprintf. For instance, if you have an array of ints, you could do something like this,

char arrayString[50];
int values[] = {12, 54, 67, 255, 62, 127, 222, 169, 47, 23};

void create ArrayString() {
    for (int i=0; i<(sizeof(values)/sizeof(values[0])); i++) {
        char s[5];
        sprintf(s, "%d,", values[i]);
        strcat(arrayString, s);
    }
}

This will leave you with a final comma that you could remove, or deal with on the other end when you convert it back to an array.

1 Like

I got that to work but now I’m having trouble parsing it. I keep getting -1 for valueStr. Any idea why?

void player1btns(const char *event,const char *data) {
  //this is the handler for receiving the subscribed event
  String newPatternEvent = String(event);
  String newPatternData = String(data);

  Serial.print("PLAYER ONE DETECTED!");
  Serial.println();
  Serial.print("newPatternData:");
  Serial.print(newPatternData);
  Serial.println();

  int currentOffset = 0;
  int bufferIndex = 0;
  int commaPos = 0;

  while( (newPatternData.indexOf( ",", currentOffset )) != -1 ) {
    commaPos = newPatternData.indexOf(",", currentOffset);
    String valueStr;
    valueStr = newPatternData.indexOf(currentOffset, commaPos);
    Serial.print("valueStr: ");
    Serial.print(valueStr);
    Serial.println();

    int value = valueStr.toInt();

    bufferOP[bufferIndex] = value;
    bufferIndex++;
    currentOffset = commaPos + 1;

    Serial.print("val: "); //this is adding all of the numbers together
    Serial.print(value);
    Serial.println();

    Serial.print("bufferOP:");
    Serial.print(value);
    Serial.println();
  }

}

I'm not sure what you're trying to do with that line. The first argument to the indexOf function needs to be a char or String, not an int. Personally, I would parse the char *data using strtok, rather than converting data to a String, and using String functions.

2 Likes

I wouldn’t use String in the handler, but copy the const char* into a big enough char[] and use strtok() for parsing.

But with String I’d guess you rather want substring() here

  valueStr = newPatternData.indexOf(currentOffset, commaPos);
1 Like

why not show your data prototype?