DIY Alexa controlled RGB Smart bulb using ESP8266

DIY Alexa controlled RGB Smart bulb using ESP8266 DIY Alexa controlled RGB Smart bulb using ESP8266

In this Project you will learn how to build an Amazon Alexa controlled RGB Smart LED bulb by interfacing ESP8266 node MCU with WS2812b Neopixel LCD strip. This is a simple ESP8266 project the connections are simple and you can build the smart bulb with in few minutes.

Now a days due to the smart home revolution, people are moving towards smart IoT products which can ease the day today activity. There are wide variety of smart products like smart door locks, smart CCTV cameras, Smart switches, Smart temperature controlled fans, Smart Refrigerators, Smart lights and everything is adopted to smart features with Internet connectivity to monitor and control them from smart phones and AI assistants.

Taking this into consideration lets build a cheap DIY smart RGB LED bulb to beat the markets costly smart bulbs.

Related project: DIY Smart LED strip with Sound reactive effects using ESP8266 or ESP32

Required components:

Here are the required components along with Amazon buying links at best prices.
Product NameQuantityamazon logoamazon logo india
ESP8266 Node MCU or D1 Mini1https://amzn.to/3tqMwkBhttps://amzn.to/3sVdSjG
WS2812b LED stripYour Choicehttps://amzn.to/3KlXEGGhttps://amzn.to/3v8jK9l
220v AC to 5v DC converter1https://amzn.to/3NZ5HeFhttps://amzn.to/3vc85qa
Micro USB cable1https://amzn.to/3s1a8g3https://amzn.to/364yInH

WS2812b Pinout Diagram:

ws2812b pinout diagram

As you see from the above pinout diagram the WS2812b LED strip has 3 pins, 2 pins for power supply 5v and GND and the middle pin is for data. Every LED has a chip which stores the data of the color which it has to show and the data of the next LEDs. The data flows from the starting to the ending LEDs, make sure to connect the data pin from the microcontroller from the beginning arrow printed on the LED strip.

For more details about Neopixel LEDs refer : How NeoPixel LED works in detail

Circuit Diagram:

Connect all the required components according to the below circuit diagram.

interfacing nodemcu with WS2812b circuit diagram

To reduce the space and cost you can also use D1 mini microcontroller instead of Node MCU.

As you can see from the above image we powered Node MCU and LED strip from the HiLink 100-240v AC supply to 5V DC converter. If its not available you can also use any mobile charger circuit to power it.

As we are using only 6 LEDs Strip, each LED consumes 50mA at maximum brightness so 6 consumes nearly 300mA you can choose the number of LEDs according to the requirement. So choose a minimum of 5v 0.5A converter for better stability.

VIN and GND pins of Node MCU are connected to +ve and -ve pins of Converter supply respectively.

+5v and GND pins of LED strip are connected to +ve and -ve pins of Converter supply respectively. Din pin of LED strip is connected to digital pin D3 of Node MCU. A resistor of 330 ohms is connected between data pin to protect the LED strip first LED from voltage fluctuations which may cause damage to it.

Program code:

Connect the NodeMCU to the PC where Arduino IDE is installed through USB cable. For safely don’t connect the AC power supply while uploading the code. Choose the correct port and board as Node MCU V1 from Tools menu and install the required libraries from the Library manager of download from the below links and extract them in Arduino Libraries folder.

Required Libraries:

  • Download FastLED library : Link
  • Download ESP8266WiFi library: Link
  • Download Espalexa.h library: Link

Next copy the below code and paste it in IDE workspace change the WiFi credentials and press upload button. That’s it the code will be uploaded.

You can also implement this :Change WiFi Credentials of ESP8266 without uploading code again

// CircuitSchools www.circuitschools.com
// ESP8266 Alexa Controlled RGB Smart Bulb

#define FASTLED_ESP8266_RAW_PIN_ORDER
#include "FastLED.h"  //Include this Library
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
#define PIN1          D3    //LED Pin
#define NUM_LEDS1      6     //Enter number of LEDs
CRGB leds1[NUM_LEDS1];

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#define ESPALEXA_A  1SYNC
#include <Espalexa.h>

int data=255;
int r,g,b;

// prototypes
boolean connectWifi();

//callback function prototype
void colorLightChanged(uint8_t brightness, uint32_t rgb);

// Change this!!
const char* ssid = "circuitschools";      //Enter WiFi SSID
const char* password = "visitus";     //Enter WiFi Password

