Today I will show you how easy you can control an Programmable RGB LED Strip WS2811 using Amazon Echo Alexa (Gen1 and Gen2) and NodeMCU ESP8266. You just need a few parts.
Things used in this project
Hardware components
- One Amazon Echo Alexa (I used an Gen2)
- One Wemos D1 Mini ESP8266 module
- One WS2812 5050 RGB LED Strip
- 5V 2A power supply
- Some connection wires
Software components (Download all the required software)
- Arduino IDE
- ESP 8266 Library installed (version 2.3)
- FauxmoESP Library – you can download the latest code from https://bitbucket.org/xoseperez/fauxmoesp
- ESPAsyncTCP if you use ESP8266 or AsyncTCP if you use ESP32
- Adafruit NeoPixel library from https://github.com/adafruit/Adafruit_NeoPixel
Lets get started
Step 1 – Install Library
If you are using the ESP devices for the first time, paste the following link in the preferences (to open preferences go to File -> Preferences) as shown below: http://arduino.esp8266.com/stable/package_esp8266com_index.json
Next, ESP boards must be installed. This can be done very easy from the board manager (to open board manager, go to Tools -> Board -> Boards Manager..) as shown below:
Next, the required libraries must be downloaded and added to the Arduino IDE. The libraries was downloaded at previews step.
To apply you can unzip them and copy paste to D:\Program Files (x86)\Arduino\libraries (for example the path for ESPAsyncTCP should be D:\Program Files (x86)\Arduino\libraries\ESPAsyncTCP) or you can add all the zips from Arduino IDE by going to Sketch -> Include Library -> Add Zip Library… and select all downloaded zips.
After that all the software is successfully installed we are now ready to look on code.
Step 2 – Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
#include <Arduino.h> #ifdef ESP32 #include <WiFi.h> #else #include <ESP8266WiFi.h> #endif #include "fauxmoESP.h" #include <Adafruit_NeoPixel.h> #define WIFI_SSID ".........." //TODO: Set your ssid #define WIFI_PASS ".........." //TODO: Set your password #define SERIAL_BAUDRATE 115200 #define LED D1 //TODO: Change pins according to your ESP pinouts fauxmoESP fauxmo; int mode; int numPixels = 32; // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, LED, NEO_GRB + NEO_KHZ800); // ----------------------------------------------------------------------------- // Wifi // ----------------------------------------------------------------------------- void wifiSetup() { // Set WIFI module to STA mode WiFi.mode(WIFI_STA); // Connect Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASS); // Wait while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println(); // Connected! Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); } void setup() { // Init serial port and clean garbage Serial.begin(SERIAL_BAUDRATE); Serial.println(); Serial.println(); // Wifi wifiSetup(); // LED pinMode(LED, OUTPUT); digitalWrite(LED, LOW); // You have to call enable(true) once you have a WiFi connection // You can enable or disable the library at any moment // Disabling it will prevent the devices from being discovered and switched fauxmo.enable(true); // Add virtual devices // Device Names for RGB Patterns // TODO: change below names as you want fauxmo.addDevice("GREEN LED"); fauxmo.addDevice("RED LED"); fauxmo.addDevice("BLUE LED"); fauxmo.addDevice("RAINBOW LED"); strip.begin(); strip.show(); strip.setBrightness(5); i = 0; // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, // this way it's easier to match devices to action without having to compare strings. fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state) { Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF"); if (device_id == 0) { if (state) i = 1; else i = 0; } if (device_id == 1) { if (state) i = 2; else i = 0; } if (device_id == 2) { if (state) i = 3; else i = 0; } if (device_id == 3) { if (state) i = 4; else i = 0; } }); // Callback to retrieve current state (for GetBinaryState queries) fauxmo.onGetState([](unsigned char device_id, const char * device_name) { if (device_id == 0) return i == 1 ? 1 ? 0; if (device_id == 1) return !i == 2 ? 1 ? 0; if (device_id == 2) return i == 3 ? 1 ? 0; if (device_id == 3) return i == 4 ? 1 ? 0; }); } void loop() { // Since fauxmoESP 2.0 the library uses the "compatibility" mode by // default, this means that it uses WiFiUdp class instead of AsyncUDP. // The later requires the Arduino Core for ESP8266 staging version // whilst the former works fine with current stable 2.3.0 version. // But, since it's not "async" anymore we have to manually poll for UDP // packets fauxmo.handle(); startLeds(); } void startShow() { switch (i) { case 0: setColor(strip.Color(0, 0, 0)); // Black/Off break; case 1: setColor(strip.Color(0, 255, 0)); // Green break; case 2: setColor(strip.Color(255, 0, 0)); // Red break; case 3: setColor(strip.Color(0, 0, 255)); // Blue break; case 4: rainbow(); break; } } // Fill the dots one after the other with a color void setColor(uint32_t c) { for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(50); } } void rainbow() { uint16_t i, j; for(j = 0; j < 256; j++) { for(i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel((i * 1 + j) & 255)); } strip.show(); delay(20); } } // Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(byte WheelPos) { if(WheelPos < 85) { return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } } |
Step 3 – Connect Hardware
Connect D5 Pin with Programmable RGB LED Strip WS2811 input pin.
Connect VCC and GND from your power supply to Programmable RGB LED Strip WS2811 VCC and GND pin.
Connect VCC and GND pin from your power supply to your ESP8266
You can check it out on GitHub