Particle.subscribe question

Hello, I have a program that receives and displays temp and humidity from a remote device via particle.subscribe. I'm running tests if with the same device I can read values generated by another remote device, obviously replacing the variables as follows

Particle.subscribe("dhtData", dataHandler, MY_DEVICES);
Particle.subscribe("RelData", relaisHandler, MY_DEVICES);
Particle.subscribe("**dhtDataP**", dataHandlerP, MY_DEVICES);
Particle.subscribe("**RelDataP**", relaisHandlerP, MY_DEVICES); 

and making the necessary changes in the program.
I would like to know your opinion on this, is it okay or there are other ways to go.
Thank you Valentino

1 Like

I wouldn't see the need for two separate sets of handlers.
Since the subscribe filter is a prefix, you can use the same handler for both the local and remote publishes.

You could do

  Particle.publish("dhtDataLocal", yourData);
  // or 
  Particle.publish("dhtDataRemote", yourData);

and still use one handler

  Particle.subscribe("dhtData", dhtHandler);

void dhtHandler(String event, String data) {
  if (event.endsWith("Local")) {
    // specific treatment for local publish
  else if (event.endsWith("Remote")) {
    // specific treatment for remote publish
  // common for all cases

If you'd use something like the device ID or device name as event postfix (instead of Local and Remote) you can even distinguish between many devices with one handler.


Thank you and good day

1 Like

Hello today I tried to enter what you suggested, unfortunately it gives me an error, and I'm not able to fix it. Attached is part of the program and the error message.
Thanks Valentino

Particle.subscribe("dhtData", dhtHandler);

void dhtHandler(const char *event, const char *data) {
// specific treatment for local publish
  if (event.endsWith("Local")) {
  /*void dataHandler(const char *event, const char *data)*/ {
    static uint32_t ms30Sec = 0;
    if (millis() - ms30Sec >= 1000) {                      // 1000 when last execution is at least one second in the past
        ms30Sec          = millis();
        char  Temp[8];
        char  Umi[8];
        float t, h;
        //if (sscanf(data, "temp:%[^,],umi:%[^,]", sTemp, sHum) == 2) {
        if (sscanf(data, "temp:%[^,],umi:%[^,]", Temp, Umi) == 2) {
        t = atof(Temp);
        h = atof(Umi);
        Serial.printlnf("Temp = %.1f, Umi = %.1f", t, h);
        Particle.publish("Tem&Umi:", data, PRIVATE); //*****remmati per non incrementare il consumo*****
        display.print("Temp & Umi Casa");
        //Serial.print("Dopo oled ");Serial.print("Temp :");Serial.print(t);Serial.print(" Umi :");Serial.println(h);

Msg Error

request for member 'endsWith' in 'event', which is of non-class type 'const char*'

endsWith() is a method of the String class but your even is a const char*.
That's why I used this in my suggested code above

void dhtHandler(String event, String data) {

or you go the more elaborate way of using vanilla C string functions.

Typically I'd favor the const char* approach, but since a internal subscribe logic already uses String there is no harm in using it as long you don't manipulate the String in ways that may cause heap fragmentation.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.