Amazon Echo ESP8266 control, with natural speech commands

Oh… this is huge! I’ve been wanting to build some automated blinds for my home, using “open” and “close” commands instead of “off” and “on.” This will be perfect.

I wonder if there’s a way to rewrite the library so that the alternative utterances can be defined in the sketch, rather than altering the library each time.

1 Like

Yes first tried the open close and it works perfect
But I tried to change the respond instead of OK to like The drink is preparing but I had no luck I’ll try again see if I am able to

@dananmo Can you share your altered library? The only place that I find “turn on” or “turn off” is in this section:

 if(request.indexOf("<BinaryState>1</BinaryState>") > 0) {
      Serial.println("Got Turn on request");
      onCallback();
  }

  if(request.indexOf("<BinaryState>0</BinaryState>") > 0) {
      Serial.println("Got Turn off request");
      offCallback();
  }

Yes in this section I changed it did u try it ?

Yes, I did try. Apparently, I’ve misunderstood something. Can you show exactly what you changed to make this work?

Yes currently am at work now once am
Home I’ll post it my friend

1 Like

Finally found time to tinker with it. It’s brilliant!

Does anybody know if it’s possible with this library to tell Alexa to set specific value?
I can do it with my philips hue lights…

Like: Alexa, set “whatever” to 50

Could you provide instructions how to do that? It’s friday time, and I need some vodka
:cocktail::slight_smile:

Thanks.

Lol sure sir

I think I failed now it’s not working it’s not recognizing the command
I tried to change it form the Src files the callbackfunction
Instead of oncallback to opencallback
It didn’t work
I think I did mistake yesterday I think Alexa mis hear me
I do apologize for that
I was happy too I said I’ll be able to added it to my bartender but I guess it’s not quit accurate .
I’ll keep trying
I apologize again

Okay, I wanted to install one of these handy Wemo emulators in the junction box of my dining room pendant lamp. This posed a small problem. If there were an issue with the wifi, there would be no way to turn on the lamp. The onboard switch would be buried inside the electrical box.

So I decided to make a variation of the code that switches the relay on at startup. In doing so, the light can be switched on at any time by simply toggling the wall switch from OFF to ON. It also makes it convenient for anyone unfamiliar with your IoT system, since the wall switch will behave in a traditional manner. I commented out lines controlling the onboard LED, since it would never be visible, anyway. EDIT: Fixed the startup button sync problem, with the help of @costas.

/* Extends the capabilities of witnessmenow's awesome WEMO emulator library to work with Blynk, IFTTT,
 * and physical switches.
 * 
 * This version is intended for use in line with an existing wall switch. The relay is set to ON at 
 * startup. In this situation, if wifi stops working, the existing wall switch can be used to turn the 
 * relay ON and OFF.  
 * 
 * OTA update capabiliity is hardware dependent (needs adequate memory), but does't cause any 
 * problems for devices that don't support it.
 * 
 * Wemo Emulator and WiFi Manager libraries:
 * https://github.com/witnessmenow/esp8266-alexa-wemo-emulator
 * https://github.com/tzapu/WiFiManager
 * 
 * In order to control a multitude of devices with a single Blynk dashboard, each ESP8266
 * should be programmed with a unique virtual pin assignment, corresponding to a Blynk switch.
 * 
 * For IFTTT control, use the Maker Channel with the following settings:
 *    URL: http://blynk-cloud.com:8080/YOUR_TOKEN/V1       Substitute your own token and vitual pin 
 *    Method: PUT
 *    Content type: application/json
 *    Body: {"1"]                                          Use 1 for ON, 0 for OFF, 2 for toggle
 */
 
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h> 
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <SimpleTimer.h>

#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"

#define VPIN V1  //Use a unique virtual pin for each device using the same token / dashboard

char auth[] = "YOUR_TOKEN"; //Get token from Blynk

//on/off callbacks
void lightOn();
void lightOff();

WemoManager wemoManager;
WemoSwitch *light = NULL;

boolean LampState = 1;
boolean SwitchReset = true;   //Flag indicating that the hardware button has been released

