Convert integer to string for publish

Hi there,

I am sorry if this is silly question, but I am a newbie to Particle and I have spent three hours trying to work out why it isn’t working.

I have a photoresistor, which I would like to publish the analog value of to my thingspeak account.
I can see the values as a integer, but as soon as I try to convert to a string it publishes as 0.

    #include "lib1.h"
    // -----------------------------------------
    // Function and Variable with Photoresistors
    // -----------------------------------------
    // In this example, we're going to register a Particle.variable() with the cloud so that we can read brightness levels from the photoresistor.
    // We'll also register a Particle.function so that we can turn the LED on and off remotely.

    // We're going to start by declaring which pins everything is plugged into.

    int led = D0; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.

    int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).

    int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
    // The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
    // That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.

    int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
    char luminanceStr[30];

    // Next we go into the setup function.

    void setup() {

        // First, declare all of our pins. This lets our device know which ones will be used for outputting voltage, and which ones will read incoming voltage.
        pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
        pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
        pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

        // Next, write one pin of the photoresistor to be the maximum possible, so that we can use this for power.
        digitalWrite(power,HIGH);

        // We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
        Particle.variable("luminanceVal", analogvalue);
        sprintf(luminanceStr,"Luminance %f", analogvalue);
        //Serial.println(luminanceStr);
        
        Particle.variable("Stringlum", analogvalue);
        // This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.
        Particle.publish("lumux", luminanceStr, PRIVATE);
        
        // We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
        Particle.function("led",ledToggle);
        // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.

    }


    // Next is the loop function...

    void loop() {

        // check to see what the value of the photoresistor is and store it in the int variable analogvalue
        analogvalue = analogRead(photoresistor);
        delay(5000);

    }


    // Finally, we will write out our ledToggle function, which is referenced by the Particle.function() called "led"

    int ledToggle(String command) {

        if (command=="on") {
            digitalWrite(led,HIGH);
            return 1;
        }
        else if (command=="off") {
            digitalWrite(led,LOW);
            return 0;
        }
        else {
            return -1;
        }

    }

What am I doing wrong? Apologies if this is just a C++ question, it’s just in all the examples it seems this should just work.

It looks like you have two Particle Cloud variables with different names that point to the same integer value (analogvalue). You want the second one to point to luminanceStr instead. But then you also have to update luminanceStr via another sprintf after you read a new value in loop. That should sort out the variables so you can read them from the Cloud.

Right now you are also only publishing once during setup() so if you want to publish more than once, you need to call Particle.publish() from within loop(). The same comment applies to using sprintf to update the char array to publish.

1 Like

Additionally if you have a %f in your sprintf() format string, you need to provide a floating point variable to it too.
But since your analogvalue is actually an int you should use %d as format place holder.

BTW, for analogRead() you should not set pinMode(photoresistor, INPUT)
https://docs.particle.io/reference/firmware/photon/#analogread-adc-

1 Like

That is absolutely brilliant, thank you both. It is working perfectly now. I hadn’t grasped that the sprintf needs to go in the loop.

The working code:

#include "lib1.h"
// -----------------------------------------
// Function and Variable with Photoresistors
// -----------------------------------------
// In this example, we're going to register a Particle.variable() with the cloud so that we can read brightness levels from the photoresistor.
// We'll also register a Particle.function so that we can turn the LED on and off remotely.

// We're going to start by declaring which pins everything is plugged into.

int led = D0; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.

int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).

int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
// The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
// That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.

int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
char luminanceStr[30];

// Next we go into the setup function.

void setup() {

    // First, declare all of our pins. This lets our device know which ones will be used for outputting voltage, and which ones will read incoming voltage.
    pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)

    pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

    // Next, write one pin of the photoresistor to be the maximum possible, so that we can use this for power.
    digitalWrite(power,HIGH);

    // We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
    Particle.variable("luminanceVal", analogvalue);
    sprintf(luminanceStr,"%d", analogvalue);
    //Serial.println(luminanceStr);
    
    Particle.variable("Stringlum", luminanceStr);
    // This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.
    Particle.publish("lumux", luminanceStr, PRIVATE);
    
    // We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
    Particle.function("led",ledToggle);
    // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.

}


// Next is the loop function...

void loop() {

    // check to see what the value of the photoresistor is and store it in the int variable analogvalue
    analogvalue = analogRead(photoresistor);
    
    
    // Update luminanceStr
    sprintf(luminanceStr,"%d", analogvalue);
    
    //Update variables
    Particle.variable("luminanceVal", analogvalue);
    Particle.variable("Stringlum", luminanceStr);
    
    Particle.publish("lumux", luminanceStr, PRIVATE);
    
    
    delay(5000);

}


// Finally, we will write out our ledToggle function, which is referenced by the Particle.function() called "led"

int ledToggle(String command) {

    if (command=="on") {
        digitalWrite(led,HIGH);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led,LOW);
        return 0;
    }
    else {
        return -1;
    }

}

Many thanks,
Jamie

2 Likes

But Particle.variable() doesn’t belong in loop() :wink:
That’s only there to register your base variables (analogvalue & luminanceStr) with the cloud. The update of the variables is done in loop() by merely assgining the new values to the base variables.

2 Likes