Arduino-HomeKit-ESP8266 icon indicating copy to clipboard operation
Arduino-HomeKit-ESP8266 copied to clipboard

How can I add a physical button to switch example?

Open greg21greg opened this issue 5 years ago • 17 comments

greg21greg avatar Nov 13 '20 08:11 greg21greg

Ask Google.

Mixiaoxiao avatar Nov 13 '20 09:11 Mixiaoxiao

I tried that already :( I was hoping someone could help me out here.

greg21greg avatar Nov 13 '20 18:11 greg21greg

Were you able to make it work? I'm trying to make it work

JGConcepcion avatar Dec 12 '20 02:12 JGConcepcion

I tried it, within switch example there is 3 lines of commented code to submit led pin status to ios app. from line 77 to 79.

but when i did that system started going into reset mode continiously exception(0), i would love help with it.

pratik019 avatar Dec 14 '20 18:12 pratik019

I was able to make it work already but it involved the use of another library which is the ESPbutton. I added this piece of code (See attached image) into the my_homekit_setup() and I got no error or whatsoever. working

JGConcepcion avatar Dec 14 '20 18:12 JGConcepcion

@JGConcepcion con you share your code?

yepkevin612 avatar Dec 23 '20 06:12 yepkevin612

I used Acebutton Libray to work with Push button Switch with INPUT_PULLUP

  • [x] When you turn ON the swich The LED will on and it update state in Home App also same like when you switch OFF.

Edit program in Example02_Switch.ino

  • First,Add this before void setup()

    #include <AceButton.h>
           using namespace ace_button;
           const int BUTTON_PIN = 14; //edit your switch pin here.
           AceButton button(BUTTON_PIN);
          void handleEvent(AceButton*, uint8_t, uint8_t);
          int lock_state;
    
  • Second,In void setup() add

    pinMode(BUTTON_PIN, INPUT_PULLUP);
    button.setEventHandler(handleEvent);
    
  • Third,Add button.check(); in your void loop() like this.

void loop() {
	my_homekit_loop();
        button.check(); //ADD THIS.
	delay(10);
}
  • Last, Add the switch event funtion(Like below) at the below of progarm.
void handleEvent(AceButton* /* button */, uint8_t eventType,
    uint8_t /* buttonState */) {
  switch (eventType) {
    case AceButton::kEventPressed:
      if(lock_state==0){
        cha_switch_on.value.bool_value = true;
        bool on = cha_switch_on.value.bool_value;
        Serial.println("LED:ON(Force)");
        digitalWrite(PIN_SWITCH, HIGH);
        homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
        lock_state = 1;
        }
      break;
    case AceButton::kEventReleased:
      if(lock_state==1){
        cha_switch_on.value.bool_value = false;
        bool on = !cha_switch_on.value.bool_value;
        Serial.println("LED:OFF(Force)");
        digitalWrite(PIN_SWITCH, LOW);
        homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
        lock_state = 0;
      }
      break;
  }
}

Varogkorn avatar Feb 15 '21 15:02 Varogkorn

@Varogkorn thank you so much,i still cannot turn off ,when i hold the button there is off,when i released my hand the switch continous turn on.

yepkevin612 avatar Feb 24 '21 14:02 yepkevin612

Hi! void handleEvent(AceButton* /* button /, uint8_t eventType, uint8_t / buttonState */) { //Serial.println('lock_state'); switch (eventType) { case AceButton::kEventPressed: if(lock_state==1){ cha_switch_on.value.bool_value = true; bool on = cha_switch_on.value.bool_value; //Serial.println("LED:ON(Force)"); digitalWrite(PIN_SWITCH, HIGH); homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value); lock_state = 0; } else { cha_switch_on.value.bool_value = false; bool on = !cha_switch_on.value.bool_value; //Serial.println("LED:OFF(Force)"); digitalWrite(PIN_SWITCH, LOW); homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value); lock_state = 1; }
break; case AceButton::kEventReleased: break; } }

alexxxpol avatar Mar 02 '21 14:03 alexxxpol

@alexxxpol Thank you , it work nice.

yepkevin612 avatar Mar 03 '21 12:03 yepkevin612

How to change the HIGH to LOW position on home app?? I mean, even the exemple works inverse for me, it is on when off in home app, and off when it's on in my home app.

I'm using a Wemos d1 mini v3

alexandrerochasalles avatar Apr 09 '21 10:04 alexandrerochasalles

Hello,

Pleas in function swich_on_set(), change the digitalWrite(2, switch_1_power) to digitalWrite(2, !(switch_1_power)).

