Particle.publish vs Spark.publish - Help?

Hi all,

I’m a bit confused with the update of what was Spark.publish (or Spark.subscribe) to now being Particle.publish. I’ve tried all sorts of combinations and can’t get Particle.publish to work. I’ve used v0.4.7 (latest at time of writing) and also down to v0.3.4 on my old Spark Core. I’m now trying to use two Photons to keep things simple but I’m still stuck!

I’ve mostly been trying to base my code around this project: https://www.hackster.io/maxeust/quantumly-entangled-leds1-1257c5

I’ve tried that exact code and also changing the words “Spark” to “Photon” and I can’t get aything to work.

Could anyone lend me some help around this? Is it a simple matter of changing the words or is the code quite different?

Thanks! :slight_smile:

Can you use the unmodified code, attempt to compile and tell us what is the error?

The quick and easy workaround is to have the publish and subscribe public.
But I guess you have not tried all combinations, sice there are some that also work with privat events.

There is an open issue for this which also contains a test sketch to find out what works

Thanks for the replies - I’ve just figured it out. Once I tidy up my code I’ll post here so that others can see and learn from it :slight_smile: Cheers!

2 Likes

Ok here we go! So basically this is a simple remote control to turn on an off an LED (momentary) with a pushbutton. This is a great code to base other ideas off.

“Send” Photon (device with button):

/*
Basic code to send pushbutton state to the cloud which can in turn be read by another Particle board.
Written (partly) by Pilbromatic. Kudos to Max Eusterbrock for the base code to work off.
*/


int localLED = D7; //choose the pin for the send (local) LED
int inputPin = D0; //choose the input pin (for a pushbutton)
int inputState = 0; //variable for reading the pushbutton pin status

void setup()
{
    pinMode(localLED, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
    pinMode(inputPin, INPUT_PULLDOWN); //Set the pin connected to the pushbutton as an INPUT with internal pulldown enabled
}

void loop()
{
    inputState = digitalRead(inputPin); //read value of pushbutton
    
    if (inputState == HIGH) //if clause activated if pushbutton pressed and thus inputPin reads HIGH
    {
        Particle.publish("DeskPhoton", "high", 0, PRIVATE); //publish the state to the Particle Cloud as high
        digitalWrite(localLED, HIGH);
        delay(2500); //primitive button debouncing. This is set high to stop the cloud being overloaded with publish requests

        inputState = digitalRead(inputPin); //read value of pushbutton to see if held for longer than 2.5s

        if (inputState == HIGH)
        {
            Particle.publish("DeskPhoton", "high", 0, PRIVATE); //publish the state to the Particle Cloud as high
            digitalWrite(localLED, HIGH);
            delay(2500); //primitive button debouncing. This is set high to stop the cloud being overloaded with publish requests
        }

        Particle.publish("DeskPhoton", "low", 0, PRIVATE); //publish the state to the Particle Cloud as low
        digitalWrite(localLED, LOW);
        delay(250); //primitive button debouncing

    }

//    else
//    {
//    }
}

“Receive” Photon (device with LED output) - this part is probably unneccessarily complex for now until I fully get my head around the Particle.subscribe command:

/*
Basic code to receive pushbutton state from the cloud designed to be used with a secondary "transmit" Photon.
Written (partly) by Pilbromatic. Kudos to Max Eusterbrock for the base code to work off.
*/


int pwBtn = D7; //choose the pin for the receive (Core1-dependent LED)
void LEDstate(const char *toggle, const char *onOff); //handler function for Particle.subscribe()
 
void setup()
{
    pinMode(pwBtn, OUTPUT); //Set the pin controlling the Core1-dependent LED as an OUTPUT
    Particle.subscribe("DeskPhoton", LEDstate, MY_DEVICES); //Set up Particle.subscribe() so that the state of Core1's Led is recorded and handled by LEDstate
}
 
void loop()
{
}


void LEDstate(const char *toggle, const char *onOff) //handler function for Particle.subscribe()
{
    if (strcmp(onOff, "high") == 0)
    {
        digitalWrite(pwBtn, HIGH); //turn on LED output
    }
    else if (strcmp(onOff, "low") == 0)
    {
        digitalWrite(pwBtn, LOW); //turn off LED output
    }
}

Ok hang on, I’ve just found out that my code is a bit buggy! I will work it out and amend.

One other thing to make sure of, is to prevent violation of the publish limit of one per sec (burst 4 with 4sec pause) - especially after cloud (re)connect, since this already causes two burst publishes.

Easiest “fix”: Increase your debounce to delay(1000);

Yup thanks - I figured that was the issue. Adjusting the delay timers does make a work-around however I need better accuracy with timing for this particular circuit. I’m just working out a better format of the code (and trying to avoid using an interrupt) to do this.

Probably bed time now but I’ll have another crack tomorrow if I get time! :smile: Cheers.

EDIT: Above code has been changed. This basically does a double check - one every 2.5s. For my particular use I need to either push the button for <4s or >4s, so the 2.5s threshold is perfect - others can change this to suit their requirements. Now the code only sends “high” and “low” states to the cloud when the button is interacted with.