ESP8266 programmed with Arduino controlls RGB LED strip.
Few libraries needs to be imported into Arduino IDE:
- ESP8266
- Adafruit_NoePixel
- Espalexa
Hope it goes without saying but I do not own any of this libraries and all the credits belongs to their authors.
/*
* This is a basic example on how to use Espalexa with RGB color devices.
*/
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#define ESPALEXA_ASYNC
#include <Espalexa.h>
#include "Adafruit_NeoPixel.h"
#define PIN 12 //D6
#define EXT_LED 16 //D0
#define NUMPIXELS 107
// prototypes
boolean connectWifi();
//callback function prototype
void colorLightChanged(uint8_t brightness, uint32_t rgb);
// Change this!! – this is an obvious flaw of this solution,
//if you change your wifi, you need to reprogram this.
//More elegant solution in the future would be to add small SD card
//with the text file with wifi settings or add a bluetooth setup or
//default IP and over the web server setup
const char* ssid = "xxxxx";
const char* password = "xxxxx";
boolean wifiConnected = false;
Espalexa espalexa;
//Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
//Initiate NeoPixel library
pixels.begin();
//initialize Neo Pixels
pixels.clear(); // Set all pixel colors to ‘off’
for (int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(255, 255, 255, 255));
}
delay(500);
pixels.show(); // Send the updated pixel colors to the hardware.
Serial.begin(115200);
// Initialise wifi connection
wifiConnected = connectWifi();
if(wifiConnected){
espalexa.addDevice("Cabinets Light 3", colorLightChanged);
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) {
uint8_t r, g, b;
r = (rgb >> 16) & 0xFF;
g = (rgb >> 8) & 0xFF;
b = rgb & 0xFF;
//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
Serial.println("##############################################");
Serial.println("R:"+String(r)+" G:"+String(g)+ " B:"+String(b));
if (brightness == 0) {
pixels.clear();
pixels.show();
} else {
if (r == 0 && g == 0 && b==0) // when Alexa sends "WHITE" it sends 0,0,0 with 100% brightness
{
r = 255;
g = 255;
b = 255;
}
r = r * brightness/255;
g = g * brightness/255;
b = b * brightness/255;
// for (int i=0; i<NUMPIXELS; i++) {
// //pixels.setPixelColor(i, pixels.Color(r, g, b));
// pixels.setPixelColor(i, r, g, b, 255);
// }
pixels.fill(pixels.Color(r, g, b), 0, NUMPIXELS);
//pixels.setBrightness(brightness);
pixels.show(); // Send the updated pixel colors to the hardware.
Serial.println("_____________________________________________");
Serial.println("R:"+String(r)+" G:"+String(g)+ " B:"+String(b));
}
}
// 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;
}