Amazon Echo ESP8266 control, with natural speech commands

No advantage at all. Alex is clearly a better programmer, and I should rewrite my code to use an interrupt. @tzapulica

However, this project isn’t interchangeable with Alex’s, and I think we had different goals. My intent was extend the functionality of the Wemo emulation beyond just the Amazon Echo, and control my devices with a variety of methods

a) natural voice commands
b) the Blynk app
c) IFTTT triggers
d) physical switches.

With the most recent sketch, we can add “preexisting switches.”

At least for my own home automation projects, this has proven to be an extremely versatile system.

1 Like

Thank you for clarification @chrome1000, I am sort of new to hardware programming, so I was genuinely curious.
Since you mentioned it, what is a preexisting switch? ))

@chrome1000 thanks, but i m really just shooting in the dark and googling most of this stuff

the boilerplate now supports the physical button on the sonoff, blynk and mqtt.
ifttt support is a http server away. but really, that s what it is, a boilerplate, to add/remove/change however it fits your project better.

last i used it was for a siri/homekit integration using mqtt as the protocol, works fine most of the time

good luck

@kost9 The “preexisting switch” is a wall light switch. I wanted to control a ceiling lamp that is hard wired, so I mounted the Sonoff in the ceiling electrical box. This meant that I would not have access to the device’s tac switch if something were to go wrong with the wifi, server, Amazon, etc. I rewrote the sketch to switch the relay on, by default, so I can always just toggle the wall switch to use the light.

Dear all,
thank you for this great hack,
Alexa is now controlling my Lamp.

One question:
is there a possibility to send some values using Alexa to dimm my LED´s via PWM ?

Unfortunately, Alexa will only send ON or OFF commands to Wemo devices (even fake ones).

I played around for a bit with Phillips Hue Bridge emulation software for the ESP8266. The Hue is much more versatile than the Wemo, and will accept commands for colors, and light levels from 0-100%. However, the emulation software is written to control a strip of Adafruit Neopixels, I attempted a few weeks ago to strip it down for more generic use, and was able to establish communication with the Hue app, but not with Alexa.

1 Like

Can you please post the link of all the libraries you used ?




I think the rest are built into the Arduino IDE.

Could you please point me to Hue bridge emulator?

@pavel Yep.

1 Like

Thanks a lot.

Can I used the same library to control 6 channel relay with Alexa separately?

What I am trying to do is controlling 6 channel relay board connected with Wemos d1 mini with alexa.

This is the code that I could manage.

  • I am using WEMOS D1 mini and a 6 channel relay board.
  • I want to control all the 6 channels separately
  • Alexa and blynk app, both would be used to control the 6 channel relay

Will this work?

