[Solved] Spark.Publishing and Subscribing between 2 cores

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://www.hackster.io/maxeust/quantumly-entangled-leds1
    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

@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.

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

Glad it worked :+1: