Aircoookie / Espalexa

Alexa voice control for ESP8266/ESP32 (including brightness and color!)
MIT License
531 stars 135 forks source link

Little FastLED example: #31

Open ChristianKnorr opened 5 years ago

ChristianKnorr commented 5 years ago
/*
   This is a basic example on how to use Espalexa with FastLED LED Strip
   - first start: LED orange indicates: connect to me ;)
   - LED switches to green if youre connected and configured wifi and Espalexa in your wlan
   - then LED fades to black and its ready
   - otherwise LED will be switches to red and reset itselfes
*/
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Espalexa.h>

#include <FastLED.h>
#define DATA_PIN D4
//#define CLOCK_PIN ?
#define NUM_LEDS 15
CRGB leds[NUM_LEDS];

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

Espalexa espalexa;

void setup()
{
  // Uncomment/edit one of the following lines for your leds arrangement.
  // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  // FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  // FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);

  // FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<P9813, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<APA102, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<DOTSTAR, RGB>(leds, NUM_LEDS);

  // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);

  for ( int i = 0; i <= 30; i++ ) { // fade from black to orange
    fill_solid( leds, NUM_LEDS, CRGB(i,round( (i / 1,5) ),0));
    FastLED.show();
    delay(20);
  }
  Serial.begin(115200);
  Serial.println("connect to Espalexa and configure wifi");
  WiFiManager wifiManager;
  if (!wifiManager.autoConnect("Espalexa")) {
    fill_solid( leds, NUM_LEDS, CRGB(255,0,0)); // red
    FastLED.show();
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  } else {
    for ( int i = 0; i <= 30; i++ ) { // fade from orange to green
      fill_solid( leds, NUM_LEDS, CRGB((30-i),(20+i),0));
      FastLED.show();
      delay(33);
    }
    Serial.println("connected to WIFI..");
    espalexa_addDevices();
    espalexa.begin();
    delay(1000);
    FastLED.show();
    for ( int i = 50; i >= 0; i-- ) { // fade from green to black
      fill_solid( leds, NUM_LEDS, CRGB(0,i,0));
      FastLED.show();
      delay(20);
    }
  }
  Serial.println("Now it's time to ask alexa to discover devices...");
}

void loop()
{
  espalexa.loop();
  delay(1);
}

void espalexa_addDevices() { // add Devices you want
  espalexa.addDevice("Light", colorLightChanged);
  Serial.println("devices added");
}

//the color device callback function has two parameters
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
  float r = ((rgb >> 16) & 0xFF);
  float g = ((rgb >>  8) & 0xFF);
  float b = (rgb & 0xFF);
  int rd = round( r / 255 * brightness );
  int gd = round( g / 255 * brightness );
  int bd = round( b / 255 * brightness );
  //do what you need to do here, for example control RGB LED strip
  Serial.print("Brightness: ");
  Serial.print(brightness);
  Serial.print("\tRed: ");
  Serial.print(r); //get red component
  Serial.print("\tGreen: ");
  Serial.print(g); //get green
  Serial.print("\tBlue: ");
  Serial.print(b); //get blue
  Serial.print("\tDimmed Red: ");
  Serial.print(rd); //get red component
  Serial.print("\tGreen: ");
  Serial.print(gd); //get green
  Serial.print("\tBlue: ");
  Serial.println(bd); //get blue
  fill_solid( leds, NUM_LEDS, CRGB(rd,gd,bd));
  FastLED.show();
}
Aircoookie commented 5 years ago

Thank you for the nice example! :) By the way, you can do the ` symbol three times before and after your code, it will show it in the correct format.

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Espalexa.h>

#include <FastLED.h>
#define DATA_PIN D4
//#define CLOCK_PIN ?
#define NUM_LEDS 15
CRGB leds[NUM_LEDS];

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

Espalexa espalexa;

void setup()
{
// Uncomment/edit one of the following lines for your leds arrangement.
// FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
// FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);

// FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<P9813, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<APA102, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<DOTSTAR, RGB>(leds, NUM_LEDS);

// FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);

for ( int i = 0; i <= 30; i++ ) { // fade from black to orange
fill_solid( leds, NUM_LEDS, CRGB(i,round( (i / 1,5) ),0));
FastLED.show();
delay(20);
}
Serial.begin(115200);
Serial.println("connect to Espalexa and configure wifi");
WiFiManager wifiManager;
if (!wifiManager.autoConnect("Espalexa")) {
fill_solid( leds, NUM_LEDS, CRGB(255,0,0)); // red
FastLED.show();
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(1000);
} else {
for ( int i = 0; i <= 30; i++ ) { // fade from orange to green
fill_solid( leds, NUM_LEDS, CRGB((30-i),(20+i),0));
FastLED.show();
delay(33);
}
Serial.println("connected to WIFI..");
espalexa_addDevices();
espalexa.begin();
delay(1000);
FastLED.show();
for ( int i = 50; i >= 0; i-- ) { // fade from green to black
fill_solid( leds, NUM_LEDS, CRGB(0,i,0));
FastLED.show();
delay(20);
}
}
Serial.println("Now it's time to ask alexa to discover devices...");
}

void loop()
{
espalexa.loop();
delay(1);
}

void espalexa_addDevices() { // add Devices you want
espalexa.addDevice("Light", colorLightChanged);
Serial.println("devices added");
}

//the color device callback function has two parameters
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
float r = ((rgb >> 16) & 0xFF);
float g = ((rgb >> 8) & 0xFF);
float b = (rgb & 0xFF);
int rd = round( r / 255 * brightness );
int gd = round( g / 255 * brightness );
int bd = round( b / 255 * brightness );
//do what you need to do here, for example control RGB LED strip
Serial.print("Brightness: ");
Serial.print(brightness);
Serial.print("\tRed: ");
Serial.print(r); //get red component
Serial.print("\tGreen: ");
Serial.print(g); //get green
Serial.print("\tBlue: ");
Serial.print(b); //get blue
Serial.print("\tDimmed Red: ");
Serial.print(rd); //get red component
Serial.print("\tGreen: ");
Serial.print(gd); //get green
Serial.print("\tBlue: ");
Serial.println(bd); //get blue
fill_solid( leds, NUM_LEDS, CRGB(rd,gd,bd));
FastLED.show();
}
ChristianKnorr commented 5 years ago

Aha okay three times 😁 i only noticed the <> icon above which inserts one time `

spiderhobbit commented 5 years ago

Any idea why i'm getting only the first 2 led's in my strip light up? The first one is white and the second is green, it also doesn't respond to commands. Alexa discovers the device and i see the commands come up in serial monitor when i say "Alexa turn on light" etc.

Also when using the alexa app, i can turn the light on, but then if i try to change the brightness or colour, it turns the device off (in the app).

I'm using a 1m 30 led ws2812b strip with nodemcu amica v2 connecting to pin D4, 3v3 and gnd. Running fastled on its own lights up all led's.

ChristianKnorr commented 5 years ago

Any idea why i'm getting only the first 2 led's in my strip light up?

Poste your code

spiderhobbit commented 5 years ago
/*
   This is a basic example on how to use Espalexa with FastLED LED Strip
   - first start: LED orange indicates: connect to me ;)
   - LED switches to green if youre connected and configured wifi and Espalexa in your wlan
   - then LED fades to black and its ready
   - otherwise LED will be switches to red and reset itselfes
*/
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Espalexa.h>

#include <FastLED.h>
#define DATA_PIN D4
//#define CLOCK_PIN ?
#define NUM_LEDS 30
CRGB leds[NUM_LEDS];

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

Espalexa espalexa;

void setup()
{
  // Uncomment/edit one of the following lines for your leds arrangement.
  // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  // FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  // FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);

  // FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<P9813, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<APA102, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<DOTSTAR, RGB>(leds, NUM_LEDS);

  // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);

  for ( int i = 0; i <= 30; i++ ) { // fade from black to orange
    fill_solid( leds, NUM_LEDS, CRGB(i,round( (i / 1,5) ),0));
    FastLED.show();
    delay(20);
  }

  Serial.begin(115200);
  Serial.println("connect to Espalexa and configure wifi");
  WiFiManager wifiManager;
  if (!wifiManager.autoConnect("Espalexa")) {
    fill_solid( leds, NUM_LEDS, CRGB(255,0,0)); // red
    FastLED.show();
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  } else {
    for ( int i = 0; i <= 30; i++ ) { // fade from orange to green
      fill_solid( leds, NUM_LEDS, CRGB((30-i),(20+i),0));
      FastLED.show();
      delay(33);
    }
    Serial.println("connected to WIFI..");
    espalexa_addDevices();
    espalexa.begin();
    delay(1000);
    FastLED.show();
    for ( int i = 50; i >= 0; i-- ) { // fade from green to black
      fill_solid( leds, NUM_LEDS, CRGB(0,i,0));
      FastLED.show();
      delay(20);
    }
  }
  Serial.println("Now it's time to ask alexa to discover devices...");
}

void loop()
{
  espalexa.loop();
  delay(1);
}

void espalexa_addDevices() { // add Devices you want
  espalexa.addDevice("Light", colorLightChanged);
  Serial.println("devices added");
}

//the color device callback function has two parameters
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
  float r = ((rgb >> 16) & 0xFF);
  float g = ((rgb >>  8) & 0xFF);
  float b = (rgb & 0xFF);
  int rd = round( r / 255 * brightness );
  int gd = round( g / 255 * brightness );
  int bd = round( b / 255 * brightness );
  //do what you need to do here, for example control RGB LED strip
  Serial.print("Brightness: ");
  Serial.print(brightness);
  Serial.print("\tRed: ");
  Serial.print(r); //get red component
  Serial.print("\tGreen: ");
  Serial.print(g); //get green
  Serial.print("\tBlue: ");
  Serial.print(b); //get blue
  Serial.print("\tDimmed Red: ");
  Serial.print(rd); //get red component
  Serial.print("\tGreen: ");
  Serial.print(gd); //get green
  Serial.print("\tBlue: ");
  Serial.println(bd); //get blue
  fill_solid( leds, NUM_LEDS, CRGB(rd,gd,bd));
  FastLED.show();
}
ChristianKnorr commented 5 years ago

You have serial connection? What it says?

spiderhobbit commented 5 years ago

All sorted now, must have been corrupt upload or something, erased the flash and it seems fine now.