Christopher Mendez Martinez
Published © GPL3+

Alexa ESP8266 Based LED Strip Controller + Gaming PC

Hello to everyone, today I am here to show you how to control a LED strip attached to a Gaming Laptop with Alexa and the ESP8266.

IntermediateFull instructions provided2 hours135
Alexa ESP8266 Based LED Strip Controller + Gaming PC

Things used in this project

Hardware components

JLCPCB Customized PCB
JLCPCB Customized PCB
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram

Code

Alexa_LED_Strip_IR.ino

Processing
This is the code of the implementation of the project
/*
 * Christopher Mndez
 * 10-Ago-2021
 * Control de Carga y Tira LED con Alexa y el ESP8266
*/

#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <ESP8266WiFi.h>

  #define IR 3 // con el pin 1 RX falla, debe ser el 3
  #define TRIAC_PIN 0
  #define boton  2

IRsend irsend(IR);  // An IR LED is controlled by GPIO pin 1(tx)

#include "fauxmoESP.h"

int estado = LOW;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50; 

#define SERIAL_BAUDRATE 115200

#define WIFI_SSID "CLARO403"
#define WIFI_PASS "2336879809"

#define LAMP_1 "tira led" // Nombre del dispositivo en Alexa


fauxmoESP fauxmo;


// Wi-Fi Connection
void wifiSetup() {

  
  // Setear mod
  WiFi.mode(WIFI_STA);

  // Conectar
  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("Conectado");
  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();

  // Wi-Fi connection
  wifiSetup();
  
  irsend.begin();
  // LED
  pinMode(TRIAC_PIN, OUTPUT);
  pinMode(boton, INPUT);
  //digitalWrite(TRIAC_PIN, HIGH);
  
  //mySwitch.enableReceive(RF_RECEIVER);  // Receiver on interrupt 0 => that is pin #2

  // By default, fauxmoESP creates it's own webserver on the defined port
  // The TCP port must be 80 for gen3 devices (default is 1901)
  // This has to be done before the call to enable()
  fauxmo.createServer(true); // not needed, this is the default value
  fauxmo.setPort(80); // This is required for gen3 devices

  fauxmo.enable(true);
  // You can use different ways to invoke alexa to modify the devices state:
  // "Alexa, turn lamp two on"

  // Add virtual devices
  fauxmo.addDevice(LAMP_1);

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
    // Callback when a command from Alexa is received. 
    // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
    // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
    // Just remember not to delay too much here, this is a callback, exit as soon as possible.
    // If you have to do something more involved here set a flag and process it in your main loop.
        
    Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
    if ( (strcmp(device_name, LAMP_1) == 0) ) {
      // this just sets a variable that the main loop() does something about
      Serial.println("RELAY 1 switched by Alexa");
      //digitalWrite(TRIAC_PIN, !digitalRead(TRIAC_PIN));
      if (state) {
        digitalWrite(TRIAC_PIN, HIGH);
        irsend.sendNEC(0xFF827D, 32);
        estado = 1;
      } else {
        digitalWrite(TRIAC_PIN, LOW);
        irsend.sendNEC(0xFF02FD, 32);
        estado = 0;
      }
    }
  });

}

void loop() {
  // fauxmoESP uses an async TCP server but a sync UDP server
  // Therefore, we have to manually poll for UDP packets
  fauxmo.handle();

  static unsigned long last = millis();
  if (millis() - last > 5000) {
    last = millis();
    Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
  }

int reading = digitalRead(boton);

  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is LOW
      if (buttonState == LOW) {
        estado = !estado;
        // Encender la luz:
      digitalWrite(TRIAC_PIN, estado);
      }
    }
  }

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
    
}

Credits

Christopher Mendez Martinez

Christopher Mendez Martinez

35 projects • 69 followers
Electronic Engineer and Tech YouTuber who loves automation, programming and sharing his knowledge with everyone.

Comments