Published

Smart Home: ESP8266/ESP32 with Alexa and Amazon Echo

Integrate Alexa with your ESP8266 (ESP32) board.

BeginnerFull instructions provided13,398
Smart Home: ESP8266/ESP32 with Alexa and Amazon Echo

Things used in this project

Story

Read more

Code

Code snippet #1

Plain text
#include <ESP8266WiFi.h&gt;
#include <ESPAsyncTCP.h&gt;
#include <fauxmoESP.h&gt;

fauxmoESP fauxmo;

void setup() { 
  Serial.begin(115200);
  
  if (connectWifi()) {
    // Setup fauxmo
     Serial.println("Adding LED device");
     fauxmo.setPort(80);  
     fauxmo.enable(true);
     fauxmo.addDevice("Led");   
  }
}

void loop() {
  fauxmo.handle();
}

boolean connectWifi() {
  // Let us connect to WiFi
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println(".......");
  Serial.println("WiFi Connected....IP Address:");
  Serial.println(WiFi.localIP());

  return true;
}

Code snippet #2

Plain text
...
fauxmo.onSetState([](unsigned char device_id, const char * device_name, 
                    bool state, unsigned char value) {
    Serial.print("Device name:");
    Serial.println(device_name);
    // Here we handle the command received
}
...

Code snippet #4

Plain text
#include <Adafruit_NeoPixel.h&gt;

#define PIN D2
#define PIN_STRIP D1
#define NUM_LEDS 12
#define NUM_LEDS_STRIP 8

// LEDS
Adafruit_NeoPixel pixelsRing(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelsStrip(NUM_LEDS_STRIP, PIN_STRIP, NEO_GRB + NEO_KHZ800);

void setup() { 
  Serial.begin(115200);
  pixelsRing.setBrightness(200);
  pixelsRing.begin();
  pixelsStrip.begin();
  ...
}

void turnOffRing() {
  pixelsRing.fill();
  pixelsRing.show();
}


void turnOffStrip() {
  pixelsStrip.fill();
  pixelsStrip.show();
}

void setRingColor(int r, int g, int b) {
  for (int i=0; i < NUM_LEDS; i++)
    pixelsRing.setPixelColor(i, pixelsRing.Color(r,g,b));

  pixelsRing.show();
}

void setStripColor(int r, int g, int b) {
  pixelsStrip.fill(pixelsStrip.Color(r,g,b));
  pixelsStrip.show();
}

Code snippet #6

Plain text
fauxmo.onSetState([](unsigned char device_id, const char * device_name, 
                     bool state, unsigned char value) {
       Serial.print("Device name:");
       Serial.println(device_name);
       if (strcmp(device_name, "Ring") == 0) {
          Serial.print("Ring status:");
          Serial.print(state);
          Serial.println("");
          // Set RGB Color
          if (state)
             setRingColor(10,100,40);
          else
             turnOffRing();         
       }
       else if (strcmp(device_name, "Strip") == 0) {
         Serial.print("Strip status:");
          Serial.print(state);
          Serial.println("");
          // Set RGB Color
          if (state)
             setStripColor(180,30,40);
          else
             turnOffStrip();                 
       }
     });
   }

Bitbucket

https://bitbucket.org/xoseperez/fauxmoesp/src/master/

Github

https://github.com/me-no-dev/ESPAsyncTCP

Github

https://github.com/me-no-dev/AsyncTCP

Github

https://github.com/adafruit/Adafruit_NeoPixel

Credits

Comments