[SOLVED] Particle.variable not working with function (*char var)

I think there is a conceptual misunderstanding.

Particle.variables() are only set up once to expose a global variable and are not published (as you seem to be trying).
Once you've exposed a variable any change to the underlying variable will immediately be accessible via the Particle.variable().

Taking your code slightly refurbished

const int LED = D7;

String strName;
String strValue;

void setup() 
{
    pinMode(LED, OUTPUT);
    digitalWrite(LED, HIGH);

    Particle.variable("Name", strName);   // make strName available to the cloud
    Particle.variable("Value", strValue); // ditto for strValue
  
    Particle.subscribe("spark/", handler);
    Particle.publish("spark/device/ip");
    Particle.publish("spark/device/name");    
}


void handler(const char *Name, const char *Value) 
{
    strName = String(Name);   // new content will now be available as Particle.variable
    strValue = String(Value); // ditto
    // delay (1000); // NEVER delay a handler
}

To clarify for drivers-by: Since the Serial RX buffer is never read or flushed one byte entered will stay there and keep the expression TRUE forever.

1 Like