boolean wifiConnected = false;

Espalexa espalexa;

void setup()
{
  Serial.begin(115200);
  FastLED.addLeds<LED_TYPE, PIN1, COLOR_ORDER>(leds1, NUM_LEDS1).setCorrection( TypicalLEDStrip );
  // Initialise wifi connection
  wifiConnected = connectWifi();
  if(wifiConnected){
    espalexa.addDevice("SmartBulb", colorLightChanged);   //you can change the Device Name
    espalexa.begin();   
  } else
  {
    while (1) {
      Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
      delay(2500);
    }
  }
}
 
void loop()
{
   espalexa.loop();
   delay(1);
}

//the color device callback function has two parameters
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
  //do what you need to do here, for example control RGB LED strip
  Serial.print("Brightness: ");
  Serial.print(brightness);
  Serial.print(", Red: ");
  Serial.print((rgb >> 16) & 0xFF); //get red component
  Serial.print(", Green: ");
  Serial.print((rgb >>  8) & 0xFF); //get green
  Serial.print(", Blue: ");
  Serial.println(rgb & 0xFF); //get blue
float hell = brightness / 255.0;
r=((rgb >> 16) & 0xFF)*hell;
g=((rgb >>  8) & 0xFF)*hell;
b=(rgb & 0xFF)*hell;
static1(r, g, b,data);
}

void static1(int r, int g, int b,int brightness)
{
  FastLED.setBrightness(brightness);
  for (int i = 0; i < NUM_LEDS1; i++ )
  {
    leds1[i] = CRGB(r, g, b);
  }
  FastLED.show();
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
  boolean state = true;
  int i = 0;
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 40){
      state = false; break;
    }
    i++;
  }
  Serial.println("");
  if (state){
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed.");
  }
  return state;
}

After uploading the code, Turn on the Alexa echo device which is connected to the same network.

The library aims to work with every Echo on the market, but there are a few things to look out for.

Espalexa only works with a genuine Echo speaker, it probably wont work with Echo emulators, Raspberry Pi homebrew devices or just the standalone mobile app.

Next open Amazon Alexa app on mobile, from bottom menu choose “More” and from that click on “Add a Device”. From the list of All devices choose ‘other’ option at the bottom. then click on “DISCOVER DEVICES” as shown in the steps screenshots below.

adding alexa smart bulb to alexa device through mobile app

After scanning you will find a light, so click on setup and choose the group and done. Now you can turn on/ off , change the brightness and even colors of the bulb.

alexa smart bulb setup and settings

Fixing the LED strip and components into bulb:

Take a normal LED bulb and remove the LEDs and drivers from it to make space for our smart LED, Connect the AC point of the bulb to Ac to 5v DC converter and place the LCD strip in such a way that it emits max brightness towards the diffuser. That’s it.

Errors and Solutions:

wl_definitions.h: No such file or directory

You will get this error if you use the latest version of the ESP8266WiFi library. Remove it and download it from the above links and extract it in libraries folder, the error will disappear.

8 comments
  1. I made this projet, works with Alexa but do not turn on more than 1(ONE) LED and the blue does not work.
    I change the strip with the same problem.
    In the serial monitor I can see the command been sent, but looks like the D3 pin do not send the blue part of the command.
    Can you help me?

    1. hi Jose,
      had you set the number of LEDs in this line #define NUM_LEDS1 6
      use the library versions we have given.
      You can use other pins D1 or D2 or D4 and also change the pin info in code.
      things to note:
      Is powersupply suffient for LEDs.
      Is Wiring is good.

  2. HEY, CAN YOU TELL ME WHY MY LED DOES NOT TURN OFF, WHEN I TRY ONE RED LED IS ALWAYS ON. I NEED HELP

  3. HI VERY NICE EXPLANATION BUT IM FACING PROBLRM.THERE IS ONLY 1ST LED GLOWING OUT OF 60 LED AND THE COLOR IS INAPPROPRIATE .THAT SINGLE LED WILL TURN RED WHEN ITS TURNED OF IN THE ALEXA APP.REPLY WOULD BE APPRECIATED.

  4. Add an extra:
    FastLED.show();

    after line 86, if you are seeing weird behaviour. Seems the underlying library may have a fault, but this fixes it, especially where it doesn’t give the right colours, only lights a single LED, doesn’t turn off, doesn’t do the right brightness, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *