# Would someone mind taking a look at this spark.publish / spark.subscribe code?

**URL:** <https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962>\
**Category:** Troubleshooting\
**Created:** [February 11, 2015, 10:40pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962 "2015-02-11T22:40:48Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![daneboomer](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/daneboomer/32/3653_2.png) [@daneboomer](https://community.particle.io/u/daneboomer)\
**Post date:** [February 11, 2015, 10:40pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/1 "2015-02-11T22:40:48Z")

</div>

Back in November I had some [fairly simple code](https://www.hackster.io/maxeust/quantumly-entangled-leds1) installed on two spark cores, basically LED state mirroring. This was working. Now when I try to test the same code again prior to developing the theme, I find it no longer works. The local LED lights when the local button is pressed, but it’s as if the two cores are no longer subscribed to each other because the remote LED does not react.

I did find that my access token had silently expired, and I thought I had solved the problem when I generated a new one, but unfortunately that hasn’t changed a thing.

Is there an error log I could view somewhere to debug?

Thanks!

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [February 11, 2015, 10:51pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/2 "2015-02-11T22:51:36Z")

</div>

Just flashed some new code, and it works perfectly, so I guess there are no current issues.

---

<div class="post-metadata">

**Author:** ![daneboomer](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/daneboomer/32/3653_2.png) [@daneboomer](https://community.particle.io/u/daneboomer)\
**Post date:** [February 11, 2015, 11:02pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/3 "2015-02-11T23:02:52Z")

</div>

Thanks for testing. I wonder what on Earth is going on…

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [February 11, 2015, 11:27pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/4 "2015-02-11T23:27:05Z")

</div>

You could try this link to see if your events are properly published:  
`https://api.spark.io/v1/devices/events/?access_token=ACCESTOKEN_HERE`  
After that, I’d suggest flashing a testing program, which does nothing more than subscribing, to see if that’s still good. When that’s working, you can try something bigger?

---

<div class="post-metadata">

**Author:** ![daneboomer](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/daneboomer/32/3653_2.png) [@daneboomer](https://community.particle.io/u/daneboomer)\
**Post date:** [February 11, 2015, 11:46pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/5 "2015-02-11T23:46:39Z")

</div>

Thank you for that, it seems that that IS working as it should:-

```
event: spark/status
data: {"data":"online","ttl":"60","published_at":"2015-02-11T23:43:54.980Z","coreid":"53ff6e066667574859291667"}

event: spark/cc3000-patch-version
data: {"data":"1.29","ttl":"60","published_at":"2015-02-11T23:43:55.100Z","coreid":"53ff6e066667574859291667"}

event: Core2Toggle
data: {"data":"State","ttl":"60","published_at":"2015-02-11T23:43:55.105Z","coreid":"53ff6e066667574859291667"}

event: spark/status
data: {"data":"online","ttl":"60","published_at":"2015-02-11T23:44:06.984Z","coreid":"53ff6e066667574828462067"}

event: spark/cc3000-patch-version
data: {"data":"1.29","ttl":"60","published_at":"2015-02-11T23:44:07.116Z","coreid":"53ff6e066667574828462067"}

event: Core1Toggle
data: {"data":"State","ttl":"60","published_at":"2015-02-11T23:44:07.118Z","coreid":"53ff6e066667574828462067"}

event: Core2Toggle
data: {"data":"ON","ttl":"60","published_at":"2015-02-11T23:44:28.531Z","coreid":"53ff6e066667574859291667"}

event: Core1Toggle
data: {"data":"ON","ttl":"60","published_at":"2015-02-11T23:44:36.932Z","coreid":"53ff6e066667574828462067"}

event: Core1Toggle
data: {"data":"OFF","ttl":"60","published_at":"2015-02-11T23:44:39.726Z","coreid":"53ff6e066667574828462067"}

event: Core2Toggle
data: {"data":"OFF","ttl":"60","published_at":"2015-02-11T23:44:46.262Z","coreid":"53ff6e066667574859291667"}

```

So something’s going wrong with my code, clearly, which is bizarre given that it’s a) copied from that guy’s website and b) has worked for me before.

---

<div class="post-metadata">

**Author:** ![daneboomer](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/daneboomer/32/3653_2.png) [@daneboomer](https://community.particle.io/u/daneboomer)\
**Post date:** [February 12, 2015, 10:46am UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/6 "2015-02-12T10:46:45Z")

</div>

[Code 1][1]

[Code 2][2]

If anyone has a spare minute to just cast their eyes over this…I’m really puzzled. And the LEDs are wired correctly because they illuminate due to

```
    digitalWrite(receiveLedPin, HIGH);

```

Thank you  
[1]: [http://pastebin.com/tzMH4vUP](http://pastebin.com/tzMH4vUP)  
[2]: [http://pastebin.com/VVUcLa4B](http://pastebin.com/VVUcLa4B)

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [February 12, 2015, 12:12pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/7 "2015-02-12T12:12:00Z")

</div>

Would you mind trying to publish them without ‘private’, and subscribe without ‘MY\_DEVICES’, and see how that goes? There have been some issues reported with private publishing/subscribing.

---

<div class="post-metadata">

**Author:** ![daneboomer](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/daneboomer/32/3653_2.png) [@daneboomer](https://community.particle.io/u/daneboomer)\
**Post date:** [February 12, 2015, 2:16pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/8 "2015-02-12T14:16:26Z")

</div>

That did it! Thank you Mr Moors7!

Golly. So whatever has happened to spark.publish and spark.subscribe in the last couple of months has broken private publishing/subscribing.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [February 12, 2015, 2:43pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/9 "2015-02-12T14:43:52Z")

</div>

Let’s ask @Dave what the state of private events to publish and subscribe is now then–maybe a bug report needs to be filed?

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [February 12, 2015, 3:39pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/10 "2015-02-12T15:39:20Z")

</div>

Hey Gang,

I got pinged, so here I am with code! 🙂

I just wrote this:

> <https://gist.github.com/dmiddlecamp/5a2476baa20583b6a57f>

It demonstrates working public and private subscribes, and will work across multiple cores (just tested it for good measure). Pub/Sub / privacy is definitely not broken, but it’s possible something isn’t behaving like you’d expect. I’ll review your code and see if anything jumps out.

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [February 12, 2015, 3:42pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/11 "2015-02-12T15:42:58Z")

</div>

It looks like your code is doing some things that will cause problems, here’s an excerpt from your file:

```auto
void setup() {
    pinMode(receiveLedPin, OUTPUT); //Set the pin controlling the Core2-dependent LED as an OUTPUT
    pinMode(sendLedPin, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
    pinMode(inputPin, INPUT); //Set the pin connected to the pushbutton as an INPUT
    digitalWrite(sendLedPin, LOW); //Ensure the local LED is set to off to begin with
    digitalWrite(receiveLedPin, HIGH); //Ensure the local LED is set to off to begin with
    Spark.publish("Core1Toggle", "State", 0, PRIVATE); //Set up Spark.publish() so that the state of the local LED is published to the Spark Cloud PRIVATELY
    Spark.subscribe("Core2Toggle", ledTwoToggle, MY_DEVICES); //Set up Spark.subscribe() so that the state of Core1's Led is recorded and handled by ledTwoToggle
}
 
void loop() {
    val = digitalRead(inputPin); //read value of pushbutton
   
    if (val == LOW) { //if clause activated if pushbutton pressed and thus inputPin reads LOW
        sendLedVal = !sendLedVal; //if clause activated if pushbutton pressed and thus inputPin reads LOW
        digitalWrite(sendLedPin, sendLedVal ? HIGH : LOW); //write the appropriate HIGH/LOW to the sendLed pin to turn it ON/OFF
        Spark.publish("Core1Toggle", sendLedVal ? "ON" : "OFF"); //publish the state to the Spark Cloud as ON/OFF
        delay(250); //primitive button debouncing
    }
}

```

For starters, you’re calling Spark.publish before your spark.subscribe call during your setup, and this will cause a race condition. If both your cores startup at the same time, those events will go into the void before your cores have subscribed to anything, and you’ll miss that message.

You also need to make sure your subscription type (MY\_DEVICES, vs. ALL), matches the privacy of your publish (PRIVATE, vs. ALL). Right now a “MY\_DEVICES” only subscription won’t catch a non-private publish, but that’s something I want to change because I think that can be a bit confusing. 🙂

I hope that helps!

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![MarkSHarrisTX](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marksharristx/32/4624_2.png) [@MarkSHarrisTX](https://community.particle.io/u/MarkSHarrisTX)\
**Post date:** [March 17, 2015, 7:33pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/12 "2015-03-17T19:33:52Z")

</div>

Ok, a newbie question. Do I enter this “[https://api.spark.io/v1/devices/events/?access\_token=ACCESTOKEN\_HERE](https://api.spark.io/v1/devices/events/?access_token=ACCESTOKEN_HERE)” command in a browser window? Or in Terminal?

The first takes a real long time and finally comes back with something like “ok”. The latter comes back with “No match”.

I’m trying to see if my spark.publish is working okay or not, since my program’s Spark.subscribe doesn’t seem to see it. Does 'No match" mean I’m not getting anything published? Or that the access code is wrong, although I copied and pasted that from my Web IDE.

---

<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:** [March 17, 2015, 9:12pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/13 "2015-03-17T21:12:15Z")

</div>

This should work with your browser just fine - unless you’re using a stubborn one like IE which always wants to download rather than display the events (at least on my machine)  
I use Chrome.

For tests you could use the public fire hose  
[https://api.spark.io/v1/events/?access\_token=ADD\_YOUR\_TOKEN](https://api.spark.io/v1/events/?access_token=ADD_YOUR_TOKEN)

Which should give you more results.

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [March 17, 2015, 9:16pm UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/14 "2015-03-17T21:16:24Z")

</div>

In a browser like Chrome or Firefox, do

```auto
view-source:https://api.spark.io/v1/events/?access_token=abcef...543210

```

to see all the public events do this for just a single core’s:

```auto
view-source:https://api.spark.io/v1/devices/012345..abcdef/events/?access_token=abcef...543210

```

where the part after devices is your core id.

---

<div class="post-metadata">

**Author:** ![MarkSHarrisTX](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marksharristx/32/4624_2.png) [@MarkSHarrisTX](https://community.particle.io/u/MarkSHarrisTX)\
**Post date:** [March 18, 2015, 5:49am UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/15 "2015-03-18T05:49:08Z")

</div>

That seems to list everybody’s events. I thought that giving my access token would limit to just mine.

---

<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:** [March 18, 2015, 7:17am UTC](https://community.particle.io/t/would-someone-mind-taking-a-look-at-this-spark-publish-spark-subscribe-code/9962/16 "2015-03-18T07:17:32Z")

</div>

That’s the reason why it’s called “public fire hose” it lists all public events 😉  
This is to see whether your browser would show any events at all.

If you want to see all your devices you’d use  
[https://api.spark.io/v1/devices/events/?access\_token=ACCESTOKEN\_HERE](https://api.spark.io/v1/devices/events/?access_token=ACCESTOKEN_HERE)

And if you only want to see one device  
[https://api.spark.io/v1/devices/DEVICE\_ID\_HERE/events/?access\_token=ACCESTOKEN\_HERE](https://api.spark.io/v1/devices/DEVICE_ID_HERE/events/?access_token=ACCESTOKEN_HERE)  
as @bko already said.