UPDATE: IT WORKS

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h> 
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"
char auth[] = "19be8f39f3f24ddca102c396d7506f07"; 
//on/off callbacks
void bedroomlightson();
void bedroomlightsoff();
void loungelightson();
void loungelightsoff();
void gymlightson();
void gymlightsoff();
void tvon();
void tvoff();
void chargeron();
void chargeroff();
void switch1on();
void switch1off();
WemoManager wemoManager;
WemoSwitch *bedroomlights = NULL;
WemoSwitch *loungelights = NULL;
WemoSwitch *gymlights = NULL;
WemoSwitch *tv = NULL;
WemoSwitch *charger = NULL;
WemoSwitch *switch1 = NULL;
const int RelayPin1 = D4;      
const int RelayPin2 = D7;
const int RelayPin3 = D6;
const int RelayPin4 = D5;
const int RelayPin5 = D0;
const int RelayPin6 = D1;
void setup(){
  pinMode(RelayPin1, OUTPUT);
  digitalWrite(RelayPin1, LOW);   // Turn relay OFF when unit is powered on
  pinMode(RelayPin2, OUTPUT);
  digitalWrite(RelayPin2, LOW);   // Turn relay OFF when unit is powered on
  pinMode(RelayPin3, OUTPUT);
  digitalWrite(RelayPin3, LOW);   // Turn relay OFF when unit is powered on
  pinMode(RelayPin4, OUTPUT);
  digitalWrite(RelayPin4, LOW);   // Turn relay OFF when unit is powered on
  pinMode(RelayPin5, OUTPUT);
  digitalWrite(RelayPin5, LOW);   // Turn relay OFF when unit is powered on
  pinMode(RelayPin6, OUTPUT);
  digitalWrite(RelayPin6, LOW);   // Turn relay OFF when unit is powered on
  
  Serial.begin(9600);
  WiFiManager wifi;   //WiFiManager intialization.
  wifi.autoConnect("FakeWemo"); //Create AP, if necessary
  wemoManager.begin();
  
  // Format: Alexa invocation name, local port no, on callback, off callback
  
  bedroomlights = new WemoSwitch("bedroom lights", 80, bedroomlightson, bedroomlightsoff);
  loungelights = new WemoSwitch("lounge lights", 81, loungelightson, loungelightsoff);
  gymlights = new WemoSwitch("gym lights", 82, gymlightson, gymlightsoff);
  tv = new WemoSwitch("tv", 83, tvon, tvoff);
  charger = new WemoSwitch("charger", 84, chargeron, chargeroff);
  switch1 = new WemoSwitch("switch 1", 85, switch1on, switch1off);
  
  wemoManager.addDevice(*bedroomlights);
  wemoManager.addDevice(*loungelights);
  wemoManager.addDevice(*gymlights);
  wemoManager.addDevice(*tv);
  wemoManager.addDevice(*charger);
  wemoManager.addDevice(*switch1);
  Blynk.config(auth);
  ArduinoOTA.begin();
}
// Toggle the relay on
void bedroomlightson() 
  {
  
    digitalWrite(RelayPin1, HIGH);
    Blynk.virtualWrite(V1, HIGH);     // Sync the Blynk button widget state
}
void loungelightson() 
{
    digitalWrite(RelayPin2, HIGH);
    Blynk.virtualWrite(V2, HIGH);     // Sync the Blynk button widget state
}
void gymlightson() 
{
    digitalWrite(RelayPin3, HIGH);
    Blynk.virtualWrite(V3, HIGH);     // Sync the Blynk button widget state
}
void tvon() 
{
    digitalWrite(RelayPin4, HIGH);
    Blynk.virtualWrite(V4, HIGH);     // Sync the Blynk button widget state
}
void chargeron()
{
    digitalWrite(RelayPin5, HIGH);
    Blynk.virtualWrite(V5, HIGH);     // Sync the Blynk button widget state
}
void switch1on() 
{
    digitalWrite(RelayPin6, HIGH);
    Blynk.virtualWrite(V6, HIGH);     // Sync the Blynk button widget state
}
// Toggle the relay off
void bedroomlightsoff() 
{
    digitalWrite(RelayPin1, LOW);
    Blynk.virtualWrite(V1, LOW);      // Sync the Blynk button widget state
}
void loungelightsoff() 
{
    digitalWrite(RelayPin2, LOW);
    Blynk.virtualWrite(V2, LOW);      // Sync the Blynk button widget state
}
void gymlightsoff() 
{
    digitalWrite(RelayPin3, LOW);
    Blynk.virtualWrite(V3, LOW);      // Sync the Blynk button widget state
}
void tvoff() 
{
    digitalWrite(RelayPin4, LOW);
    Blynk.virtualWrite(V4, LOW);      // Sync the Blynk button widget state
}
void chargeroff() 
{
    digitalWrite(RelayPin5, LOW);
    Blynk.virtualWrite(V5, LOW);      // Sync the Blynk button widget state
}
void switch1off() 
{
    digitalWrite(RelayPin6, LOW);
    Blynk.virtualWrite(V6, LOW);      // Sync the Blynk button widget state
    }
