Skip to content

Instantly share code, notes, and snippets.

@gregmankes
Last active August 3, 2016 15:45
Show Gist options
  • Save gregmankes/5691f234fe0ffb6d0ca3e26141563b07 to your computer and use it in GitHub Desktop.
Save gregmankes/5691f234fe0ffb6d0ca3e26141563b07 to your computer and use it in GitHub Desktop.
The generic code for the heart-to-heart project, swap 'hunter dozen' to your event name and swap the numbers associated with it (hunter_dozen_button_pressed_1,2,3...) to flash this to multiple particles
/*****************************************************
* Author: Greg Mankes
* Date: 1/8/16
* Description: This Program publishes and subscribes
* to an event to a separate photon. When each Photon
* receives the event, the board LED will light up.
* When a user presses a button, the photon will
* publish an event
****************************************************/
int boardLed = D7;
int pushButton = D6;
void myHandler(const char *, const char *);
void setup() {
Particle.unsubscribe(); // unsubscribe from previous if any
pinMode(boardLed,OUTPUT); // Our on-board LED is output
pinMode(pushButton, INPUT);// Set up a pull up resistor push button at pin
// Here we are going to subscribe to your buddy's event using Spark.subscribe
Particle.subscribe("hunter_dozen_button_pressed_1", myHandler, MY_DEVICES);
// Subscribe will listen for the event buddy_unique_event_name and, when it finds it, will run myHandler()
Serial.begin(9600);
}
void loop() {
// This loop sends a publish when the beam is broken.
if (digitalRead(pushButton) == LOW) {
Particle.publish("hunter_dozen_button_pressed_2","pressed",PRIVATE);
// publish this public event
// rename your_unique_event_name with your actual unique event name. No spaces, 63 ASCII characters.
delay(1000);
}
else {
// publish that the button isn't pressed
Particle.publish("hunter_dozen_button_pressed_2","notPressed",PRIVATE);
delay(1000);
}
}
// Now for the myHandler function, which is called when the cloud tells us that our buddy's event is published.
void myHandler(const char *event, const char *data)
{
if(strcmp(data,"pressed")==0){
digitalWrite(boardLed, HIGH);
}
else{
digitalWrite(boardLed, LOW);
}
}
@gregmankes
Copy link
Author

This code has some flaws in it such that at the time, you needed to have a delay in publishing an event. This can be attributed to the documentation (due to spark's transition to particle) and may not be necessary anymore. Be sure to check the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment