Hi all,
I have a few situations where there are multiple controllers on a single physical location. One of the controllers has a set of sensors that allows the controller to make a decision (open or close?). The other controllers should react to that and do the operation (open or close).
Settings are stored in the EEPROM of the controller with an object. The object has an item to store if the controller is a master (making decisions) or a slave (actually opening an closing). Another item in the object is the projectName this identifies the controller as part of a set of controllers for a single location.
My idea is to make the masters publish an event with the eventName being the projectName and the data being “open” or “close”.
Here is my test code.
int addr = 1; // EEPROM addres
struct MyObject { // create object structure
bool status; // master = true, slave = false
char project[64]; // project name
} settings;
String projectName = "projectX";
void setup(){
bool status;
bool action = false;
unsigned long previousMillis;
int interval = 20000;
}
void loop(){
// publish as if controller is a master (this controller is master and slave for testing purposes)
if ((millis() - previousMillis) >= interval){
// publish event to open or close.
if (action == false){
Particle.publish(projectName, "open");
Serial.println("Master says to open");
action = !action;
}
else if (action == true){
Particle.publish(projectName, "close");
Serial.println("Master says to close");
action = !action;
}
previousMillis = millis();
}
}
class Subscriber {
public:
Subscriber() {
Particle.subscribe(projectName, &Subscriber::handler, this);
}
void handler(const char *eventName, const char *data) {
Serial.println(data);
if (data == "open"){
Serial.println("Slave will open");
}
else if (data == "close"){
Serial.println("Slave will close");
}
else {
Serial.println("invalid command");
}
}
};
When I use a static projectName like “My_project_name” the program works great.
When I use String projectName = “My_project_name” and use the code above the publish succeeds but in the log I get this:
����������������������������������������
Published by 2e0036000xxxxxxxxxxxxx on 11/21/18 at 9:39:21 am
instead of:
My_project_name
Published by 2e0036000xxxxxxxxxxxxxxxx on 11/21/18 at 9:50:09 am
Am I making an error here or am I abusing the Particle.publish function?