#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
#include <Adafruit_NeoPixel.h>
// --- Wi-Fi Configuration ---
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// --- NeoPixel Configuration ---
#define LED_PIN 4 // GPIO4 (D2 on NodeMCU/D1 Mini; GPIO4 on ESP32)
#define NUM_LEDS 30 // Total number of NeoPixels in your string
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
Espalexa espalexa;
// Forward declaration of the callback function
void onColorLightChanged(uint8_t brightness, uint32_t rgb);
void setup() {
Serial.begin(115200);
delay(500);
// Initialize NeoPixels
strip.begin();
strip.setBrightness(0);
strip.show(); // Turn all off at boot
// Connect to 2.4 GHz Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Add the device to Espalexa
// Passing a callback with (brightness, rgb) registers it as a full-color device
espalexa.addDevice("LED Strip", onColorLightChanged);
espalexa.begin();
Serial.println("Espalexa started. Ask Alexa to discover devices!");
}
void loop() {
espalexa.loop();
delay(1); // Keeps the network watchdog happy
}
// Callback triggered whenever Alexa sends a command
void onColorLightChanged(uint8_t brightness, uint32_t rgb) {
// Extract RGB components from the 24-bit color value
uint8_t r = (rgb >> 16) & 0xFF;
uint8_t g = (rgb >> 8) & 0xFF;
uint8_t b = rgb & 0xFF;
Serial.printf("Command received - Bri: %d | R: %d, G: %d, B: %d\n", brightness, r, g, b);
if (brightness == 0) {
// Strip turned off
strip.clear();
strip.show();
} else {
// If turning on with default white (when Alexa sends 0,0,0 at non-zero brightness)
if (r == 0 && g == 0 && b == 0) {
r = 255;
g = 255;
b = 255;
}
// Set brightness and apply color to all pixels
strip.setBrightness(brightness);
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
}
strip.show();
}
}
/*---------------------------------------------------*/
Wiring Guide
VCC (+5V): For more than 8–10 LEDs, run power directly from an external 5V power adapter (calculate ~60 mA per LED at full white). Connect the ESP's VIN or 5V pin to this supply as well.
GND: Connect the external power supply GND, the NeoPixel string GND, and the ESP board GND together (common ground is mandatory).
DIN (Data In): Connect to LED_PIN (GPIO 4) through a 330Ω to 470Ω resistor to prevent voltage spikes.
Setup in the Alexa App
Ensure your ESP board and your Amazon Echo (or Alexa app device) are connected to the same 2.4 GHz local Wi-Fi subnet.
Open the Amazon Alexa App and navigate to Devices > Add Device > Light > Other, or say:
"Alexa, discover devices."
Alexa will discover "LED Strip" as a Philips Hue color bulb.
Test voice commands such as:
"Alexa, turn on LED Strip"
"Alexa, set LED Strip to Blue"
"Alexa, dim LED Strip to 30%"
/*----------------------------------------------------------------------------------------*/
No comments:
Post a Comment