# \[Solved\] Spark.Publishing and Subscribing between 2 cores

**URL:** <https://community.particle.io/t/solved-spark-publishing-and-subscribing-between-2-cores/8339>\
**Category:** Troubleshooting\
**Created:** [November 22, 2014, 2:52am UTC](https://community.particle.io/t/solved-spark-publishing-and-subscribing-between-2-cores/8339 "2014-11-22T02:52:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meth](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Meth](https://community.particle.io/u/Meth)\
**Post date:** [November 22, 2014, 2:52am UTC](https://community.particle.io/t/solved-spark-publishing-and-subscribing-between-2-cores/8339/1 "2014-11-22T02:52:40Z")

</div>

Hey Guys I am stuck. I can’t get my cores to respond with each other. The scenario is that when I touch one of the core the other cores should blink and vice versa. So I have read the documents and seen a lot of similar examples but I am stack I can not get it to execute. I think I am missing something I have used this posts to get an idea, any help is appreciated thanks in advance.

1. List item  
[http://community.spark.io/t/spark-subscribe-attempt/4852/13](http://community.spark.io/t/spark-subscribe-attempt/4852/13)  
[http://www.hackster.io/maxeust/quantumly-entangled-leds1](http://www.hackster.io/maxeust/quantumly-entangled-leds1)  
[http://blog.spark.io/2014/05/12/subscribe-and-hackster/](http://blog.spark.io/2014/05/12/subscribe-and-hackster/)  
Here is my code

Code for core1

```
// Which I/O pins are we using
    #define zPin A2
    #define Dpin D1 // Input pin for the led to blink when its moved
     //Minimum and Maximum data for the Z axes- the accelerometer
    int zAccMin = 1722;
    int zAccMax = 2400;
    
    int sendLedPin = D1; // Blinks when accelerometer is touched
    int zAccPin = zPin; // Value for reading the data for the Z axe
    int receiveLedPin = D2; // The pin that blinks when it receives.
    
    void setup()
    {
        Serial.begin(9600); // sets the serial port to 9600
        pinMode(zAccPin, INPUT);
        pinMode(receiveLedPin, OUTPUT); 
        pinMode(sendLedPin, OUTPUT);  
        Spark.subscribe("motionDetected2", ledTwoToggle, MY_DEVICES); 
    }

           void loop()
   {
       zAccPin = analogRead(zPin); // read the value of the Z axe sensor
       if(zAccPin == (zAccMin) || zAccPin < (zAccMax)){ // If core1 is lifted up 
                digitalWrite(sendLedPin, HIGH);
                Spark.publish("motionDetected1","ON");
                Serial.print("Zaxe is ActiveCORE1 "); //Debug
                Serial.print("\t");
                delay(100);
            } 
            else
            {
                digitalWrite(sendLedPin, LOW);
                Spark.publish("motionDetected1","OFF");
                Serial.print("Zaxe is NotActiveCORE1"); //Debug
                Serial.print("\t");
                delay(100);
            }
        }    
        void ledTwoToggle(const char *event, const char *onOff){ 
            if (strcmp(onOff,"ON"))  
            { 
              digitalWrite(receiveLedPin, HIGH); 
            } else if (strcmp(onOff, "OFF")) )
            { 
              digitalWrite(receiveLedPin, LOW);
            }
        }

```

For core 2 its similar to cores 1

```
// Which I/O pins are we using
#include "application.h"
        // Which I/O pins are we using
        #define zPin A2
        #define Dpin D1 // Input pin for the led to blink when its moved
        //Minium and Maxium data for the axes- the accelerometer 
        int zAccMin = 1722;
        int zAccMax = 2400;
        int sendLedPin = D1;
        int zAccPin = zPin; 
        int receiveLedPin = D2; 
      
        void setup()
        {
            Serial.begin(9600); // sets the serial port to 9600
            pinMode(receiveLedPin , OUTPUT); 
            pinMode(sendLedPin, OUTPUT); 
            Spark.subscribe("motionDetected1", ledTwoToggle, MY_DEVICES);
        }
        
        void loop()
        {
            zAccPin = analogRead(zPin); 

                 if(zAccPin == (zAccMin) || zAccPin < (zAccMax)){
                digitalWrite(sendLedPin, HIGH); 
                Spark.publish("motionDetected2", "ON"); //Publish the state to the Spark Cloud as ON
                Serial.print("Zaxe is ActiveCORE2 "); //Debug
                Serial.print(zAccPin); //Debug
                Serial.print("\t");
                delay(250); // Primitive debouncing 
            } else{
                digitalWrite(sendLedPin, LOW);
                Spark.publish("motionDetected2","OFF");
                Serial.print("Zaxe is NotActiveCORE1"); //debug
                Serial.print("\t");
                delay(100);;
            }
        }
        
        void ledTwoToggle(const char *event, const char *onOff){ //Handler function for Spark.subscribe ()
            if (strcmp(onOff,"ON")) ){ 
              digitalWrite(receiveLedPin, HIGH); 
            } else if (strcmp(onOff, "OFF")) { 
              digitalWrite(receiveLedPin, LOW); 
            }
        }

```

Any help is appreciated  
Thanks  
Meth

---

<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:** [November 22, 2014, 8:40am UTC](https://community.particle.io/t/solved-spark-publishing-and-subscribing-between-2-cores/8339/2 "2014-11-22T08:40:58Z")

</div>

@Meth, I’d suspect that your `char *onOff` does not actually look like you’d expect it to, so your `strcmp()` might not actually satisfy the if-condition.  
Just for the test you could `Serial.println(onOff)` outside any if-statement

BTW: To save yourself the hassle of having two different firmwares for your two Cores, you could publish/subscribe to the same event and just check the `deviceID` of the event message against the receivers own `Spark.deviceID()` to distinguish if its a self-published event or a “foreign” one.

---

<div class="post-metadata">

**Author:** ![Meth](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Meth](https://community.particle.io/u/Meth)\
**Post date:** [November 22, 2014, 7:03pm UTC](https://community.particle.io/t/solved-spark-publishing-and-subscribing-between-2-cores/8339/3 "2014-11-22T19:03:52Z")

</div>

> [@ScruffR](#):
>
> Serial.println(onOff)

Hi Thanks for the help yes there was some issue with the strcmp. I have fixed it know.

---

<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:** [November 22, 2014, 11:14pm UTC](https://community.particle.io/t/solved-spark-publishing-and-subscribing-between-2-cores/8339/4 "2014-11-22T23:14:13Z")

</div>

Glad it worked 👍
