Code for Reed Switch to Publish an event as an IFTTT Trigger


#1

I am currently running a Particle Photon on this: https://www.controlanything.com/product/i2c_devices/Relay-Controller?sku=MCP23008_SCIO7R1G5LE_IFTTT_10A

with the following firmware:

/* Includes ------------------------------------------------------------------*/
#include "NCD1Relay.h"

NCD1Relay relayController;

SYSTEM_MODE(AUTOMATIC);

int triggerRelay(String command);

bool tripped[7];

int debugTrips[7];

int minTrips = 5;

/* This function is called once at start up ----------------------------------*/
void setup()
{
	Particle.function("controlRelay", triggerRelay);
	Serial.begin(115200);
	relayController.setAddress(0,0,0);
}

/* This function loops forever --------------------------------------------*/
void loop()
{
	int status = relayController.readAllInputs();
	int a = 0;
	for(int i = 1; i < 65; i*=2){
		if(status & i){
			debugTrips[a]++;
			if(debugTrips[a] >= minTrips){
				if(!tripped[a]){
					tripped[a] = true;
					//set input trip event to true
					String eventName = "Input_";
					eventName+=(a+1);
					Particle.publish(eventName, "ON");
					Serial.print("eventName: ");
					Serial.println(eventName);
					Serial.print("eventContents: ");
					Serial.println("ON");
				}
			}
		}else{
			debugTrips[a] = 0;
			if(tripped[a]){
				tripped[a] = false;
				//set input trip event to false
				String eventName = "Input_";
				eventName+=(a+1);
				Particle.publish(eventName, "OFF");
				Serial.print("eventName: ");
				Serial.println(eventName);
				Serial.print("eventContents: ");
				Serial.println("OFF");
			}
		}
		a++;
	}
}

int triggerRelay(String command){
	if(command.equalsIgnoreCase("on")){
		Serial.println("Turning on relay");
		relayController.turnOnRelay();
		Serial.println("returning");
		return 1;
	}
	if(command.equalsIgnoreCase("off")){
		relayController.turnOffRelay();
		return 1;
	}
	if(command.equalsIgnoreCase("toggle")){
		relayController.toggleRelay();
		return 1;
	}
	if(command.equalsIgnoreCase("momentary")){
		relayController.turnOnRelay();
		delay(300);
		relayController.turnOffRelay();
		return 1;
	}
	return 0;
}

I know nothing about the logic or format of the coding required to program this device. I have found a few internet guide for using a reed switch to control an LED over the internet. But, what I really want to do is use a reed switch to publish an event to particle as a trigger through IFTTT to send me a notification on my phone.

The totality of the project is to be able to use the device above with Alexa to control my garage door, and know what state my garage door is in via notifications.

It’d be great if someone knows how to do code this without too much effort, but i’m also willing to learn if someone wants to take the time to walk me through it. I don’t know if the above code needs to be totally rewritten or if code related to the reading of a reed switch and publishing of an event can just be added.

Thanks in advance for any help.


#2

Have you selected your reed switch already?


#3

Something simple like this

But open to suggestions for best function.


#4

Something like this should work:

Connect one side of the reed switch to GND and another to pin D5.

You will see the blue LED on the Photon change state with the reed switch status and a publish message every five seconds.

#define reedSwitch D5
#define ledPin D7

void setup()
{
    //setting up reedswitch GPIO to be an Input pulled to 3V3
    pinMode(reedSwitch, INPUT_PULLUP);
    
    //Setting up onboard blue LED
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
}


unsigned long lastPublishedTime = 0;

void loop() {
    if (millis() - lastPublishedTime > 5000) {
        if (checkReedSwitch ) {
            Particle.publish("reed", "closed");
        }
        else {
            Particle.publish("reed", "closed");
        }
        lastPublishedTime = millis();
    }
}


//---- Everything below to check if Reed switch is opened or closed ----//


int counter = 0;       // how many times we have seen new value
int reading;           // the current value read from the input pin
int current_state = LOW;    // the debounced input value

// the following variable is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastTime = 0;         // the last time the output pin was sampled
int debounce_count = 10; // number of millis/samples to consider before declaring a debounced input


boolean checkReedSwitch() {
    // If we have gone on to the next millisecond
    if(millis() != lastTime) {
        reading = digitalRead(reedSwitch);
        
        if(reading == current_state && counter > 0) {
            counter--;
        }
        if(reading != current_state) {
            counter++; 
        }
        // If the Input has shown the same value for long enough let's switch it
        if(counter >= debounce_count) {
            counter = 0;
            current_state = reading;
            digitalWrite(ledPin, current_state);
        }
        
        lastTime = millis();
        return current_state;
    }
}

#5

Wow thanks. Before i try to add this code i have a couple questions.

  1. I want to use the publishing of an event to trigger a notification to my phone through ifttt, but only if the status of the switch changes i.e. from open to close or vice versa. It looks like this will publish an event every 5 seconds, is that right?

  2. Can i run this code and the original firmware above at the same time?


#6

Oh and how/where do i add this code to my current firmware code above?


#7

you can create a new app and paste it in.

Let’s at least get this working and you understanding what is going on in the code before going further. That’s where you learn something. :wink:


#8

Ok makes sense. So get the switch wired up, flash the code in, and the trigger the switch to see what happens?


#9

Sounds right. You can also do it with a normal switch/button if you have them handy.