[SOLVED] Photon: Function Variable Request from a Nodejs Server

I am testing this example code “Function Variable” so that I can request the value of the Spark variable “analogvalue” from a web server. Since I don’t have a photoresistor, I set the value of “analogvalue” equal to 255.

The problem I am having is the response from the get request. The status code is 200 showing the API call successfully delivered to the device and executed. However, when I read the response data in the terminal I get a lot of information and do not get the value of “analogvalue” reported in the response.

Below is the code I have used to to test this functionality. Can anyone provide guidence to why I can read the value of the variable “analogvalue”?

    // -----------------------------------------
    // Function and Variable with Photoresistors
    // -----------------------------------------
    // In this example, we're going to register a Spark.variable() with the cloud so that we can read brightness levels from the photoresistor.
    // We'll also register a Spark.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.

    // 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 the power of the photoresistor to be the maximum possible, so that we can use this for power.

    // We are going to declare a Spark.variable() here so that we can access the value of the photoresistor from the cloud.
    Spark.variable("analogvalue", &analogvalue, INT);
    // 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.

    // We are also going to declare a Spark.function so that we can turn the LED on and off from the cloud.
    // 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 = 255; //analogRead(photoresistor);
      Spark.publish("analogValue", String(analogvalue));

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

    int ledToggle(String command) {

    if (command=="on") {
        return 1;
    else if (command=="off") {
        return 0;
    else {
        return -1;


One thing is, you need to adhere the one publish per second limit.
If you exceed this, after four publishes you’ll be kicked till you slow down your piblishes :wink:

And how do you retrieve the Particle.variable()

Have a test with this

@ScruffR Your advice was right - but I still can’t see the data in the terminal response from my server. I added this code to correct the publishing issue:

void loop() {

// check to see what the value of the photoresistor is and store it in the int variable analogvalue
analogvalue = 255; //analogRead(photoresistor);
Spark.publish("analogValue", String(analogvalue));

Next I signed into the console from the link you provided. It showed the variable analogvalue and provided the correct value of 255 as defined in the firmware. When I clicked on:

the new window opened and displayed the correct variable information.

In my terminal, I still get a status code 200, yet I can not find where or if the data is displayed in the terminal response. I took the response out and pasted into a word doc to search just in case I was missing the data by visual inspection. No luck there either. Can you let me know if there is a specific attribute I should be referencing from the response?

It’s obviously not a problem with the device or the code running on it, so it must be your requesting side.

What do you use as terminal and how do you issue the GET request?
How do you process the response?

@ScruffR Thank you for the guidance. Your thought process was clear and helped me resolve this issue. My server is built on nodejs. The following was the initial code I was using:

https.get("https://api.particle.io/v1/devices/deviceID_here/analogvalue?access_token=accessToken_here", function(res){        

This above code would give a status code of 200 and a large amount of callback information. However, based on your feedback and guidance I started re-examining the possible options for requesting data from the particle cloud - since this as an issue on the server-side script. I decided to give the option from the Particle core a try just to see if I could use it for the photon - why not right!

I found this site here: https://www.npmjs.com/package/sparkcloud

Sure enough after an hour of testing and reworking the code I discovered a readable json result that I could specifically extract the result value of the variable declared on the Photon! Here is what I adapted from the above link. I added the function in the callback to grab the result from within the object that is returned:

var spark   = require('sparkcloud')('access_token')
var photon = spark.device('device_id') 

photon.variable('analogvalue', console.log );

photon.variable('analogvalue', function(err, data) {
    console.log('data on callback. analogvalue == ' + data.result);

Now I can send the variable to be saved in the DB server-side and have a basic working communication link between the server and Photon. Thanks again.

1 Like