How to send numeric variables between Particles? (part2: SPRINTF)

Hi again, fantastic community! :wave:

Objective: How to get a series of variables (integers and floating point) from one Photon into a second one?

In this tutorial, I try to provide another tool with this objective, using SPRINTF formatting on both sides.
In PART1 we used JSON. (You will find both are complementary and each have their advantages and disadvantages)

For this technique you will NOT need any library!

You need again 2 particles to test this:

Particle 1 runs the “SPRINTF Generator & Publish test” sketch.
It sends a message called “Status” with the variables in a simple text string.

Particle 2 runs the sketch called “SPRINTF Parsing & Subscribe test”.
It ‘catches’ the SPRINTF text string in the “Status” message and splits it up into the variables again.
These variables can then be output separately to your serial monitor.
You can also calculate with the numeric ones (INT, FLOAT…)

Please understand that I deserve no credit for the techniques used in these sketches, only for the final polishing to a (hopefully) useful set of tools.
I will be very interested to read your reactions and probably improvements…


Below you find both sketches: Just flash them to your 2 Particles and connect the receiver via USB to your serial monitor:

  1. This is the sketch for the sending Particle:

    /* SPRINTF Generator & Publish test

    Written by @FiDel, it’s the result of several days of work together with some great members:
    @Ric , @Moors7 , @ScruffR , @peekay123 (and probably I forget a few, sorry!)

    Flash this to the sending Photon. It will publish a compact string with variables, but without labels like this:
    “ECO system,8,16,24,32,40,48,888,12.7”

    */

    // We initiate all variables we want to package in a JSON string:
    char Controller = “ECO system”;
    int T1=0;
    int T2=0;
    int T3=0;
    int T4=0;
    int T5=0;
    int T6=0;
    int T7=0;
    float Energy=5;

    char str[255];

    void setup()
    {
    Serial.begin(9600);
    }

    void loop()
    {
    // Modify the variables each step:
    T1=T1+1;
    T2=T2+2;
    T3=T3+3;
    T4=T4+4;
    T5=T5+5;
    T6=T6+6;
    T7=T7+111;
    Energy=Energy*1.1234;

    // Concatenate all variables to a string with SPRINTF:
    sprintf(str, “%s,%d,%d,%d,%d,%d,%d,%d,%.1f”, Controller, T1, T2, T3, T4, T5, T6, T7, Energy);
    Particle.publish(“Status”, str,60,PRIVATE);
    delay(5000);
    }

  2. This is the sketch for the receiving Particle:

    /* SPRINTF Parsing & Subscribe test

    Written by @FiDel, it’s the result of several days of work together with some great members:
    @Ric , @Moors7 , @ScruffR , @peekay123 (and probably I forget a few, sorry!)

    For the receiving Photon, receiving the published string with variables like this: “ECO system,8,16,24,32,40,48,888,12.7”
    It will then serial.print all variables separately (and eventually concatenated as a string)
    */

    void setup()
    {
    Serial.begin(9600);
    Particle.subscribe(“Status”, stringParser, MY_DEVICES);
    }

    void loop()
    {
    }

    void stringParser(const char *event, const char data)
    {
    char
    Controller = strtok(strdup(data), “,”);
    int T1 = atoi(strtok(NULL, “,”));
    int T2 = atoi(strtok(NULL, “,”));
    int T3 = atoi(strtok(NULL, “,”));
    int T4 = atoi(strtok(NULL, “,”));
    int T5 = atoi(strtok(NULL, “,”));
    int T6 = atoi(strtok(NULL, “,”));
    int T7 = atoi(strtok(NULL, “,”));
    float Energy = atof(strtok(NULL, “,”));

    // Serial.print all variables separately:
    Serial.println(Controller);
    Serial.println(T1);
    Serial.println(T2);
    Serial.println(T3);
    Serial.println(T4);
    Serial.println(T5);
    Serial.println(T6);
    Serial.println(T7);
    Serial.println(Energy);
    Serial.println();

    // Optionally Serial.print all variables again as one string: (Check “printlnf” command! What? How?)
    Serial.printlnf(“Controller: %s T1: %d T2: %d T3: %d T4: %d T5: %d T6: %d T7: %d Energy: %.1f”,Controller,T1,T2,T3,T4,T5,T6,T7,Energy);
    }

If you are interested in the other method, check out the Part1 tutorial.
Curious for your contributions!

3 Likes