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

Back in November I had some fairly simple code 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!

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

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

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?

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.

[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
[2]: http://pastebin.com/VVUcLa4B

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.

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!

1 Like

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?

2 Likes

Hey Gang,

I got pinged, so here I am with code! :slight_smile:

I just wrote this:

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

2 Likes

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

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. :slight_smile:

I hope that helps!

Thanks,
David

1 Like

Ok, a newbie question. Do I enter this “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.

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

Which should give you more results.

In a browser like Chrome or Firefox, do

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:

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

where the part after devices is your core id.

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

That’s the reason why it’s called “public fire hose” it lists all public events :wink:
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

And if you only want to see one device
https://api.spark.io/v1/devices/DEVICE_ID_HERE/events/?access_token=ACCESTOKEN_HERE
as @bko already said.

1 Like