void switch_1_on_set(homekit_value_t value) { if (value.format != homekit_format_bool) { printf("Invalid on-value format: %d\n", value.format); return;} switch_1_power = value.bool_value; digitalWrite(2, switch_1_power); //led_update(); }

Pozdrawiam/Mit freundlichen Grüssen Dawid Rosak TEL. +48 609105069

Sent from my iPhone

On 9 Apr 2021, at 12:21, Alexandre Rocha @.***> wrote:

 How to change the HIGH to LOW position on home app?? I mean, even the exemple works inverse for me, it is on when off in home app, and off when it's on in my home app.

I'm using a Wemos d1 mini v3

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

david19610 avatar Apr 09 '21 15:04 david19610

Hello, Pleas in function swich_on_set(), change the digitalWrite(2, switch_1_power) to digitalWrite(2, !(switch_1_power)). void switch_1_on_set(homekit_value_t value) { if (value.format != homekit_format_bool) { printf("Invalid on-value format: %d\n", value.format); return;} switch_1_power = value.bool_value; digitalWrite(2, switch_1_power); //led_update(); } Pozdrawiam/Mit freundlichen Grüssen Dawid Rosak TEL. +48 609105069

I can't find the function that you are talking about. Can you please give me the full code? Because for the switch function I'm using as the switch example:

void cha_switch_on_setter(const homekit_value_t value) {
	bool on = value.bool_value;
	cha_switch_on.value.bool_value = on;	//sync the value
	LOG_D("Switch: %s", on ? "ON" : "OFF");
	digitalWrite(PIN_SWITCH, on ? LOW : HIGH);
}

void my_homekit_setup() {
	pinMode(PIN_SWITCH, OUTPUT);
	digitalWrite(PIN_SWITCH, HIGH);

	cha_switch_on.setter = cha_switch_on_setter;
	arduino_homekit_setup(&config);

}

alexandrerochasalles avatar Apr 09 '21 17:04 alexandrerochasalles

Hello,

you probably need to refresh status of the button, see the highlighted text in yellow

Pozdrawiam Dawid Rosak

pt., 9 kwi 2021 o 19:24 Alexandre Rocha @.***> napisał(a):

Hello, Pleas in function swich_on_set(), change the digitalWrite(2, switch_1_power) to digitalWrite(2, !(switch_1_power)). void switch_1_on_set(homekit_value_t value) { if (value.format != homekit_format_bool) { printf("Invalid on-value format: %d\n", value.format); return;} switch_1_power = value.bool_value; digitalWrite(2, switch_1_power); //led_update(); } Pozdrawiam/Mit freundlichen Grüssen Dawid Rosak TEL. +48 609105069 … <#m_2188757451150947713_>

I can't find the function that you are talking about. Can you please give me the full code? Because for the switch function I'm using as the switch example:

// access your HomeKit characteristics defined in my_accessory.c extern "C" homekit_server_config_t config; extern "C" homekit_characteristic_t cha_switch_on;

void cha_switch_on_setter(const homekit_value_t value) {

    bool on = value.bool_value;

cha_switch_on.value.bool_value = on; //sync the value

      //update current status of Button on Apple Home app
      homekit_characteristic_notify(&cha_switch_on,

cha_switch_on.value);

LOG_D("Switch: %s", on ? "ON" : "OFF");

digitalWrite(PIN_SWITCH, on ? LOW : HIGH);

}

void my_homekit_setup() {

pinMode(PIN_SWITCH, OUTPUT);

digitalWrite(PIN_SWITCH, HIGH);

cha_switch_on.setter = cha_switch_on_setter;

arduino_homekit_setup(&config);

}

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266/issues/82#issuecomment-816835729, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASNFVVJF5AP4ORLWQ54UYHDTH4Z5ZANCNFSM4TUIVHKQ .

david19610 avatar Apr 09 '21 19:04 david19610

@david19610 Thank you so much for the help!!

alexandrerochasalles avatar Apr 10 '21 00:04 alexandrerochasalles

Search for a project named "4 Relay & Switch" by Sachin Soni (aka techiesms). He has several projects that use physical switches and relays and I've got it working fine with HomeKit on a couple of different devices.

madmacks59 avatar Dec 31 '21 05:12 madmacks59

The problem with the Techiesms project is that when you interact with the HomeKit App, you have to doble click the physical switch. I made the project a few months ago and I’m still dealing with that problem.

DenisMaldonadoC avatar Nov 22 '22 03:11 DenisMaldonadoC