Getting an Array of data from spark :)

I don’t know the answer to your question, but another way to accomplish your goal without using JSON, is to use sprintf to construct a string with a suitable separator between your data points ( a comma in my example). In the receiving Photon, you would use strtok to unpack the string. The example below has the publish and subscribe in the same Photon for testing purposes, but of course, you would have the subscribe and its handler in the receiving Photon.

void setup() {
    Serial.begin(9600);
    delay(3000);
    char str[255];
    sprintf(str, "%.1f,%.1f,%.1f,%d,%d", 23.8, 47.1, 12.5, 61432, 99999999);
    Serial.println(str);
    Particle.subscribe("rdTestPub", stringParser);
    Particle.publish("rdTestPub", str);
    
}


void stringParser(const char *event, const char *data) {

    float first = atof(strtok((char*)data, ","));
        float second = atof(strtok(NULL, ","));
        float third = atof(strtok(NULL, ","));
        int fourth = atoi(strtok(NULL, ","));
        int fifth = atoi(strtok(NULL, ","));
        Serial.printlnf("first: %.1f  second: %.1f  third: %.1f  fourth: %d  fifth: %d", first,second,third,fourth,fifth);
}
1 Like