FelixAdiy
Published

Alexa as a Smart Switch!

If you ever wanted to build your own smart switch, please read the story below.

AdvancedProtip3,991
Alexa as a Smart Switch!

Things used in this project

Hardware components

ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×1
Relay (generic)
×1
Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

Alexa Voice Service
Amazon Alexa Alexa Voice Service

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic

Code

the code

C/C++
Here you have to know which echo dot you are using this code is for a V2 echo dot
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"

// prototypes
boolean connectWifi();
int zeit = 450;// 600 = 1min //60= 6sek/ 10 = 1sek
int count = 0;
//on/off callbacks 
bool LichtAn();
bool LichtAus();
//bool kitchenLightsOn();
//bool kitchenLightsOff();

// Change this before you flash
const char* ssid = "TP-LINK_28CE";
const char* password = "20348939";

boolean wifiConnected = false;

UpnpBroadcastResponder upnpBroadcastResponder;

Switch *Licht = NULL;
//Switch *kitchen = NULL;

bool isOfficeLightsOn = false;
//bool isKitchenLightstsOn = false;


boolean connectWifi(){
  boolean state = true;
  int i = 0;
  WiFi.mode(WIFI_STA);
  WiFi.setAutoReconnect(true);
  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 > 100){
      state = false;
      break;
    }
    i++;
  }
  
  if (state){
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("");
    Serial.println("Connection failed.");
  }
  
  return state;
}
void wait()
{
  delay(100);
  count++;
}
void setup()
{
  pinMode(4,OUTPUT);
   
  Serial.begin(9600);
   Serial.println("Start");
  // Initialise wifi connection
  wifiConnected = connectWifi();
  
  if(wifiConnected){
    upnpBroadcastResponder.beginUdpMulticast();
    
    // Define your switches here. Max 10
    // Format: Alexa invocation name, local port no, on callback, off callback
    Licht = new Switch("Licht", 80, LichtAn, LichtAus);
    //kitchen = new Switch("kitchen lights", 81, kitchenLightsOn, kitchenLightsOff);

    Serial.println("Adding switches upnp broadcast responder");
    upnpBroadcastResponder.addDevice(*Licht);
    //upnpBroadcastResponder.addDevice(*kitchen);
  }
}
 
void loop()
{
  if(isOfficeLightsOn && count <zeit) //Nachlaufzeit hochlaufen lassen in 100ms Schritten bis zu 45Sekunden
  {
    
    wait();
    
  }
  else                              // Wenn 45s erreicht abschalten oder ber "Alexa Licht Aus"
  {
    digitalWrite(4,0);
    isOfficeLightsOn =LOW;
   
    
  }
  if( !isOfficeLightsOn)
  {
    count = 0;
  }
  
	 if(wifiConnected){
      upnpBroadcastResponder.serverLoop();
      
      //kitchen->serverLoop();
      Licht->serverLoop();
      
	 }
   /* Evtl. wenn auto reconnect nicht funktioniert.
   if(WiFi.status()!= WL_CONNECTED)
   {
    ESP.reset();
   }*/
  
}

bool LichtAn() {
    Serial.println("Switch 1 turn on ...");
    digitalWrite(4,1);
    isOfficeLightsOn = true;    
    return isOfficeLightsOn;
}

bool LichtAus() {
    Serial.println("Switch 1 turn off ...");
    digitalWrite(4,0);
    isOfficeLightsOn = false;
    return isOfficeLightsOn;
}
/*
bool kitchenLightsOn() {
    Serial.println("Switch 2 turn on ...");

    isKitchenLightstsOn = true;
    return isKitchenLightstsOn;
}

bool kitchenLightsOff() {
  Serial.println("Switch 2 turn off ...");

  isKitchenLightstsOn = false;
  return isKitchenLightstsOn;
}
*/
// connect to wifi  returns true if successful or false if not

Credits

FelixAdiy

FelixAdiy

0 projects • 2 followers

Comments