Switching 5 relays via Google Home, IFTTT & Particle


#21

Hi thanks for that, yes I missed the comma.

It now works!! So thank you so much for your help. I clearly need to learn more to expand function and usability. Where to start!?


#22

Hi, Thanks for the guidance, as a very unskilled tinkerer it would be good to learn best practice. Can you elaborate a little (or a lot in my case)? In relation to the code above. Would be nice to have a Switch All command/option too.


#23

That should be easy, e.g. by using the 0 for all relays. Unless there is a “all” command which would then need to be treated that way.


Update:

Maybe not best practice :see_no_evil:, but on the way to it :wink: might need more iterations to get there, but tha main expected issues with erronous input data should be caught this way.

const int relayCount = 5;

int setRelay(const char* args)
{
  char msg[64];
  char relayState[5];
  int  relayNumber;
  int  r0           = 0;
  int  rX           = relayCount;
  bool relayOn      = false;

  switch (sscanf(args, "%d,%4s", &relayNumber, relayState))
  {
    case 2: // all 2 arguments parsed
      relayOn = (strcmp(relayState, "on") == 0);

    case 1: // only 1 argument parsed
      if (0 == relayNumber)
        ; // do nothing, initial values are set to controll ALL relays
      else if (0 < relayNumber && relayNumber <= relayCount) 
        r0 = rX = (relayNumber-1);
      else 
        break; // relay number disallowed, bail out

      for (int i = r0; i < rX; i++)
      {
        pinMode(relay[i], OUTPUT); // set as output
        digitalWrite(relay[i], relayOn);
      }
      break;

    default:
      relayNumber = -1; // no valid relay number provided
      break;
  }   

  // ------- optional -------
  snprintf(msg, sizeof(msg), "%s -> r[%d] state: %s", args, relayNumber, relayOn ? "ON" : "OFF");
  Particle.publish("log", msg, PRIVATE); 
  // ------------------------

  return relayNumber*100 +  relayOn;
}

#24

Well done. The mighty @ScruffR strikes again.


#25

OK I am a bit stuck, I added in the pin assignments for the each relay at the top as

int relay[5] = {SDA,SCL,D0,D1,D2};

But should something like

Particle.function("setRelay", setRelay);

be in there? I ask because in IFTTT asks for the call function which is unavailable but {{NumberField}} {{TextField}} show as input (Function Input). Particle console is not picking up anything.


#26

For one, I’d not use SDA & SCL but rather the pin labels printed on the board.
Using any other labels (like you did) will cover the fact that SDA & SCL are also used otherwise (i.e. SDA = D0 & SCL = D1)
See
https://docs.particle.io/reference/firmware/photon/#wire-i2c-
https://docs.particle.io/faq/particle-devices/i2c-faq/photon/#pins

In where?
I only rewrote setRelay() all the other functions @nrobinson2000 has provided you with are to be taken the same.


#27

Yes, thanks I am aware of that, its just I have an old set up that is wired that way that I am using for testing. I will look again at integrating the two blocks of code to get this working. Thanks again.


#28

So you have a setup that connects two relays to one pin - twice?

  • relay[0] … D0 (SDA)
  • relay[1] … D1 (SCL)
  • relay[2] … D0
  • relay[3] … D1
  • relay[4] … D2

#29

@MettaUK are you still running this on a Raspberry Pi? Which 5 pins do you want to use?


#30

:flushed: @nrobinson2000, I totally missed that :flushed:

In that case, the point I made about the pins is mute :see_no_evil:


#32

@MettaUK I’m trying to do the same project as you did, where did you put the comma ?
I’m using your code posted above , the logs from Particle console are okay i think, ex 1 off , 5 off but the gpio pins are not changing their state. All of them (D4,D5,D6,D7,D8) are remaining HIGH regardless of the input.
Any idea why is this happening?