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:

// 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?


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.

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 -

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:
    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.print("Relay Command: " + relayCommand);  
        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);
            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
    // Publish function on the cloud
        Particle.function("changeRelayS", changeRelayS);
    void loop() {

You can easily test it from Linux CLI using cURL:

curl \
     -d access_token=YOUR_ACCESS_TOKEN \
     -d "D0,on"

Have fun :smile: