The function is working but subscribe is not -- It did work a week ago!

My Code:

    /* Includes ------------------------------------------------------------------*/
     #include "NCD2Relay/NCD2Relay.h"

    NCD2Relay relayController;

    SYSTEM_MODE(AUTOMATIC);

    int triggerRelay(String command);

    void arlsHandler(const char *event, const char *data);

     //String arlsDr = "Straight";

    /* This function is called once at start up ----------------------------------*/
    void setup()
    {
	    Particle.function("controlRelay", triggerRelay);
	    Particle.subscribe("arls", arlsHandler, MY_DEVICES);
     // Particle.variable("arlsDirec", arlsDr);
	    Serial.begin(115200);
	    relayController.setAddress(0,0,0);
    }

    /* This function loops forever --------------------------------------------*/
    void loop()
    {
     //
    }

    int triggerRelay(String command){
	if(command.equalsIgnoreCase("turnonallrelays")){
		relayController.turnOnAllRelays();
		return 1;
	}
	if(command.equalsIgnoreCase("turnoffallrelays")){
		relayController.turnOffAllRelays();
		return 1;
	}
	if(command.startsWith("setBankStatus:")){
		int status = command.substring(14).toInt();
		if(status < 0 || status > 255){
			return 0;
		}
		Serial.print("Setting bank status to: ");
		Serial.println(status);
		relayController.setBankStatus(status);
		Serial.println("done");
		return 1;
	}
	//Relay Specific Command
	int relayNumber = command.substring(0,1).toInt();
	Serial.print("relayNumber: ");
	Serial.println(relayNumber);
	String relayCommand = command.substring(1);
	Serial.print("relayCommand:");
	Serial.print(relayCommand);
	Serial.println(".");
	if(relayCommand.equalsIgnoreCase("on")){
		Serial.println("Turning on relay");
		relayController.turnOnRelay(relayNumber);
		Serial.println("returning");
		return 1;
	}
	if(relayCommand.equalsIgnoreCase("off")){
		relayController.turnOffRelay(relayNumber);
		return 1;
	}
	if(relayCommand.equalsIgnoreCase("toggle")){
		relayController.toggleRelay(relayNumber);
		return 1;
	}
	if(relayCommand.equalsIgnoreCase("momentary")){
		relayController.turnOnRelay(relayNumber);
		delay(300);
		relayController.turnOffRelay(relayNumber);
		return 1;
	}
	return 0;
    }

    void arlsHandler(const char *event, const char *data){
    //Serial.printlnf("%s: <%s>", (const char*)event, (const char*)data);
    if(strcmp(event, "arls") == 0 && strcmp(data, "on") == 0){
        relayController.turnOnAllRelays();
    }else if(strcmp(event, "arls") == 0 && strcmp(data, "off") == 0){
        relayController.turnOffAllRelays();
    }
  }

From the console:

{“name”:“arls”,“data”:“off”,“ttl”:“60”,“published_at”:“2016-10-24T01:47:12.597Z”,“coreid”:“001”}
{“name”:“arls”,“data”:“off”,“ttl”:“60”,“published_at”:“2016-10-24T01:47:31.700Z”,“coreid”:“001”}
{“name”:“arls”,“data”:“on”,“ttl”:“60”,“published_at”:“2016-10-24T01:47:40.863Z”,“coreid”:“001”}
{“name”:“arls”,“data”:“off”,“ttl”:“60”,“published_at”:“2016-10-24T01:48:17.670Z”,“coreid”:“001”}
{“name”:“arls”,“data”:“off”,“ttl”:“60”,“published_at”:“2016-10-24T01:53:04.438Z”,“coreid”:“001”}
{“name”:“arls”,“data”:“on”,“ttl”:“60”,“published_at”:“2016-10-24T01:53:08.126Z”,“coreid”:“001”}
{“name”:“arls”,“data”:“on”,“ttl”:“60”,“published_at”:“2016-10-24T01:53:28.852Z”,“coreid”:“001”}

Who is publishing the event that is subscribed?

Me on the cli

Do you see your device when doing particle list in that CLI session?

You should always have some unconditional code path in the handler to see if it gets called at all (like your commented Serial.print() - which looks familiar :sunglasses:)

void arlsHandler(const char *event, const char *data) {
  Serial.printlnf("%s: <%s>", (const char*)event, (const char*)data);
  if(strcmp(event, "arls") == 0 && strcmp(data, "on") == 0){
    relayController.turnOnAllRelays();
  } else if(strcmp(event, "arls") == 0 && strcmp(data, "off") == 0) {
    relayController.turnOffAllRelays();
  }
}

What version CLI are you using?
How are you publishing?

particle publish eventName dataPayload
-- or
particle publish eventName dataPayload --private

I was using the non private cli call. And yes the code is the same one from the product I was creating.

Have you tried private then?
Since you are subscribing MY_DEVICES you would need to Particle.publish(evtName, data, PRIVATE) from a device, so it would be just consistent to do the same with CLI.

2 Likes

particle publish arls on --private

Ran this and no joy… Is the syntax wrong somehow?

This is what I get

$> particle publish arls on --private
Published private event: arls

Hence again

I get the same return as you and I am running 1.16.0

With this slightly adapted handler


void arlsHandler(const char *event, const char *data){
  char txt[64];
  snprintf(txt, sizeof(txt), "%s: <%s>", (const char*)event, (const char*)data);
  Particle.publish("received", txt, PRIVATE);
  if(strcmp(event, "arls") == 0 && strcmp(data, "on") == 0){
    relayController.turnOnAllRelays();
  } else if(strcmp(event, "arls") == 0 && strcmp(data, "off") == 0) {
    relayController.turnOffAllRelays();
  }
}

I tried these commands

$> particle publish arls off
Published public event: arls

$> particle publish arls on --private
Published private event: arls

And got the expected result

Yes I see it in list. I’ve been in the country of Panama for the week. I will resume this process this week. @ScruffR thanks for that insightful coding!

1 Like