Hello everyone! 🤗
First of all, I want to extend a huge thank you for the fantastic response to my previous post about the DIY temperature and humidity meter! Your feedback has been incredibly inspiring, and it motivates me to continue exploring exciting projects.
This time, I'm embarking on a new project using the XIAO ESP32C6 and a relay to control a single-color LED strip! 💡
I wanted to integrate a relay with HomeAssistant to control my LED strip easily. Recently, I've been diving into the Zigbee capabilities of the ESP32C6, and I’ve found its low power consumption and ease of connectivity to be perfect for my needs. Using Zigbee allows me to seamlessly integrate the relay into HomeAssistant, enabling me to turn the LED strip on and off as needed.
Project GoalsMy aim is to create a simple yet effective system that allows me to manage the lighting with just a few clicks in HomeAssistant. The relay serves as a switch for the LED strip, providing a straightforward way to control its power supply. However, the beauty of the relay is that it can be utilized in various unexpected applications—so let your creativity run wild!
What I Used● XIAO ESP32C6: The core of this project, responsible for Zigbee communication and relay control.
● Grove-Relay Module: This will turn the LED strip on and off, serving as a switch for various devices.
● XIAO Expansion Board: Made it easier to connect everything together.
● HomeAssistant: : The platform that allows for smart home control.
● 3D Printed Shell: I designed a custom enclosure with a trapezoidal shape to accommodate the unique profile of the relay, resembling either a whale or a boot. This design not only houses all the components neatly but also enhances the overall aesthetic. Plus, it remains super compact, measuring just 60x30x40mm, making it an elegant and tidy addition to any room.
○ Top Cover: The top cover features openings for the screw terminals and wiring.
○ Bottom Cover: The bottom cover has a slot for the Type-C port and eliminates screw holes, allowing for screw-free installation.
○ Middle Connector: The component securely connects and holds the expansion board and sensor module in place, requiring four screws to secure them together.
Before using it to connect to HomeAssistant, please confirm if your HomeAssistant is equipped with peripherals that receive Zigbee signals, such as: Home Assistant Connect ZBT-1.
So, here's what this small device looks like😎:
1. Connecting the Relay: I'll connect the relay module to the XIAO ESP32C6, so it can control the power to the single-color LED strip.
2. Zigbee Integration: The ESP32C6 will be connected wirelessly to HomeAssistant via Zigbee, enabling easy control.
3. Programming: I’ll write the firmware to ensure that the ESP32C6 can receive commands from HomeAssistant and actuate the relay accordingly.
Next, I will show you the actual effect: I am using a portable CNC power supply to power a 5V monochrome LED strip, and then I use the Zigbee relay module I created as a switch. When I turn on the relay switch in Home Assistant, the LED strip lights up. When I turn off the switch, the LED strip turns off.
In the Home Assistant interface, we just need to click the Light button to control the relay switch and therefore control the on and off of the lights!
Here’s the code I used for the project. This code is modified from the Zigbee example in Arduino-ESP32:
#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif
#include "Zigbee.h"
/* Zigbee light bulb configuration */
#define ZIGBEE_LIGHT_ENDPOINT 10
#define RELAY_PIN D0
uint8_t relay = RELAY_PIN;
uint8_t led = LED_BUILTIN;
uint8_t button = BOOT_PIN;
ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
/********************* Relay functions **************************/
void setRelay(bool value) {
digitalWrite(led, value);
digitalWrite(relay, value);
}
/********************* Arduino functions **************************/
void setup() {
Serial.begin(115200);
// Init RF
pinMode(WIFI_ENABLE, OUTPUT); // pinMode(3, OUTPUT);
digitalWrite(WIFI_ENABLE, LOW); // digitalWrite(3, LOW); // Activate RF switch control
delay(100);
pinMode(WIFI_ANT_CONFIG, OUTPUT); // pinMode(14, OUTPUT);
digitalWrite(WIFI_ANT_CONFIG, LOW); // digitalWrite(14, HIGH); // Use external antenna
// Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood)
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
// Init button for factory reset
pinMode(button, INPUT_PULLUP);
//Optional: set Zigbee device name and model
zbLight.setManufacturerAndModel("Espressif", "ZBRelay");
// Set callback function for light change
zbLight.onLightChange(setRelay);
//Add endpoint to Zigbee Core
Serial.println("Adding ZigbeeLight endpoint to Zigbee Core");
Zigbee.addEndpoint(&zbLight);
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
if (!Zigbee.begin()) {
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
}
Serial.println("Connecting to network");
while (!Zigbee.connected()) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.println("Connect to network Success!");
}
void loop() {
// Checking button for factory reset
if (digitalRead(button) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(button) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
delay(1000);
Zigbee.factoryReset();
}
}
// Toggle light by pressing the button
zbLight.setLight(!zbLight.getLightState());
}
delay(100);
}
Future PlansI’m excited to think about the potential applications beyond just controlling the LED strip. The relay can be employed in many creative ways within a smart home setup, whether it's for lights, fans, or other appliances!
I can't wait to share progress on this new project with all of you! If you have any suggestions or experiences to share, please let me know in the comments! 🙌
Thank you all for your continued support! Let’s keep exploring more DIY smart home projects together!
Comments
Please log in or sign up to comment.