Skip to content

Instantly share code, notes, and snippets.

@dmiddlecamp
Created February 12, 2015 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmiddlecamp/5a2476baa20583b6a57f to your computer and use it in GitHub Desktop.
Save dmiddlecamp/5a2476baa20583b6a57f to your computer and use it in GitHub Desktop.
a working demo of public/private publish / subscribe for multiple cores
#define publish_delay 10000
#define PUBLISH_TO "some_private_event_1"
#define PUBLIC_EVENT "some_public_event_1"
unsigned int lastPublish = 0;
void setup() {
Serial.begin(115200);
//the firmware uses the event name to figure out which handler it should toss things to, and doesn't select based on privacy, so we need to also make the event names differ in this example.
Spark.subscribe("some_public", publicEventHandler);
Spark.subscribe("some_private", privateEventHandler, MY_DEVICES);
}
void loop() {
unsigned int now = millis();
if ((now - lastPublish) > publish_delay) {
Serial.println("Publishing...");
Spark.publish(PUBLIC_EVENT, String(now));
Spark.publish(PUBLISH_TO, String(now), 60, PRIVATE);
lastPublish = now;
}
}
void publicEventHandler(const char* name, const char* data) {
Serial.println("heard event: " + String(name));
}
void privateEventHandler(const char* name, const char* data) {
Serial.println("heard private event: " + String(name));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment