Master and slave photon

Hi All

I am currently sat here with two photons. what i am trying to do is on a (master photon) when i press a button it turns its own D7 light off and then the (slave photon) which is also subscribed to the same event turns its D7 light on.
i also only want to publish from the master when there is a state change.

any help would be great

also could you please tell me what the variables in this line means

void TurnOutputOn(const char *event, const char *data)

code from master

int highfloat = D4; // high float will be connected to pin d2 on the unit
int highlevellight = D7;

void setup() 
{
//pinMode(highfloat, INPUT_PULLUP);// declare that pin an INPUT
pinMode(highfloat, INPUT_PULLDOWN);
/*If the float switch connects D3 to 3V3 and is open when the level is high:
pinMode(highfloat, INPUT_PULLDOWN) is required instead.
And: if (digitalRead(highfloat) == LOW)
*/
pinMode(highlevellight, OUTPUT);// declare the highlevellight pin as an output
//highfloat = LOW; // set the input as low to make sure  the program is not started with it on
//highlevellight = HIGH;
}

void loop() {

   
    
    if (digitalRead(highfloat) == LOW)
    {
    Particle.publish("TANKHEALTHY");
    digitalWrite(highlevellight, HIGH);
 
    delay(1000);
    }
    else
    {
    digitalWrite(highlevellight, LOW);
    }
    delay(1000);
    
}

code from slave

int relayPin = D7;
//char publishString[40];


void setup() {
pinMode(relayPin, OUTPUT);


}

void loop() {
    Particle.subscribe("TANKHEALTHY", TurnOutputOn);
    delay(10000);

}

void TurnOutputOn(const char *event, const char *data)
{
   digitalWrite(relayPin, HIGH); 
     
}

The docs do tell, such stuff
https://docs.particle.io/reference/firmware/photon/#particle-subscribe-

In order to only trigger on a state change you'd store the old state in a global or static variable and only do your work if the current state is unequal to the old state.

Here is an example of a Master/Slave relationship using Publish/Subscribe.

Via my Home Automation controller, pushbutton or DO button, I control the brightness of one under-counter LED strip. Adjustments to the dimmer value are broadcast to the slaves and they follow along…

I hope you find it of use.

Master:

#include "Fade.h"
#include <application.h>

#define WEBHOOK_STRING_LENGTH 60
#define POWER_PIN D4     // Toggles LEDs on/off
#define FADE_UP_PIN D6   // increases LED fade
#define FADE_DOWN_PIN D2 // decreases LED fade
#define FADE_BUTTON_INCREMENT 25  // modify to step up/down in larger or smaller increments
#define PWM_PIN  A4      // output pin to your MOSFET

const int buttonPin[] = {POWER_PIN, FADE_UP_PIN, FADE_DOWN_PIN};  // array for the pushbuttons

int oldValue[3];
int currentLevel = 0;
int lastSetpoint = 255;

const unsigned int localPort = 2222;
unsigned long udpHeartbeatTimer = millis();
unsigned long lastPress;
String myParticleID;// = Particle.deviceID();

//extern "C" char* itoa(int a, char* buffer, unsigned char radix);

Fade ledStrip(PWM_PIN, 15); // Create the Fade object, set the PWM pin and set the time between increments in milliseconds

void setup()
{
  myParticleID = Particle.deviceID();
  Particle.function("setDimLevel" , incomingDimCommand);
  Particle.variable("currentLevel", &currentLevel, INT);
  int startupLevel = 255;
  ledStrip.write(startupLevel);
  for (int i = 0; i < 3; i++)
  {
    pinMode(buttonPin[i], INPUT_PULLUP);
  }
  pinMode(PWM_PIN, OUTPUT);
  char myMessage[60] = "Kitchen Dimmer Reset: now set to max";
  Particle.publish("pushover", String(myMessage), 60, PRIVATE); // Spark WebHook!!!
}
//
void loop()
{
  ledStrip.update();
  currentLevel = ledStrip.read();
  for (int i = 0; i < 3; i++)
  {
    int value = digitalRead(buttonPin[i]);
    if (value != oldValue[i] && value == LOW && millis() - lastPress > 50)
    {
      lastPress = millis();
      int fadeTarget = ledStrip.read();
      switch (i)
      {
        case 0:
          fadeTarget = fadeTarget? 0 : 255;
          break;
        case 1:
          fadeTarget = min(fadeTarget + FADE_BUTTON_INCREMENT, 255);
          break;
        case 2:
          fadeTarget = max(fadeTarget - FADE_BUTTON_INCREMENT, 0);
          break;
        default:
          // Nothing to do here...
          ;
      }
      Particle.publish("kitchenDimmer", String(fadeTarget), 60, PRIVATE);
      ledStrip.write(fadeTarget);
    }
    oldValue[i] = value;
  }
}

int incomingDimCommand(String params)
{
  int value = params.toInt();
  if (value == -1) // i.e. -1 to toggle
  {
    value = (ledStrip.read()? 0 : 100);
  }
  else
  {
    value = constrain(value, 0, 100);
  }
  Particle.publish("kitchenDimmer", String(value), 60, PRIVATE);
  ledStrip.write(map(value, 0, 100, 0, 255));
  char myMessage[60] = "";
  sprintf(myMessage, "Kitchen Dimmer now set to %d%%", value);
  Particle.publish("pushover", String(myMessage), 60, PRIVATE); // Spark WebHook!!!
  return value;
}

Slave:

// This #include statement was automatically added by the Particle IDE.
#include "Fade.h"

#include <application.h>

const int LED_PIN = D1;
const int RECOVERY_BRITENESS = 255;

int currentLevel = 0;

Fade ledStrip(LED_PIN, 15);
//
void setup()
{
  pinMode(D7, OUTPUT);
  pinMode(D1, OUTPUT);
  digitalWrite(D7, HIGH);
  Particle.function("setDimLevel" , incomingDimCommand);
  Particle.variable("currentLevel", &currentLevel, INT);
  Particle.subscribe("kitchenDimmer", masterSetpoint, MY_DEVICES);//kitchenDimmer
  ledStrip.write(RECOVERY_BRITENESS);
  char myMessage[60] = "";
  sprintf(myMessage, "Dimmer Slave Reset: now set to %d%%", map(ledStrip.setPoint(), 0, 255, 0, 100));
  Particle.publish("pushover", String(myMessage), 60, PRIVATE); // Spark WebHook!!!
}
//
void loop()
{
  ledStrip.update();
  currentLevel = ledStrip.read();
}
//


int incomingDimCommand(String params)
{
  int value = params.toInt();
  if (value == -1) // i.e. -1 to toggle
  {
    value = (ledStrip.read()? 0 : 100);
  }
  else
  {
    value = constrain(value, 0, 100);
  }
  ledStrip.write(map(value, 0, 100, 0, 255));
  char myMessage[60] = "";
  sprintf(myMessage, "Kitchen Dimmer Slave now set to %d%%", value);
  Particle.publish("pushover", String(myMessage), 60, PRIVATE); // Spark WebHook!!!
  return value;
}

void masterSetpoint(const char *event, const char *data)
{
  String dataReturn = String(data);
  char dataBuffer[4] = "";
  dataReturn.toCharArray(dataBuffer, 4);
  ledStrip.write(map(atoi(dataBuffer), 0, 100, 0, 255));
  char myMessage[60] = "";
  sprintf(myMessage, "Dimmer Slave now set to %d%%", atoi(dataBuffer));
  Particle.publish("pushover", String(myMessage), 60, PRIVATE); // Spark WebHook!!!
}

thanks for your reply, i have managed to get the two working using two subscribes when different events occur