Alexa as a Smart Switch!

Alexa as a Smart Switch!

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

  • 1,412 views
  • 1 comment
  • 5 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

Introduction

I have seen some other project with Alexa and I have wanted to play a little with it and so I have decided to buy one. But after I have ordered it I have found a good and useful application in my own house with Alexa!

The problem is in the entire room of the house. We are currently using a motion detector to turn on the light. But as you can see in the picture the location for the sensor isn't the best so sometimes it will not switch on the light. And also sometimes it detects our small dog also as a person which is at night quite annoying.

Part 1

So I have decided to realize the hole thing with Alexa and an ESP8266 NodeMCU (ESP-12) – last but not least also a relay. The first test were made with a simple LED, this LED can be turned on with “Alexa Licht an!” which is German and says “Alexa Light on”.

So basically with the LED I have tested the hole thing at first. The LED will be turned on after 45 sec. It will be turned off again automatically or you can also say “Alexa Licht aus!”(“Alexa lights out”). Then I started to print a housing for the hole application (power supply/relay/ESP). Do not wonder the power supply is a little over-sized, but I had this as a leftover from another project ;)

Part 2

the first check

If you want to have to CAD (stl-file) form the box please leave a comment. Then I have put everything together and installed it into one of the LED light housings.

While putting everything together I have also installed an “emergency switch” in case the WLAN goes down or so that I can still turn the lights on.--> also it is pretty good in case you want to have the lights turned on for more than 45 sec. If you search something or so.

the end result

Code

the codeC/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

Schematics

Schematic
schaltplan_kZhv9stlkN.fzz

Comments

Similar projects you might like

Alexa Smart Socket

Project tutorial by Emmanuel Edwards

  • 1,209 views
  • 4 comments
  • 9 respects

Alexa Based Smart Home Monitoring

Project tutorial by Adithya TG

  • 16,639 views
  • 19 comments
  • 47 respects

Hygge Home - Alexa Smart Bath

Project tutorial by J Howard

  • 5,355 views
  • 2 comments
  • 18 respects

Alexa Enabled USB Power Switch

Project tutorial by Stephen Harrison

  • 3,654 views
  • 7 comments
  • 29 respects

Smart Pool: Alexa Controlled Pool Manager

Project tutorial by Benjamin Winiarski

  • 1,360 views
  • 2 comments
  • 7 respects
Add projectSign up / Login