Why isn't my first particle function not working?

I have this code and when I toggle my MQTT Switch the Reset2 Particle function works but the Reset1 one doesnt work and it shoud

int SolarInput = (A1);
int SolarRAW =0;
int VRaw = 0;
int sensorValue1 = 0;
int sensorValue = 0;
char *message = "SolarRelayOn";

String aString;
int Reset1(String command);
int Reset2(String command);
int Relay1 = (D0);
int Relay2 = (D1);

void setup() {
    
Serial.begin(9600);
pinMode(SolarInput , INPUT);

Particle.function("SolarRelayOn", Reset1);
Particle.function("SolarRelayOn", Reset2);

pinMode (D0,OUTPUT);
pinMode (D1,OUTPUT);

digitalWrite (D0,LOW);
digitalWrite (D1,LOW);
}

void loop() {
    
    char message[56];
    SolarRAW = analogRead(A1);
    VRaw = (analogRead(A1)/10);
    
    sensorValue = (SolarRAW);
    Serial.println(sensorValue);
    sprintf(message, "%d" , sensorValue);
    Particle.publish("Solar_Level", message, PUBLIC);
    delay(10000);
    
    sensorValue1 = (VRaw);
    Serial.println(sensorValue1);
    sprintf(message, "%d" , sensorValue1);
    Particle.publish("Solar_Level_RAW", message, PUBLIC);
    delay(10000);

}

int Reset1(String command) {

  if(command == "1")
  {
digitalWrite(D0,HIGH);
Particle.publish("D0_HIGH", "1",PRIVATE);
delay(10000);
digitalWrite(D0,LOW);
Particle.publish("D0_Reset", "0",PRIVATE);
  }
return 1 ;

}

int Reset2(String command) {

  if(command == "2")
  {
digitalWrite(D0,HIGH);
Particle.publish("D1_HIGH", "1",PRIVATE);
delay(10000);
digitalWrite(D0,LOW);
Particle.publish("D1_Reset", "0",PRIVATE);
  }
return 1 ;

}

Your two Particle.functions need to have two different names, not the same name.

1 Like

You also should not use that long delays in a Particle.function() callback.

Given @ric’s comment, I’d rather doubt this statement :wink:

Seeing that code, it definetly should not work with the same name used twice.
You might want it to, but it won’t.

But given the “copy/paste” logic of the two functions, I’d just go with one and do the differentiation via the command parameter.

BTW, you are using D0 in both functions and don’t use your RelayX variables either.

So the [ Particle.function(“SolarRelayOn”, Reset1); ] should be like Particle.function(“SolarRelay1On”,Reset1); ??

Generally speaking: Yes!

But …

LOL thanks about the D0 & D1 missed that part

I tried that and it didn’t seem to take …when I toggled Switch 1 it worked but when I toggles switch 2 nothing happened

I am using node red as an intermediary so that might be the issue as well …just thought id ask

You may want to test your firmware via https://console.particle.io/devices
There you can test the functions with a known to work environment

Thanks I will

Thanks for your help

int Reset1(String command) {

  if(command =="1")
  {
digitalWrite(D0,HIGH);
Particle.publish("D0_HIGH", "1",PRIVATE);
delay(10000);
digitalWrite(D0,LOW);
Particle.publish("D0_Reset", "0",PRIVATE);
}
else if(command =="2")
  {
digitalWrite(D1,HIGH);
Particle.publish("D1_HIGH", "1",PRIVATE);
delay(10000);
digitalWrite(D1,LOW);
Particle.publish("D1_Reset","0", PRIVATE);
  }

return 1 ;

}

Seems to work ok now

@jade7272, to properly format your code it should be done like so,
```cpp
// code here
```

The ` is not an apostrophe, it’s the grave accent mark that is under the tilde, on the key to the left of the “1” key.

1 Like

Was wondering how that was done thanks

You should edit your previous post to apply this fix to the formatting.

1 Like

Done thanks