const int TacSwitch = 0;      //Pin for hardware momentary switch. Pin 0 on Sonoff
const int RelayPin = 12;      //Relay switching pin. Relay is pin 12 on the SonOff
//const int LED = 13;          //ON / OFF indicator LED. Onboard LED is 13 on Sonoff

SimpleTimer timer;

void setup(){
  pinMode(RelayPin, OUTPUT);
  //pinMode(LED, OUTPUT);
  pinMode(TacSwitch, INPUT_PULLUP);
  delay(10);
  digitalWrite(RelayPin, HIGH);   // Turn relay ON when unit is powered on
  //digitalWrite(LED, LOW);
  
  Serial.begin(115200);

  WiFiManager wifi;   //WiFiManager intialization.
  wifi.autoConnect("FakeWemo"); //Create AP, if necessary

  wemoManager.begin();
  // Format: Alexa invocation name, local port no, on callback, off callback
  light = new WemoSwitch("SonOfWemo", 80, lightOn, lightOff);
  wemoManager.addDevice(*light);

  Blynk.config(auth);
  ArduinoOTA.begin();

  // Wait for the connection to Blynk, and sync the dashboard button state
  while (Blynk.connect() != true && millis() < 10000){
  }
  Blynk.virtualWrite(VPIN, HIGH);
  Blynk.syncVirtual(VPIN);
   
  timer.setInterval(100, ButtonCheck);
}

void loop()
{
  wemoManager.serverLoop();
  Blynk.run();
  ArduinoOTA.handle();
  timer.run();
}

// Toggle the relay on
void lightOn() {
    Serial.println("Switch 1 turn on ...");
    digitalWrite(RelayPin, HIGH);
    //digitalWrite(LED, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VPIN, HIGH);     // Sync the Blynk button widget state
}

// Toggle the relay off
void lightOff() {
    Serial.println("Switch 1 turn off ...");
    digitalWrite(RelayPin, LOW);
    //digitalWrite(LED, LOW);
    LampState = 0;
    Blynk.virtualWrite(VPIN, LOW);      // Sync the Blynk button widget state
}

// Handle switch changes originating on the Blynk app
BLYNK_WRITE(VPIN){
  int SwitchStatus = param.asInt();

  Serial.println("Blynk switch activated");

  // For use with IFTTT, toggle the relay by sending a "2"
  if (SwitchStatus == 2){
    ToggleRelay();
  }
  else if (SwitchStatus){
    lightOn();
  }
  else lightOff();
}

// Handle hardware switch activation
void ButtonCheck(){
  // look for new button press
  boolean SwitchState = (digitalRead(TacSwitch));

  // toggle the switch if there's a new button press
  if (SwitchState == LOW && SwitchReset == true){
    Serial.println("Hardware switch activated");
    
    ToggleRelay();

    SwitchReset = false;  // Flag that indicates the physical button hasn't been released
    delay(50);            //debounce
  }
  else if (SwitchState){
    SwitchReset = true;   // reset flag the physical button release
  }
}

void ToggleRelay(){

  if (LampState){
    lightOff();
  }
  else lightOn();
}

2 Likes

Next question: how’s wall switch connected to relay? or simply put: could you dedcribe all the wirings?

Thanks!

I used a Sonoff basic unit, which simplifies the wiring dramatically, as it combines a relay, an ESP8266 and a low voltage power supply into a single device. The device takes in mains voltage on the input side (typically 110V in the U.S.), and delivers that same voltage to the output side when the relay is switched on. There are several tutorials online that show how to program the Sonoff, but if you have any trouble, let me know, and I’ll post instructions for that, too.

WARNING Be sure to turn off the power at the circuit breaker or fuse box before you try any of this.

So there are a few different ways that you could wire this. If you just wanted to replace a wall switch with a Sonoff, the connections are identical to that of the switch. The wall switch has two wires originating at the circuit breaker box that enter it. Another two wires exit the switch, and terminate at the socket or outlet that is being switched. Just remove the wires from the switch, and make the same connections to the Sonoff. The wires are normally black and white. The black is the so-called “hot” wire, and the white one is “neutral.” They’re marked with L and N, respectively, on the connection points of the Sonoff. There may be a green ground wire in the junction box, as well, but we can ignore it. You simply take the black and white wires that come from the circuit breaker, and connect them to the input side of the Sonoff, and connect the two wires that go to the outlet or lamp socket, and connect them to the output side of the Sonoff. That’s it.

