# Using Tasker to read published variables

**URL:** <https://community.particle.io/t/using-tasker-to-read-published-variables/32372>\
**Category:** Getting Started\
**Created:** [May 3, 2017, 8:36pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372 "2017-05-03T20:36:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bear](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@Bear](https://community.particle.io/u/Bear)\
**Post date:** [May 3, 2017, 8:36pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/1 "2017-05-03T20:36:34Z")

</div>

My Photon publishes a variable “led1state” with the following commands:  
Particle.publish(“led1state”,“ON”);  
Particle.publish(“led1state”,“OFF”);

I can toggle “led1state” with Tasker “HTTP Post” tasks, and the console shows that the above publish commands toggle “led1state” properly.

However, I am having trouble reading “led1state” with a Tasker “HTTP Get” task. I have it configured like this

- Server Port: `https://api.particle.io`
- Path: `/v1/devices/<device id>/led1state? access_token=<access token>`
- Attributes: format=raw
- Output File: %HTTPD

I then flash %HTTPD to see the message: `{"ok":false,"error":"Not Found"}`

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 3, 2017, 9:00pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/2 "2017-05-03T21:00:04Z")

</div>

To read a variable state this way, you need to expose that variable via [`Particle.variable()`](https://docs.particle.io/reference/firmware/photon/#particle-variable-)

---

<div class="post-metadata">

**Author:** ![Bear](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@Bear](https://community.particle.io/u/Bear)\
**Post date:** [May 3, 2017, 9:04pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/3 "2017-05-03T21:04:46Z")

</div>

This is the statement that I have in my firmware setup().  
Particle.variable(“led1state”, &led1state, INT);

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 3, 2017, 9:14pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/4 "2017-05-03T21:14:28Z")

</div>

The current (modern) way to write that statement would be

```cpp
  Particle.variable("led1state", led1state);

```

But where do you have that instruction?  
When does it execute when you boot up your device?  
Can you show more of your code?  
Do you really have a blank between `?` and `access_token` in your path?

As it this is a GET request, you can read the variable just with a normal browser.

---

<div class="post-metadata">

**Author:** ![Bear](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@Bear](https://community.particle.io/u/Bear)\
**Post date:** [May 3, 2017, 9:28pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/5 "2017-05-03T21:28:45Z")

</div>

Okay, this command is now in Setup():  
Particle.variable(“led1state”, led1state);

When I toggle: Console shows this: {“data”:“ON”,“ttl”:“60”,“published\_at”:“2017-05-03T21:16:59.793Z”,“coreid”:“410049001051363036373538”,“name”:“led1state”}

but Tasker’s output has not changed.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 3, 2017, 9:40pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/6 "2017-05-03T21:40:02Z")

</div>

`Particle.publish()` and `Particle.variable()` are two completely different things.  
Maybe choose a different name for the event (e.g. `Particle.publish("eventLedState", ...)`) to distinguish the two.

And to repeate myself

> [@ScruffR](#):
>
> Can you show more of your code?

---

<div class="post-metadata">

**Author:** ![Bear](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@Bear](https://community.particle.io/u/Bear)\
**Post date:** [May 3, 2017, 9:46pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/7 "2017-05-03T21:46:39Z")

</div>

Sure … I appreciate your help !!!

As you can see, I’ve just modified one of the samples … “Web-Connected LED”. I reversed the names of the LEDS, so I could use the D7 led rather than wiring a breadboard.

```cpp
// -----------------------------------
// Controlling LEDs over the Internet
// -----------------------------------

// First, let's create our "shorthand" for the pins
// Same as in the Blink an LED example:
// led1 is D0, led2 is D7

int led2 = D0;
int led1 = D7;
String led1state="";

// Last time, we only needed to declare pins in the setup function.
// This time, we are also going to register our Particle function

void setup()
{

   // Here's the pin configuration, same as last time
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);

   // 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);
   Particle.variable("led1state", led1state);

   // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.

   // For good measure, let's also make sure both LEDs are off when we start:
   digitalWrite(led1, LOW);
   digitalWrite(led2, LOW);

}

// Last time, we wanted to continously blink the LED on and off
// Since we're waiting for input through the cloud this time,
// we don't actually need to put anything in the loop

void loop()
{
   // Nothing to do here
}

// We're going to have a super cool function now that gets called when a matching API request is sent
// This is the ledToggle function we registered to the "led" Particle.function earlier.

int ledToggle(String command) {
    /* Particle.functions always take a string as an argument and return an integer.
    Since we can pass a string, it means that we can give the program commands on how the function should be used.
    In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
    Then, the function returns a value to us to let us know what happened.
    In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
    and -1 if we received a totally bogus command that didn't do anything to the LEDs.
    */

    if (command=="on") {
        digitalWrite(led1,HIGH);
        digitalWrite(led2,HIGH);
        //Particle.publish("pushbullet","LED Turned On!", 60, PRIVATE);
        Particle.publish("led1state","ON");
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led1,LOW);
        digitalWrite(led2,LOW);
        //Particle.publish("pushbullet","LED Turned Off!", 60, PRIVATE);
        Particle.publish("led1state","OFF");
        return 0;
    }
    else {
        return -1;
    }
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 3, 2017, 10:11pm UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/8 "2017-05-03T22:11:35Z")

</div>

> [@Bear](#):
>
> Particle.variable("led1state", &led1state, INT);

Since your `led1state` is a `String` you should never had used this, but the new syntax got that sorted already.  
But I don't see your code ever assigning anything to `led1state` - why is that?

As I said `Particle.publish()` doesn't do anything to any variables, so you need to have some `led1state = command;` (or similar) in your code.  
Once you have that you should see the variable change in the console  
[https://console.particle.io/devices](https://console.particle.io/devices)  
when you click on your device.

And navigating in a browser to  
`https://api.particle.io/v1/devices/<deviceID>/led1state?access_token=<accessToken>`  
will give you something like this

```plaintext
"cmd":"VarReturn","name":"led1state","result":"off","coreInfo":{"last_app":"","last_heard":"2017-05-03T22:18:05.436Z","connected":true,"last_handshake_at":"2017-05-03T22:14:56.196Z","deviceID":"<deviceID>","product_id":6}}

```

---

<div class="post-metadata">

**Author:** ![Bear](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@Bear](https://community.particle.io/u/Bear)\
**Post date:** [May 5, 2017, 1:55am UTC](https://community.particle.io/t/using-tasker-to-read-published-variables/32372/9 "2017-05-05T01:55:33Z")

</div>

Okay, I was able to work through things with the information that you provided. The URL was the key that unlocked the door. The code samples that I had reviewed always seemed to show Particle.publish publishing the Particle.variable … so that is how I thought we exposed the variable. Now I know that is not right. Once you declare the Particle.variable, the URL (or Tasker) can display that variable when you execute them. That is the functionality that I was looking for, so I ended up commenting out the Particle.publish lines in my code. I assume we use Particle.publish to actively push data to other places on the net. This data can be the Particle variable or anything else that we want to push. Please let me know if that is incorrect.

Thanks again for the time you spent helping me work through this. Looking back, I realize that I overlooked a couple of the questions that you asked. I apologize for that. I simply didn’t see them.

Along my journey to better understanding, I decided to simplify my code. I attach the final version here. Someone else may find it and your comments useful:  
Note: the Particle.variable name changed to from “led1state” to “led” in the process.

```cpp
// LedTest
// -----------------------------------------------------------------------------
const int LED_PIN = D7;

String led;

void setup() {
   pinMode(LED_PIN, OUTPUT);
   Particle.function("ledToggle",ledToggle);
   Particle.variable("led",led);
   ledToggle("OFF");
}

void loop() {
}

int ledToggle(String command) {
    if (command == "ON") {
        digitalWrite(LED_PIN,HIGH);
        led = "ON";
        //Particle.publish("led","ON");
    } else if (command == "OFF") {
        led = "OFF";
        digitalWrite(LED_PIN,LOW);
        //Particle.publish("led","OFF");
    } else {
        return -1;
    }
    return 0;
}

```
