Spark.subscribe help

I thought I was doing a simple proof of concept between two cores for integration into my project, but I seem to be hung up on the subscribe end of things. My core running the publish code is publishing like its supposed to as seen via subscribe in the CLI. The other core is not responding with its led as expected. Thanks as always.

Publish Code:

const int switchPin = D0;
const int ledPin = D7;
int switchStatus = 0;
volatile bool high_flag= 0;

unsigned long startTime;
unsigned long duration = 5000;


void setup() {
    pinMode(switchPin, INPUT_PULLDOWN);
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);

    attachInterrupt(switchPin, switchHigh, RISING);
}



void loop() {
    switchStatus = digitalRead(switchPin);
    digitalWrite(ledPin, switchStatus);
    if (high_flag) {
        
            startTime = millis();
                    
             while(switchStatus == HIGH) {
                switchStatus = digitalRead(switchPin);
                
                
                if(millis() - startTime >= duration)    {
                    
                    Spark.publish("switch-status","alert");
                    delay(1000);
                    //break;
                }
             
            }
        
        
    
    }
    
    high_flag = 0;
    
}


void switchHigh() {
    
    high_flag = 1;
       
}

Subscribe Code:

int led = LOW;

void dimmer(const char *event, const char *data)    {
    if(strcmp(data,"alert"))    
        led = HIGH;
    else
        led = LOW;
    digitalWrite(D7, led);
    
}

void setup()    {

    pinMode(D7, OUTPUT);
    Spark.subscribe("switch-status", dimmer);
 }


void loop() {
    
    digitalWrite(D7, led);
   
}

@techbutler, the strcmp function returns 0 if both strings are equal, so your if should be

if(0 == strcmp(data, "alert")){
    ...
}

Also don’t forget to add some small delay in your loop and add some logic to publish the button up state once it is pressed, otherwise your second core’s LED will be always on once the button is pressed.

3 Likes

That will most certainly make a difference :wink:
Seeing as you’re already setting the LED status with your subscribed data, I don’t see a reason for doing so in the loop. And seeing that there will never be a message other than “alert”,

else
        led = LOW;

won’t have any effect; the LED won’t turn off, like @krvarma mentioned.

Anyhow, perhaps you’ll find this useful as well: http://www.hackster.io/maxeust/quantumly-entangled-leds1

2 Likes

Also, sometimes it’s quicker to debug code locally (ish) than to try to debug it on the core. I’ve used, and recommend, ideone.com. Here’s an example of me “fixing” your problem and testing it on ideone.com: http://ideone.com/RKLbnz and here’s my fix. Not as clear as @krvarma’s but a little cleaner.

if(!strcmp(data,"alert"))   // If data == alert 
    cout << "data is 'alert'";
else
    cout << "data is something other than 'alert'";
2 Likes

That did the trick @krvarma . Thank you. I know it looks clunky… I was just trying to get the two talking with a button before incorporating this into my project which involves remotely turning on and dimming flood lights based on sensor input from another core.

Thanks for those other resources @Moors7 and @harrisonhjones, always welcome.

3 Likes