Now, for my ceiling lamp, I did things slightly differently. I wanted the wall switch to remain in place, and to still be usable in case the wifi was out. For this, I placed the Sonoff unit in the ceiling electrical box that holds up my pendant lamp. The wiring was the same as in the previous instructions, only this time, the black and white wires coming from the wall switch were attached to the input side of the Sonoff, and the wires going to the pendant lamp were attached to the output side. For this type of connection, use the most recent variation of the code, which turns the relay on before it attempts to connect to the wifi.

The Sonoff can be found on Ebay or here:
http://sonoff.itead.cc/en/products/sonoff/sonoff-basic

1 Like

Thanks a lot!

Do you feel comfortable connecting the wires from circuit box to Sonoff? I mean, I trust Itead, but…

Let me clarify the question. If you’re asking if I’m comfortable working with mains power, the answer is yes, but not everyone is, which is why I put the warning up there. There’s not too much that you can screw up with the instructions above. Even if you if you hooked the device up backwards, it would just not function. However, I don’t want to be responsible for noobs electrocuting themselves. So if you’ve never replaced an outlet or light switch, maybe stick with the Itead S20, which doesn’t require you to touch the mains wiring.

If you’re asking about my comfort with Itead’s component quality and safety circuitry, the answer is… uh… well… sorta. :slight_smile: But here’s my logic: the Sonoff has to be safer inside a junction box than outside. I mean, the whole point of a junction box is containment. If anything sparks or short circuits, the junction box is there to keep the evil inside, which is why they’re always made of non-flamable material. Also, the Sonoff’s own factory enclosure is made of ABS, which is inherently fire retardant, so you should be double protected. Finally, I’m only using this unit to power a single 100W light fixture, which means that the relay is passing less than 10% of the current for which it’s rated. That’s a pretty big margin of safety.

Hope this helps.

1 Like

I’ve ordered two of those Sonoff’s once. One of them nearly exploded in my face when I connected the mains, so not for me anymore. I’m also comfortable working with mains and large amounts of current (I’m in IT and I do datacenters as well). No more of this for me until I find a way to properly relay 220v. I\m thinking of using some standard components like those solid state relays in conjunction with some a separate security for each relay.

I don’t have to do stuff outside though (not yet anyway…).

1 Like

Can you elaborate on what happened with your Sonoff device? Is it clear which component failed? Did it fail when Power was applied, or when the relay was activated? I’ve read in several blogs that trying to program the Sonoff while it is attached to the mains will definitely fry it.

To be sure, I’ve “let the smoke out” of my fair share of electronic components, but it’s almost always been a case of user error. My record with the Sonoff, so far, is zero failures from 9 devices (knock on wood). :grinning:

1 Like

I have no idea where it failed, it scared me enough to jst rip all the cords put, lol. No fun getting hit by 220v … unless that is your thing.

I wired it up like it should go, the other one was fine, same wiring, and it’s not like you can switch the wires with mains.

Kinda sorta true, as far as CLOSED circuit operation goes… but one side is HOT and the other NEUTRAL (ground reference), so when OPEN a properly wired device will isolate the HOT wire from the rest of the circuit, whereas wired the other way around leaves potential for even a OPEN circuit to be accidently shorted to ground… usually through the person fiddling with the thing :scream:

Hi, this is a great project!
I have a question though. What is an advantage of handling the physical button press by calling the ButtonCheck function every 0.1 seconds over using interrupts like it was done in the sonoff boiler project by @tzapulica ? Thanks!

T.b.h. I think calling an interrupt is a better solution. The harsh truth is that interrupt handling can be a bit tricky. So the timed solution is easier to implement.

It would be a great idea for the documentation, though. It would save considerable CPU time, which is already pretty limited on an MCU.