Update:
Well, it turns out I picked the wrong relay. Didn't realize it at the time but that was a latching relay. I had to signal the relay twice to get it to change state. So, I had to go buy some other ones.. which of course I could only find in a 5 pack. (https://www.amazon.ca/gp/product/B07YFGSK5Q/ref=ppx_yo_dt_b_asin_title_o07_s00?ie=UTF8&th=1)
Using the proper relays and the HomeSpan library, I've got the solution working on the bench. Now I'm just waiting for a prototype board to come in so I can solder it instead of the breadboard I'm using now. In the meantime, here's what I'm using to achieve this three-way approach, first the code:
/******************************
* This program is used to control a relay to turn a fireplace on and off
* while converting the existing wall switch into a three way switch.
*
* To do this, the existing wall switch will need to be wired between the Ground and a GPIO Pin on the ESP32.
* The Relay would be attached to the other GPIO Pin.
*
* Once in place, the wall switch changing state, whether being turned on or turned off,
* will cause a flip-flop in both the relay and the HomeKit state. This allows the user the ability to turn on
* the fireplace with either the wall switch or Homekit, and also turn off the fireplace with either the
* wall switch or Homekit. This is an improvement over the other provided solutions that require the fireplace
* to be turned off via the same method it was turned on.
*
*****************************/
#include "HomeSpan.h"
#include "DEV_FIREPLACE.h"
void setup() {
//Setup the Serial Monitor.
Serial.begin(115200);
homeSpan.begin(Category::Heaters,"Fireplace");
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Name("Fireplace");
new Characteristic::Manufacturer("Heat-N-Glo");
new Characteristic::SerialNumber("123-ABC");
new Characteristic::Model("6000DVTFL");
new Characteristic::FirmwareRevision("0.9");
new Characteristic::Identify();
new Service::HAPProtocolInformation();
new Characteristic::Version("1.1.0");
new DEV_FIREPLACE(16,17); //Structure is the Pin Number going to the Relay Input, followed by the Pin Number where the wall switch gets attached.
}
void loop() {
// put your main code here, to run repeatedly:
homeSpan.poll();
}
and
/**********************************************************
* New structure to support a Relay as a Switch in Homekit
*********************************************************/
int switchStatus;
int previousSwitchStatus;
struct DEV_FIREPLACE : Service::Switch {
int switchPin;
int wallSwitchPin;
SpanCharacteristic *power;
SpanCharacteristic *name;
DEV_FIREPLACE(int switchPin, int wallSwitchPin) : Service::Switch(){
power=new Characteristic::On();
name=new Characteristic::Name("Fireplace");
this->switchPin=switchPin;
this->wallSwitchPin=wallSwitchPin;
pinMode(switchPin,OUTPUT);
pinMode(wallSwitchPin,INPUT_PULLUP);
}
boolean update(){
digitalWrite(switchPin,power->getNewVal());
return(true);
}
void loop(){
switchStatus = digitalRead(wallSwitchPin);
if(switchStatus != previousSwitchStatus)
{
//Need to make Homekit flip
power->setVal(1-power->getVal());
digitalWrite(switchPin,power->getNewVal());
//Then update the previous status to the new status
previousSwitchStatus = switchStatus;
}
}
};
Supporting this, I would need to wire the ESP32 and the Relay (sort of, not exact as I didn't draw in all the components on the relay board, just the transistor and the relay itself so I can represent the VCC, GND and Trigger the board takes) like this:
Once I get the ESP, relay and connectors mounted into a permanent state, I'll update again with how those turned out.
Thanks again for all the help.
Mike
