Hello everyone
I have an esp32 c3 supermini with a wall switch setup via binary sensor and it is work fine to control a garden water pump (the gard water pump has a seperate esp and relay)
im using mqtt to send the wall switch command from esp32 c3 to the esp on the water pump and everyhing is fine
now I want to use a custom homekit firmware on esp8266 to connect it via uart with the esp32 c3 since those two are on different networks
Im using this library for the esp8266
this is the code for esp32 c3
esphome:
name: mam
friendly_name: MAM
esp32:
board: esp32-c3-devkitm-1
framework:
type: arduino
# Enable Home Assistant API
api:
encryption:
key: "="
ota:
- platform: esphome
password: ""
wifi:
ssid: ""
password: ""
manual_ip:
static_ip: 192.168.100.93
gateway: 192.168.100.1
subnet: 255.255.255.0
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Mam Fallback Hotspot"
password: ""
captive_portal:
web_server:
port: 80
uart:
id: uart_bus
tx_pin: GPIO21 # TX to ESP8266 RX
rx_pin: GPIO20 # RX to ESP8266 TX
baud_rate: 9600
logger:
level: DEBUG
# Diagnostics: WiFi signal strength
sensor:
- platform: wifi_signal
name: "WiFi Signal Strength (dB)"
id: wifi_signal_db
update_interval: 60s
entity_category: "diagnostic"
- platform: copy
source_id: wifi_signal_db
name: "WiFi Signal Strength (%)"
unit_of_measurement: "%"
filters:
- lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
entity_category: "diagnostic"
- platform: dht
pin: GPIO3
temperature:
name: "Mam Kitchen Temprature"
filters:
- calibrate_linear:
- 30 -> 21
- 100 -> 66
humidity:
name: "Mam Kitchen Humidity"
update_interval: 2s
switch:
- platform: restart
name: "MAM Restart esp32c3 supermini"
- platform: gpio
pin: GPIO8
name: "GPIO8 LED"
id: led
inverted: true
globals:
- id: pump_state
type: bool
restore_value: yes
initial_value: 'false'
# For use with 3.3V input from ESP32-S2's 3.3V pin
binary_sensor:
- platform: gpio
pin:
number: GPIO5
mode: INPUT_PULLUP
inverted: true
name: "Garden Pump Wall Switch"
id: wall_switch
filters:
- delayed_on: 500ms # Increased from 50ms
- delayed_off: 500ms # Increased from 50ms
on_state:
- if:
condition:
binary_sensor.is_on: wall_switch
then:
- mqtt.publish:
topic: 'gar/switch/garden_pump/command'
payload: 'ON'
retain: false
- uart.write:
id: uart_bus
data: "PUMP_ON\n"
- if:
condition:
binary_sensor.is_off: wall_switch
then:
- mqtt.publish:
topic: 'gar/switch/garden_pump/command'
payload: 'OFF'
retain: false
- uart.write:
id: uart_bus
data: "PUMP_OFF\n"
- platform: gpio
pin:
number: GPIO6
mode: INPUT_PULLUP
inverted: true
name: "Tank Pump Wall Switch"
id: wall_switch2
filters:
- delayed_on: 500ms # Increased from 50ms
- delayed_off: 500ms # Increased from 50ms
on_state:
- if:
condition:
binary_sensor.is_on: wall_switch2
then:
- mqtt.publish:
topic: 'gar/switch/garden_pump/command'
payload: 'ON'
retain: false
- if:
condition:
binary_sensor.is_off: wall_switch2
then:
- mqtt.publish:
topic: 'gar/switch/garden_pump/command'
payload: 'OFF'
retain: false
mqtt:
broker: ""
username: ""
password: ""
discovery: true
discovery_prefix: homeassistant
this is the code for the main file of the homekit setup
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include "wifi_info.h"
#include "my_accessory.h" // Include the header file
#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
//==============================
extern "C" homekit_server_config_t config;
void my_homekit_setup() {
arduino_homekit_setup(&config);
}
static uint32_t next_heap_millis = 0;
static uint32_t next_report_millis = 0;
void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if (t > next_report_millis) {
// report sensor values every 10 seconds
next_report_millis = t + 10 * 1000;
my_homekit_report();
}
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());
}
}
void my_homekit_report() {
// FIXME, read your real sensors here.
float t = random_value(10, 30);
float h = random_value(30, 70);
float l = random_value(1, 10000);
uint8_t c = random_value(0, 10) < 5 ? 1 : 0; // 1 = detected, 0 = not detected
bool m = random_value(0, 10) < 5; // Motion detected is a boolean value
uint8_t o = random_value(0, 10) < 5 ? 1 : 0; // 1 = occupancy detected, 0 = not detected
cha_temperature.value.float_value = t;
homekit_characteristic_notify(&cha_temperature, cha_temperature.value);
cha_humidity.value.float_value = h;
homekit_characteristic_notify(&cha_humidity, cha_humidity.value);
cha_light.value.float_value = l;
homekit_characteristic_notify(&cha_light, cha_light.value);
cha_contact.value.uint8_value = c;
homekit_characteristic_notify(&cha_contact, cha_contact.value);
cha_motion.value.bool_value = m;
homekit_characteristic_notify(&cha_motion, cha_motion.value);
cha_occupancy.value.uint8_value = o;
homekit_characteristic_notify(&cha_occupancy, cha_occupancy.value);
LOG_D("t %.1f, h %.1f, l %.1f, c %u, m %u, o %u", t, h, l, c, (uint8_t)m, o);
}
int random_value(int min, int max) {
return min + random(max - min);
}
and this is the code for the accessory file of the homekit setup
#include <homekit/homekit.h>
#include <homekit/characteristics.h>
#include <Arduino.h> // Make sure we can use Serial communication
#include "my_accessory.h"
void my_accessory_identify(homekit_value_t _value) {
printf("accessory identify\n");
}
// Define the characteristics (no need for extern "C" here)
homekit_characteristic_t cha_temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 1);
homekit_characteristic_t cha_humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 1);
homekit_characteristic_t cha_light = HOMEKIT_CHARACTERISTIC_(CURRENT_AMBIENT_LIGHT_LEVEL, 1);
homekit_characteristic_t cha_contact = HOMEKIT_CHARACTERISTIC_(CONTACT_SENSOR_STATE, 0);
homekit_characteristic_t cha_motion = HOMEKIT_CHARACTERISTIC_(MOTION_DETECTED, false);
homekit_characteristic_t cha_occupancy = HOMEKIT_CHARACTERISTIC_(OCCUPANCY_DETECTED, 0);
// New Pump Switch characteristic
homekit_characteristic_t cha_pump_switch_on = HOMEKIT_CHARACTERISTIC_(ON, false);
homekit_accessory_t *accessories[] = {
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_bridge, .services=(homekit_service_t*[]) {
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
HOMEKIT_CHARACTERISTIC(NAME, "Multiple Sensors"),
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
NULL
}),
NULL
}),
HOMEKIT_ACCESSORY(.id=8, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) {
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
HOMEKIT_CHARACTERISTIC(NAME, "Garden Pump Switch"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
NULL
}),
HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
&cha_pump_switch_on,
NULL
}),
NULL
}),
NULL
};
homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111"
};
Im really noob when it comes to aurduino and I wonder if this setup is possible or not I couldnt manage to have any result just had the switch appear from the homekit but id did nothing
the connection is rx to tx and vise versa for the uart
I would appreciate any input
Thanks