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", ¤tLevel, 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", ¤tLevel, 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!!!
}