DIY Zigbee light for USD 6 and coded in Arduino IDE for Zigbee2MQTT and Home Assistant

I took a couple of hours and a couple of :beer: 's and mocked up a DIY Zigbee light using the new M5Stack NanoC6 chip. You are able to code a complete Zigbee device using only the Arduino IDE. No need to have to used the more complex Platform I/O or Expressif IDE’s.

The ESP32-C6 chip is a very interesting device, in that you can use it to create a WiFi 6 device, Zigbee device or Matter device, by only uploading different software to the chip.

M5Stack does an excellent job of both hardware and software documentation for their products. And at an amazing price point of six dollars U.S.

Link to the steps I did to get this up and running is below. I still have much learning to do, however this short first pass shows the power of this ESP32-C6 chip and might give you a start to some interesting home automation ideas.

5 Likes

Another ESP32-C6 option to try, this one is sub USD 6 and has external antenna connection (or internal via software selection) and more pins broken out. Also looks to have some good documentation (2 deep sleep examples, how yet to see how these interact with Zigbee radio stack).

1 Like

Spotted that WeAct ESP32-C6-Mini ESP32-C6 development board can now be found for less than $4

https://www.aliexpress.com/item/1005006800070921.html

It is a clone of the official ESP32-C6-DevKitC-1 development kit ao can use that documentation from Espressif:

My friend - I want to try this.
But step 5.>

  1. Follow the steps to configure the Arduino IDE for a Zigbee project at this URL:

https://github.com/espressif/arduino-esp32/tree/master/libraries/ESP32/examples/Zigbee/Zigbee_Light_Bulb

Is 404ed.
What happens in that step?

I’ve not revisited this code in some time, apologies for not being much help.

It’s looks like they made an update to this repository recently and the examples are missing for Zigbee. If you do a search within the repository, the older code is still there (power of github versions :wink: ). You might post a issue in the repository that they ‘86ed’ the examples. I note they have a whole bunch of new contributors to the repository. That said, I made a copy of this specific example to my repository, so you really do not need the original example, I had to make some changes to this code to get it work with the C6 and Zigbee2MQTT, hence why I made a copy.

All that said, my original modded code quit working a while back as noted by this issue (link below) posted to my repository. The author posted some fixes to make the code work with the newer revisions of the library, have a look at those to see if you can make progress. Good hunting!

Thank you. No worries, I got some stuff kludged together :slight_smile:

if future people care - this works ok with z2m:

#include "Zigbee.h"
#include <Adafruit_NeoPixel.h>

/* Zigbee color dimmable light configuration */
#define ZIGBEE_RGB_LIGHT_ENDPOINT 10
#define LED_PIN    20
#define ENABLE_PIN 19
#define NUM_LEDS   1

Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
uint8_t button = BOOT_PIN;

ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_RGB_LIGHT_ENDPOINT);

/********************* RGB LED functions **************************/
void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) {
    if (!state) {
        strip.setPixelColor(0, 0, 0, 0); // Turn LED off
        strip.show();
        return;
    }
    float brightness = (float)level / 255;
    uint8_t bright_red = red * brightness;
    uint8_t bright_green = green * brightness;
    uint8_t bright_blue = blue * brightness;
    
    strip.setPixelColor(0, bright_red, bright_green, bright_blue);
    strip.show();
}

// Create a task on identify call to handle the identify function
void identify(uint16_t time) {
    static uint8_t blink = 1;
    if (time == 0) {
        // If identify time is 0, stop blinking and restore light as it was used for identify
        zbColorLight.restoreLight();
        return;
    }
    strip.setPixelColor(0, 255 * blink, 255 * blink, 255 * blink);
    strip.show();
    blink = !blink;
}

/********************* Arduino functions **************************/
void setup() {
    Serial.begin(115200);

    // Initialize NeoPixel library
    strip.begin();
    strip.setPixelColor(0, 0, 0, 0); // Turn LED off initially
    strip.show();

    // Init button for factory reset
    pinMode(button, INPUT_PULLUP);

    //cam extras
    pinMode(ENABLE_PIN, OUTPUT);
    digitalWrite(ENABLE_PIN, HIGH);

    // Set callback function for light change
    zbColorLight.onLightChange(setRGBLight);

    // Optional: Set callback function for device identify
    zbColorLight.onIdentify(identify);

    // Optional: Set Zigbee device name and model
    zbColorLight.setManufacturerAndModel("Espressif", "ZBColorLightBulb");

    // Add endpoint to Zigbee Core
    Serial.println("Adding ZigbeeLight endpoint to Zigbee Core");
    Zigbee.addEndpoint(&zbColorLight);

    // When all EPs are registered, start Zigbee in End Device mode
    if (!Zigbee.begin()) {
        Serial.println("Zigbee failed to start!");
        Serial.println("Rebooting...");
        ESP.restart();
    }

    //blink the light to make sure its the right pin
    strip.setPixelColor(0, 0, 255, 0); // Set color to green
    strip.show();
    delay(200);
    strip.setPixelColor(0, 0, 0, 0); // Turn LED off
    strip.show();
    delay(200);
    strip.setPixelColor(0, 0, 255, 0); // Set color to green
    strip.show();
    delay(200);
    strip.setPixelColor(0, 0, 0, 0); // Turn LED off
    strip.show();
    delay(200);

    Serial.println("Connecting to network");
    while (!Zigbee.connected()) {
        Serial.print(".");
        delay(100);
    }
    Serial.println();
    Serial.println("Connected to Zigbee Network!");
}

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();
            }
        }
        // Increase brightness by 50 every time the button is pressed
        zbColorLight.setLightLevel(zbColorLight.getLightLevel() + 50);
    }
    delay(100);
}
1 Like

Look at my github I have big playground with arduino 3.3 for zigbee and special things and working converter for z2m. My gh: dbn-b4e

1 Like