Cloud function with two parameters

Hi all!

I am trying to write a simple function that allows me to turn on and off 4 Relays.

I read here:
https://docs.particle.io/reference/firmware/photon/#particle-function-

// Cloud functions must return int and take one String

How can I structure my function, then?

I’d do something like this:

void changeRelayS(int relayNumber, String command);
void changeRelayS(int relayNumber, String command){

  if (command == "on") {
      digitalWrite(relayNumber, LOW);
  }
  if (command == "off") {
      digitalWrite(relayNumber, HIGH);
  }
  
}

But it seems it is not possible.

Any ideas?

Thanks!

Have it take one argument, and parse that on the Photon. It’s what’s being done with the Tinker example as well, and isn’t uncommon in general.

1 Like

Hi, you may want to take a look at what @wgbartley has explained in this write-up.
Gustavo.

Interesting solution but his code will not compile as is…
Will check it :slight_smile:

Thanks, I like the easy way you suggest :slight_smile:

1 Like

That's easily fixed.

// replace
// Particle.variable("relayStates", &relayStates, INT);
// with
Particle.variable("relayStates", relayStates);

// and
// void toggleRelay(String command) {
// with
int toggleRelay(String command) {

@wgbartley, can you update that there too?
Keeping Binary States with Integers - Hackster.io

1 Like

Thanks for all your kind help and input.

Here’s my temporary poor man’s solution, I will check the other option in order to improve my coding:

    // ------------
    // Magic Relay Box
    // ------------
    
    
    /*-------------
    
    This code is used to control Relays from Arduino / Photon, using a similar module:
    https://www.amazon.de/dp/B00PQC34E6/ref=pe_386171_151021841_TE_item

    twitter.com/pitto
    Haarlem, September 2016
    
    -------------*/
    
    
    // Declaring changeRelayS function
    int changeRelayS(String command)
    {
    // Put into commaIndex variable comma character index number, in order to use it later to split the string
        int commaIndex = command.indexOf(',');
    // Use the comma separator index to take the 1st part of the command (containing Photon pin port number connected to Relay)
        String pinNumber = command.substring(0, commaIndex);
    // Use the comma separator index to take the 2nd part of the command (containing the command being sent e.g. on, off, on_off etc)
        String relayCommand = command.substring(commaIndex+1);
    // Convert the pinNumber from string to integer in order to successfully issue the digitalWrite command later on
        int pinNumberI = pinNumber.toInt();
    // Print both values to serial for debugging purpose
        Serial.print("Pin Number: " + pinNumber);  
        Serial.println();
        Serial.print("Relay Command: " + relayCommand);  
        Serial.println();
    
        if (relayCommand == "on") {
            
            digitalWrite(pinNumberI, LOW);
            return pinNumberI;
            
        } else if (relayCommand == "off") {
            
            digitalWrite(pinNumberI, HIGH);
            return pinNumberI;
            
        } else if (relayCommand == "on_off") {
            
            digitalWrite(pinNumberI, LOW);
            delay(1000);
            digitalWrite(pinNumberI, HIGH);
            return pinNumberI;
            
        } else {
            
            return -1;
            
        }
    
    }
    
    
    void setup() {
    // Initialize all pins to receive commands
        pinMode(D0, OUTPUT);
        pinMode(D1, OUTPUT);
        pinMode(D2, OUTPUT);
        pinMode(D3, OUTPUT);
    // Turn all pins off
        digitalWrite(D0, HIGH);
        digitalWrite(D1, HIGH);
        digitalWrite(D2, HIGH);
        digitalWrite(D3, HIGH);
    // Initialize serial port
        Serial.begin(9600);
    // Publish function on the cloud
        Particle.function("changeRelayS", changeRelayS);
    }
    
    void loop() {
    
    }

You can easily test it from Linux CLI using cURL:

curl https://api.particle.io/v1/devices/YOUR_DEVICE_ID/changeRelayS \
     -d access_token=YOUR_ACCESS_TOKEN \
     -d "D0,on"

Have fun :smile:

2 Likes