I have once done something similar for another member
// name all devices with a common prefix e.g ScruffyMaster, ScruffySlave, ...
#define COMMON_DEVICE_NAME_PREFIX "Scruffy"
/* Types required for RGB-LED control */
struct ARGB_COLOR {
byte B; // blue channel
byte G; // green channel
byte R; // red channel
byte A; // alpha channel to indicate RGB.control true (!= 0) / false (== 0)
};
union COLOR {
unsigned int value;
ARGB_COLOR argb;
};
struct devSetup
{
int pinTrigger;
int pinAction;
COLOR RGB;
char devName[32];
};
devSetup devices[] =
{
{ -1, -1, 0x000000, "" },
{ D0, D7, 0xFF0000, "ScruffyRed"},
{ D1, D7, 0x00FF00, "Scruffy3G"},
{ D2, D7, 0x0000FF, "ScruffyBlue"},
{ D6, D7, 0xFFFFFF, "ScruffyWhite"}
};
const int devCnt = sizeof(devices) / sizeof(devSetup);
char myName[32];
int myIdx;
void sparkEvents(const char *topic, const char *data)
{
Serial.printlnf("%s: %s", topic, data);
if (strstr(topic, "spark/device/name"))
{
strcpy(myName, data);
Serial.printlnf("myName = %s", myName);
// find my own index in devices list
for (myIdx = 1; myIdx < devCnt && strcmp(myName, devices[myIdx].devName); myIdx++);
myIdx %= devCnt; // if I'm not in there reset to 0
}
}
void devEvents(const char *topic, const char *data)
{
int idx;
int state;
Serial.printlnf("%s received %s: %s", myName, topic, data);
if (strstr(topic, myName))
{
Serial.println("Uhh, That's me!");
if (strstr(data, "=") && strstr(data, ";")) // rudimentary sanity check
{
idx = atoi(data);
if (0 < idx && idx < devCnt)
{
state = atoi(strstr(data, "=") + 1);
if (state < 0) // negative numbers indicates toggle current state
state = !digitalRead(devices[idx].pinAction);
digitalWrite(devices[idx].pinAction, state);
Serial.printlnf("%s told me to %s its action", devices[idx].devName, state ? "START" : "STOP");
if (devices[idx].RGB.value > 0)
RGB.color(devices[idx].RGB.argb.R, devices[idx].RGB.argb.G, devices[idx].RGB.argb.B);
}
else
Serial.println("I shan't talk to strangers!");
}
}
else
Serial.println("Meh, no one loves me!");
}
void setup()
{
Serial.begin(115200);
for (int i=0; i < devCnt; i++) // setup LED pins
{
pinMode(devices[i].pinTrigger, INPUT_PULLUP);
pinMode(devices[i].pinAction, OUTPUT);
if (devices[i].RGB.value > 0)
{
RGB.control(true);
RGB.color(0,0,0);
}
}
Particle.subscribe("spark/", sparkEvents);
Particle.subscribe(COMMON_DEVICE_NAME_PREFIX, devEvents, MY_DEVICES);
Particle.publish("spark/device/name"); // push cloud to send my device name
}
void loop()
{
static uint32_t msLastPublish;
int devNr;
for (int i = 1; i < devCnt; i++)
{
if (!digitalRead(devices[i].pinTrigger))
{
Particle.publish(devices[i].devName, String::format("%d=%d;", myIdx, -1), PRIVATE); // -1 for toggle state (otherwise HIGH/LOW for explicit state)
delay(1010); // currently there is an issue #1023 with the rate limit
}
}
}
You have some buttons for your buddy devices you want to signal and on the receiver side the RGB LED indicates who called.
And then the receiver can either answer back or in turn invite another buddy. (or so
it’s a while since)