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.