// Handle switch changes originating on the Blynk app
BLYNK_WRITE(V1){
  int SwitchStatus1 = param.asInt();
   if (SwitchStatus1 == 1) {
    digitalWrite(RelayPin1, LOW);
  }
  else {
    digitalWrite(RelayPin1, HIGH);
  }
}
BLYNK_WRITE(V2){
  int SwitchStatus2 = param.asInt();
  if (SwitchStatus2 == 1) {
    digitalWrite(RelayPin2, LOW);
  }
  else {
    digitalWrite(RelayPin2, HIGH);
  }
}
BLYNK_WRITE(V3){
  int SwitchStatus3 = param.asInt();
  if (SwitchStatus3 == 1) {
    digitalWrite(RelayPin3, LOW);
  }
  else {
    digitalWrite(RelayPin3, HIGH);
  }
}
BLYNK_WRITE(V4){
  int SwitchStatus4 = param.asInt();
  if (SwitchStatus4 == 1) {
    digitalWrite(RelayPin4, LOW);
  }
  else {
    digitalWrite(RelayPin4, HIGH);
  }
}
BLYNK_WRITE(V5){
  int SwitchStatus5 = param.asInt();
  if (SwitchStatus5 == 1) {
    digitalWrite(RelayPin5, LOW);
  }
  else {
    digitalWrite(RelayPin5, HIGH);
  }
}
BLYNK_WRITE(V6){
  int SwitchStatus6 = param.asInt();
  if (SwitchStatus6 == 1) {
    digitalWrite(RelayPin6, LOW);
  }
  else {
    digitalWrite(RelayPin6, HIGH);
  }
 
}
void loop()
{
  wemoManager.serverLoop();
  Blynk.run();
  ArduinoOTA.handle();
}

Maybe this will be of interest to some of you

1 Like

Yeah, I watched this video several months ago, but chose to use Wemo emulation instead, since it didn’t require an external controller. The Swiss engineer’s system just seemed too complex.

Hi @chrome1000

This is brill stuff and I will be deffo trying this. This almost sounds like a re-working of what I was trying to do with my 2 way lighting with ac feedback to Blynk app.

My question is when you flick the manual wall switch and the wifi HASN’T failed I’m guessing it wont update the blynk app to say it is on?

But either way I love this and have an alexa and this was going to be my next step for my 2 way circuits to operate with voice via alexa, but to also get the feedback to blynk so you know it is on from the app.

Cheers

Kev

You’re right. I’ll have to fix that. It might be as simple as adding a Blynk.virtualWrite to the setup. Does anyone know if it’s okay to do that before the Blynk.run() statement?

But how will you do that real time ie after setup loop, when you flick the switch - how does anything know you have done that ???

Cheers

kev

After the setup, all changes to the relay are made in the lightOn and lightOff functions. The last command in each of those functions is a Blynk.virtualWrite that synchronizes the associated button on the Blynk dashboard. It doesn’t matter if the trigger came from IFTTT, Blynk, Alexa, or a physical switch. The Blynk dashboard will always be synchronized to the relay.

Now, if you turn off the system with a wall switch, the logic breaks down. Obviously, if you cut the power going to the ESP8266, it can’t update the Blynk dashboard.

1 Like

still don’t quite get this @chrome1000 - when you toggle the light switch how does blynk know you have done that - where is it getting the feedback from - diagram to explain perhaps ?

Cheers

kev

Okay, the button sync on startup is fixed.

I decided to leave the onboard button polling as-is. My attempt to use an interrupt caused the ESP to crash, so I’m sticking with what works.

@newdos Here’s the setup for the ON-at-startup version of the code:

When the wall switch is toggled from OFF to ON, the Sonoff/ESP is powered on. In the setup function, the relay is turned on, which sends power to the lamp, turning it on. A Blynk.syncVirtual() command is also called, so that the Blynk dashboard button matches the state of the lamp.

While the wall switch REMAINS in the ON position, the ESP is running, all the other methods are available to turn the relay ON or OFF (Alexa, IFTTT, Blynk, onboard switch). Whenever one of these methods is used, either the lightOn() function or the lightOff() function is called. The last command in each of those functions is a Blynk.virtualWrite(), which again synchronizes the Blynk dashboard button with the current state of the relay and lamp.

If the wall switch is toggled from ON to OFF, power is removed from the ESP, relay, and lamp. This is the one case where your Blynk button would not be synchronized with the state of the lamp. However, the normal condition with this setup is to leave the wall switch ON just about all the time. However, if your wifi stops working for some reason, the wall switch is still usable in a traditional manner. Turning it on would turn on your light. Turning it off would turn off your light. Under this condition, synchronization with Blynk is moot, since your wifi isn’t working anyway.

2 Likes