Basic relay coding ( solved first part)

APOLOGIES DID SOME MORE SEARCHING AND FOUND CODE RELATED TO BLINK LED , SOLVED MY ISSUE. Now to add reed switches /notification on closed/Open and alerts.

Hi, very new to coding and automation of any sort. Thought now would be good to dip my toe. Had a spare pi b+ v3 laying around and thought i would try to automate my ageing garage door. Found this thread Particle/Google assistant and worked my way through with great results. However , being this is for switching a light on , it doesnt quite match what i need. i.e. I need a momentary button/voice push to simulate garage door button push. I have tried to amend the code below to include items such as “delay(1000);” but still only able to switch relay permanently on or perma off. Any help would be appreciated .

int relay = D1; //pin to which relay is connected
int boardLed = D7;
bool vin = LOW; //a virtual boolean variable

// setup() is run only once, it's where we set up GPIO and //initialise peripherals
void setup() {
    
  // Setup GPIO
  pinMode(relay,OUTPUT); // relay pin is set as output
  digitalWrite(relay,HIGH);
 // Subscribe to events published by IFTTT using  Particle.subscribe
  Particle.subscribe("Name_of_The_Event-1", myHandler); 
      //turning off function declaration
Particle.subscribe("Name_of_The_Event-2", thisHandler); 
      //turning on function declaration
}

// loop() runs continuously, it's our infinite loop.

void loop() {
     if (vin==HIGH){
         digitalWrite(relay,LOW);
     }
else if (vin==LOW){
         digitalWrite(relay,HIGH);
     }

}

//our events are called when IFTTT applets are triggered

void myHandler(const char *event, const char *data){
    vin=LOW;
}
void thisHandler(const char *event, const char *data){
     vin=HIGH;
}

Hey @freddy871, welcome to the Particle community.

Particle on Raspberry Pi is no longer officially supported. For future projects, I would recommend using a supported device like the Argon.

If you wish to use Raspberry Pi, you may be better suited by gpiozero.


Regarding your question, to pulse the relay, you could use a handler like this, and leave your loop() empty:

void pulseRelay(const char* event, const char* data) {
    digitalWrite(relay, HIGH);
    delay(1000);
    digitalWrite(relay, LOW);
}

For a reed switch, I would recommend wiring it between GND and an input pin, and enabling a pullup resistor. When the switch is closed, digitalRead will return LOW, and when open, it will return HIGH.

You will need to define reedPin globally and pinMode(reedPin, INPUT_PULLUP); in your setup().

To send alerts when the state of the reed switch changes, you could use the following in your loop():

static bool reedState = HIGH;
bool newState = digitalRead(reedPin);
if (newState != reedState) {
    reedState = newState;
    Particle.publish("reedStatus", (reedState) ? "OPEN" : "CLOSED");
    delay(250);
}

Hi N , thanks for quick reply, have some magnetic reed switches a d various bits n bobs laying around. Reluctant to buy extra components at this time as just a little covid lockdown project. As mentioned never coded anything before, so quite chuffed when I managed to get pi and relays working. Could be the start of something new, brain not as fresh now in my late 50s , but do find automationi quite interesting … cheers again for the info.

1 Like