Using Generic Esp32 Board
THE CODE:
**WaterTest.ino:**
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include "wifi_info.h"
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n"), ##__VA_ARGS__);
void setup() {
Serial.begin(115200);
wifi_connect(); // in wifi_info.h
//homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example
my_homekit_setup();
}
void loop() {
my_homekit_loop();
delay(10);
}
//==============================
// HomeKit setup and loop
//==============================
// access your HomeKit characteristics defined in my_accessory.c
extern "C" homekit_server_config_t config;
extern "C" homekit_characteristic_t cha_switch_on;
extern "C" homekit_characteristic_t cha_switch_on2;
extern "C" homekit_characteristic_t cha_switch_on3;
extern "C" homekit_characteristic_t cha_switch_on4;
static uint32_t next_heap_millis = 0;
#define PIN_SWITCH 5
#define PIN_SWITCH2 2
#define PIN_SWITCH3 14
#define PIN_SWITCH4 12
//Called when the switch value is changed by iOS Home APP
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 cha_switch_on_setter2(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on2.value.bool_value = on; //sync the value
LOG_D("Switch: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH2, on ? LOW : HIGH);
}
void cha_switch_on_setter3(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on3.value.bool_value = on; //sync the value
LOG_D("Switch: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH3, on ? LOW : HIGH);
}
void cha_switch_on_setter4(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on4.value.bool_value = on; //sync the value
LOG_D("Switch: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH4, on ? LOW : HIGH);
}
void my_homekit_setup() {
pinMode(PIN_SWITCH, OUTPUT);
digitalWrite(PIN_SWITCH, HIGH);
pinMode(PIN_SWITCH2, OUTPUT);
digitalWrite(PIN_SWITCH2, HIGH);
pinMode(PIN_SWITCH3, OUTPUT);
digitalWrite(PIN_SWITCH3, HIGH);
pinMode(PIN_SWITCH4, OUTPUT);
digitalWrite(PIN_SWITCH4, HIGH);
//Add the .setter function to get the switch-event sent from iOS Home APP.
//The .setter should be added before arduino_homekit_setup.
//HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function.
//Maybe this is a legacy design issue in the original esp-homekit library,
//and I have no reason to modify this "feature".
cha_switch_on.setter = cha_switch_on_setter;
cha_switch_on2.setter = cha_switch_on_setter2;
cha_switch_on3.setter = cha_switch_on_setter3;
cha_switch_on4.setter = cha_switch_on_setter4;
arduino_homekit_setup(&config);
//report the switch value to HomeKit if it is changed (e.g. by a physical button)
//bool switch_is_on = true/false;
//cha_switch_on.value.bool_value = switch_is_on;
//homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
}
void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if (t > next_heap_millis) {
// show heap info every 5 seconds
next_heap_millis = t + 5 * 1000;
LOG_D("Free heap: %d, HomeKit clients: %d",
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
}
}
> my_accessory.c
#include <homekit/homekit.h>
#include <homekit/characteristics.h>
// Define characteristics for each switch
homekit_characteristic_t cha_switch_on = HOMEKIT_CHARACTERISTIC_(ON, false);
homekit_characteristic_t cha_switch_on2 = HOMEKIT_CHARACTERISTIC_(ON, false);
homekit_characteristic_t cha_switch_on3 = HOMEKIT_CHARACTERISTIC_(ON, false);
homekit_characteristic_t cha_switch_on4 = HOMEKIT_CHARACTERISTIC_(ON, false);
// Define the accessory information
homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, "ESP32 Light Controller");
homekit_characteristic_t serial_number = HOMEKIT_CHARACTERISTIC_(SERIAL_NUMBER, "0123456");
homekit_characteristic_t manufacturer = HOMEKIT_CHARACTERISTIC_(MANUFACTURER, "Custom");
homekit_characteristic_t model = HOMEKIT_CHARACTERISTIC_(MODEL, "ESP32-Light");
homekit_characteristic_t firmware_revision = HOMEKIT_CHARACTERISTIC_(FIRMWARE_REVISION, "1.0");
// Accessory identification function
void accessory_identify(homekit_value_t _value) {
printf("Accessory identified\n");
}
// Define the accessory and its services
homekit_accessory_t *accessories[] = {
HOMEKIT_ACCESSORY(
.id = 1,
.category = homekit_accessory_category_switch,
.services = (homekit_service_t *[]){
HOMEKIT_SERVICE(
ACCESSORY_INFORMATION,
.characteristics = (homekit_characteristic_t *[]){
&name,
&manufacturer,
&serial_number,
&model,
&firmware_revision,
HOMEKIT_CHARACTERISTIC(IDENTIFY, accessory_identify),
NULL }),
HOMEKIT_SERVICE(SWITCH, .primary = true, .characteristics = (homekit_characteristic_t *[]){ HOMEKIT_CHARACTERISTIC(NAME, "Switch 1"), &cha_switch_on, NULL }), HOMEKIT_SERVICE(SWITCH, .characteristics = (homekit_characteristic_t *[]){ HOMEKIT_CHARACTERISTIC(NAME, "Switch 2"), &cha_switch_on2, NULL }), HOMEKIT_SERVICE(SWITCH, .characteristics = (homekit_characteristic_t *[]){ HOMEKIT_CHARACTERISTIC(NAME, "Switch 3"), &cha_switch_on3, NULL }), HOMEKIT_SERVICE(SWITCH, .characteristics = (homekit_characteristic_t *[]){ HOMEKIT_CHARACTERISTIC(NAME, "Switch 4"), &cha_switch_on4, NULL }), NULL }),
NULL
};
// Configuration for the accessory
homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111" // Replace with your desired pairing code
};
wifi_info.h
/*
* wifi_info.h
*
* Manages WiFi connection setup for the ESP32
*/
#ifndef WIFI_INFO_H_
#define WIFI_INFO_H_
#include <WiFi.h>
// Replace these with your WiFi credentials
const char *ssid = "Airtel_Zerotouch";
const char *password = "Airtel@123";
void wifi_connect() {
WiFi.mode(WIFI_STA); // Set the ESP32 to station mode
WiFi.setAutoReconnect(true); // Enable auto-reconnect
WiFi.begin(ssid, password); // Connect to WiFi
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500); // Wait for connection
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // Print the local IP address
}
#endif /* WIFI_INFO_H_ */