-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_homekit_togglebuttons.ino
120 lines (95 loc) · 3.5 KB
/
esp32_homekit_togglebuttons.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <HomeSpan.h>
class MyPushButton : public Service::StatelessProgrammableSwitch {
String name;
Characteristic::ProgrammableSwitchEvent* switchEvent; // Reference to the switch event characteristic
public:
MyPushButton(const char* name)
: Service::StatelessProgrammableSwitch(), name(name) {
new Characteristic::Name(name);
switchEvent = new Characteristic::ProgrammableSwitchEvent(0); // Initialize with default event
}
void triggerSingleButtonPress() {
Serial.println(name + " programmatically pressed");
switchEvent->setVal(Characteristic::ProgrammableSwitchEvent::SINGLE_PRESS); // Set to SINGLE_PRESS
}
void triggerDoubleButtonPress() {
Serial.println(name + " programmatically pressed");
switchEvent->setVal(Characteristic::ProgrammableSwitchEvent::DOUBLE_PRESS); // Set to SINGLE_PRESS
}
};
class ToggleSwitch : public Service::Switch {
String name;
SpanCharacteristic* power; // Reference to the On Characteristic
MyPushButton* button;
public:
ToggleSwitch(const char* name)
: Service::Switch(), name(name) { // Use the Switch service
new Characteristic::Name(name);
power = new Characteristic::On();
}
boolean update() override {
boolean currentlyOn = power->getVal();
boolean calledOn = power->getNewVal();
boolean intendedOn = (currentlyOn != calledOn) ? calledOn : !currentlyOn;
Serial.println("Switch command received:");
Serial.print("currentlyOn: ");
Serial.println(currentlyOn ? "true" : "false");
Serial.print("calledOn: ");
Serial.println(calledOn ? "true" : "false");
Serial.print("IntendedOn: ");
Serial.println(intendedOn ? "true" : "false");
power->setVal(intendedOn);
digitalWrite(2, intendedOn ? HIGH : LOW);
if (intendedOn) {
button->triggerSingleButtonPress();
} else {
button->triggerDoubleButtonPress();
}
return true;
}
void setButton(MyPushButton* button) {
this->button = button;
}
};
class ToggleButtonAccessory {
public:
ToggleButtonAccessory(const char* name) { // Use the Switch service
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Name(name);
new Characteristic::Manufacturer("BAR Technologies");
new Characteristic::SerialNumber("300000001");
new Characteristic::Model("BAR-VTS");
new Characteristic::FirmwareRevision("1.0");
new Characteristic::Identify();
ToggleSwitch* toggleSwitch = new ToggleSwitch("Toggle Switch"); // Create a new toggle switch using the button instances
MyPushButton* button = new MyPushButton("Push Button");
toggleSwitch->setButton(button);
}
};
void
setup() {
Serial.begin(115200);
homeSpan.setWifiCallback([]() {
homeSpan.setPairingCode("11122333");
Serial.println("WiFi Connected. Pairing Code Set.");
});
homeSpan.begin(Category::Bridges, "BAR Hub");
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Manufacturer("BAR Technologies");
new Characteristic::SerialNumber("100000001");
new Characteristic::Model("BAR-H1");
new Characteristic::FirmwareRevision("1.0");
new Characteristic::Identify();
const int numberOfAccessories = 20;
for (int i = 1; i <= numberOfAccessories; i++) {
String accessoryName = "Toggle Button " + String(i); // Create the name
new ToggleButtonAccessory(accessoryName.c_str());
}
new Service::HAPProtocolInformation();
new Characteristic::Version("1.1.0");
}
void loop() {
homeSpan.poll(); // Needed to process HomeKit-related tasks
}