Amazon Echo ESP8266 control, with natural speech commands

Perhaps I am not so smart as you are.
But but what I must say, the “Wemo Emulator” (the Belkin Wemo) that you used in your first project ,fauxmoESP ,espurna ,Espalexa ,Tasmota and a lot more all are present thanks to Aruna Tennakoon " kakopappa ".
all the hard work was done by kakopappa.
now with SINRIC also from kakopappa you can control

TV / Speaker

Alexa.PowerController
Alexa.PlaybackController
Alexa.InputController
Alexa.Speaker
Alexa.StepSpeaker

Window Shutters

Alexa.PowerController
Alexa.ChannelController
Alexa.PercentageController

Thermostat

Alexa.PowerController
Alexa.ThermostatController
Alexa.TemperatureSensor

Switch

Alexa.PowerController

Light

Alexa.PowerController
Alexa.ColorTemperatureController
Alexa.ColorController
Alexa.BrightnessController

and now with GOOGLE HOME
you can check my post

@chrome1000 i gave up trying the routines.On a separate issue,i followed the tempory fix for light ON in Alexa app, changed the text like it said but lights still turn on in Alexa app when i enter the devices page,any ideas?

@jasperdog Yep. It started with an Alexa app update several months ago. However, since I typically use either voice control or Blynk, It’s a pretty rare occurrence.

here’s a code that i wrote for using a 4channel relay board. i have not modified it but you could just modify this code. this works using an easy to use sinric skill. since i am terrible at coding. Pardon ME.
it also has some other stuff for controlling my ac and the temerature

#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRtimer.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Sinric.h>
#include <SinricSwitch.h>
#include <SinricThermostat.h>
#include <SimpleTimer.h>
#include <ESP8266HTTPClient.h>
#include “CallbackFunction.h”
#include <TimeLib.h>
#include <WidgetRTC.h>
//Blynk Credentials
char auth = “BLYNK AUTH”; //Get token from Blynk
const char* api_key = “SINRIC API KEY”;
const char* switchId = "DEVICE ID;
const char* switchId1 = “DEVICE ID”;
const char* switchId2 = “DEVICE ID”;
const char* switchId3 = “DEVICE ID”;
const char* thermoId = “DEVICE ID”;

HTTPClient http;
WidgetRTC rtc;
WidgetBridge bridge1(V7); //Initiating Bridge Widget on V1 of Device A
WidgetTerminal terminal(V25);
SimpleTimer timer;
const int Relays[1][4] = {
  {16,13,14,3} //
};
const int Switch[1][4] = {
  {5,2,4,0} //
};
unsigned int AcData[4][14]{
  //17,     18,      19,      20,      21,      22,      23,      24,      25,      26,      27,      28,      29,      30
  {0xB2BF00,0xB2BF10,0xB2BF30,0xB2BF20,0xB2BF60,0xB2BF70,0xB2BF50,0xB2BF40,0xB2BFC0,0xB2BFD0,0xB2BF90,0xB2BF80,0xB2BFA0,0xB2BFB0},//Auto
  {0xB29F00,0xB29F10,0xB29F30,0xB29F20,0xB29F60,0xB29F70,0xB29F50,0xB29F40,0xB29FC0,0xB29FD0,0xB29F90,0xB29F80,0xB29FA0,0xB29FB0},//Low
  {0xB25F00,0xB25F10,0xB25F30,0xB25F20,0xB25F60,0xB25F70,0xB25F50,0xB25F40,0xB25FC0,0xB25FD0,0xB25F90,0xB25F80,0xB25FA0,0xB25FB0},//Mediun
  {0xB23F00,0xB23F10,0xB23F30,0xB23F20,0xB23F60,0xB23F70,0xB23F50,0xB23F40,0xB23FC0,0xB23FD0,0xB23F90,0xB23F80,0xB23FA0,0xB23FB0}//High
};
int AcR;
int AcC;
const int IR_PIN = 12;
boolean SwitchReset[5] = {true,true,true,true,true};
int DeviceState[5]={0,0,0,0,0};
int Counter[5] = {0,0,0,0,0};
int AcFanSpd = 3;
int AcOnFanOff = 0;
int AcTemp = 23;
int TEMP;
int temperature;
//Real Time Clock
int RTC_HOUR;
int RTC_MINUTES;
int c = 0;

//TIMER_INPUT
int On_timer[2][3];
int Off_timer[2][3];
IRsend irsend(IR_PIN); 

void setup()      
 {
  Serial.begin(115200);
  pinMode(Relays[0][0], OUTPUT);
  pinMode(Relays[0][1], OUTPUT);
  pinMode(Relays[0][2], OUTPUT);
  pinMode(Relays[0][3], OUTPUT);
  digitalWrite(Relays[0][0],HIGH);
  digitalWrite(Relays[0][1],HIGH);
  digitalWrite(Relays[0][2],HIGH);
  digitalWrite(Relays[0][3],HIGH);
  pinMode(Switch[0][0], INPUT_PULLUP);
  pinMode(Switch[0][1], INPUT_PULLUP);
  pinMode(Switch[0][2], INPUT_PULLUP);
  pinMode(Switch[0][3], INPUT_PULLUP);
  WiFiManager wifi;   //WiFiManager intialization.
  wifi.autoConnect("IOTA-Automation Systems"); //Create AP, if necessary
  wifi.setConfigPortalTimeout(180);
  WiFi.mode(WIFI_STA);
//PINMODE
  Blynk.config(auth);
  ArduinoOTA.begin();

  timer.setInterval(60, ButtonCheck);
    setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
  timer.setInterval(1000, clockDisplay);
  setupSinric();
}
void ButtonCheck()
{
  SwitchState();
//  SwitchState0();
//  SwitchState1();
 // SwitchState2();
//  SwitchState3();
}
// callback for switch...
void onPowerState(const String& deviceId, bool state) { 
  if (deviceId == switchId){    //LIGHTS
    if (state == HIGH){
      lightOn(2);
    }else if (state == LOW){
      lightOff(2);
    }
  }
    if (deviceId == switchId1){  //FAN
    if (state == HIGH){
      lightOn(0);
    }else if (state == LOW){
      lightOff(0);
    }
  }
    if (deviceId == switchId2){ //MEDIA CENTER
    if (state == HIGH){
      lightOn(1);
    }else if (state == LOW){
      lightOff(1);
    }
  }
    if (deviceId == switchId3){  //SOCKET
    if (state == HIGH){
      lightOn(3);
    }else if (state == LOW){
      lightOff(3);
    }
  }
    if (deviceId == thermoId){ //AC
    if (state == HIGH){
      ACON();
    }else if (state == LOW){
      ACOFF();
    }
  }
}
void onSetTargetTemperature(const String& deviceId, double temp, const String& scale) {
  if (deviceId == thermoId){
if(DeviceState[4] = 1){
  AcTemp = round(temp);
  ACTEMP();
} else if (DeviceState[4] = 0){
  DeviceState[4] = 1;
  AcTemp = round(temp);
  timer.setTimeout(1000, ACTEMP);
} Blynk.virtualWrite(V4,HIGH);
ACONFANOFF();
  }
}
void onAdjustTargetTemperature(const String& deviceId, double temp, const String& scale) { Serial.printf("onAdjustTargetTemperature(%s,%f,%s)\r\n", deviceId.c_str(), temp, scale.c_str()); }
void onSetThermostatMode(const String& deviceId, const String& mode)                     { Serial.printf("onSetThermostatMode(%s,%s)\r\n", deviceId.c_str(), mode.c_str()); }
//SETUP SINRIC
void setupSinric() {
  Serial.print("Connecting Sinric");
  Sinric.begin(api_key);
  while (!Sinric.isConnected()) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("connected!");
  // add new Switch 
  SinricSwitch& mySwitch = Sinric.add<SinricSwitch>(switchId);
  SinricSwitch& mySwitch1 = Sinric.add<SinricSwitch>(switchId1);
  SinricSwitch& mySwitch2 = Sinric.add<SinricSwitch>(switchId2);
  SinricSwitch& mySwitch3 = Sinric.add<SinricSwitch>(switchId3);
  // set the callback
  mySwitch.onPowerState(onPowerState);
  mySwitch1.onPowerState(onPowerState);
  mySwitch2.onPowerState(onPowerState);
  mySwitch3.onPowerState(onPowerState);
  // add new Thermostat 
  SinricThermostat& myThermo = Sinric.add<SinricThermostat>(thermoId);
  // set the callbacks for thermostat
  myThermo.onPowerState(onPowerState);
  myThermo.onSetTargetTemperature(onSetTargetTemperature);
  myThermo.onAdjustTargetTemperature(onAdjustTargetTemperature);
  myThermo.onSetThermostatMode(onSetThermostatMode);
}

void loop(){
  Blynk.run();
  ArduinoOTA.handle();
  timer.run();
  irsend.begin();
  Sinric.handle();
}
void lightOn(int pin) {
    
    digitalWrite(Relays[0][pin],LOW);
    DeviceState[pin] = 1;
  switch(pin){
    case 0:Blynk.virtualWrite(V0, HIGH); break;
    case 1:Blynk.virtualWrite(V1, HIGH); break;
    case 2:Blynk.virtualWrite(V2, HIGH); break;
    case 3:Blynk.virtualWrite(V3, HIGH); break;    
}         // Sync the Blynk button widget state
}
void lightOff(int pin) {
    
    digitalWrite(Relays[0][pin],HIGH);
    DeviceState[pin] = 0;
  switch(pin){
    case 0:Blynk.virtualWrite(V0, LOW); break;
    case 1:Blynk.virtualWrite(V1, LOW); break;
    case 2:Blynk.virtualWrite(V2, LOW); break;
    case 3:Blynk.virtualWrite(V3, LOW); break;    
}         // Sync the Blynk button widget state
}

void ACON(){
    DeviceState[4] = 1;
    ACTEMP();
    ACONFANOFF();
    Blynk.virtualWrite(V4, HIGH);
    }
void ACOFF(){
   irsend.sendCOOLIX(0xB27BE0, 24);
   DeviceState[4] = 0;
   ACONFANOFF();
   Blynk.virtualWrite(V4, LOW); 
}
BLYNK_CONNECTED() {
    Blynk.syncVirtual(V0, V1, V2, V3, V30 ,V4);
    bridge1.setAuthToken("AUTH DEVICE 2"); // Token of the hardware B
     rtc.begin();
  terminal.clear();
  terminal.println("DEVICE CONNECTED");
  terminal.print("connected to: ") ;terminal.println(WiFi.SSID());
  terminal.print("local ip: ") ;terminal.println(WiFi.localIP());
  terminal.flush();
}
BLYNK_WRITE(V0){
  int SwitchStatus = param.asInt();
  if (SwitchStatus == 1){lightOn(0); // Relay ON
  Serial.println("Blynk Virtual Pin V0 state Changed To HIGH ");
  }else if (SwitchStatus == 0) {lightOff(0); // Relay OFF
  Serial.println("Blynk Virtual Pin V0 state Changed To LOW ");
  }
}BLYNK_WRITE(V1){
  int SwitchStatus = param.asInt();
  if (SwitchStatus == 1){lightOn(1);  // Relay ON
  Serial.println("Blynk Virtual Pin V1 state Changed To HIGH ");
  } else if (SwitchStatus == 0) { lightOff(1); // Relay OFF
  Serial.println("Blynk Virtual Pin V1 state Changed To LOW ");
  }
}BLYNK_WRITE(V2){
  int SwitchStatus = param.asInt();
  if (SwitchStatus == 1) { lightOn(2); // Relay ON
  Serial.println("Blynk Virtual Pin V2 state Changed To HIGH ");
  }else if (SwitchStatus == 0) {  lightOff(2); // Relay OFF
  Serial.println("Blynk Virtual Pin V2 state Changed To LOW ");
  }
}BLYNK_WRITE(V3){
  int SwitchStatus = param.asInt();
  if (SwitchStatus == 1){ lightOn(3); // Relay ON
  Serial.println("Blynk Virtual Pin V3 state Changed To HIGH ");
  }else if (SwitchStatus == 0) { lightOff(3); // Relay OFF
  Serial.println("Blynk Virtual Pin V3 state Changed To LOW ");
  }
}BLYNK_WRITE(V4){
  int SwitchStatus = param.asInt();
   if (SwitchStatus == 1) {ACON();
   Serial.println("Blynk Virtual Pin V4 state Changed To HIGH");
  } else if (SwitchStatus == 0) {ACOFF();}}
void ACTEMP(){
  AcR = AcFanSpd;
  AcC = (AcTemp - 17);
   if ((DeviceState[4]) == 1){
irsend.sendCOOLIX(AcData[AcR][AcC], 24);  
Blynk.virtualWrite(V21,AcTemp);
switch(AcFanSpd){
 case 0:Blynk.virtualWrite(V22,"AUTO");  break;
 case 1:Blynk.virtualWrite(V22,"LOW");   break;
 case 2:Blynk.virtualWrite(V22,"MEDIUM");break;
 case 3:Blynk.virtualWrite(V22,"HIGH");  break; 
}
    }
}
 BLYNK_WRITE(V5){
  AcTemp = param.asInt();
  ACTEMP();
  ACONFANOFF();
 }
BLYNK_WRITE(V6){
  AcFanSpd = param.asInt();
  ACTEMP();
  ACONFANOFF();
}
BLYNK_WRITE(V9){
  if (param.asInt() == 1){
    WiFiManager wifi;
wifi.startConfigPortal("BLYNK-REQUESTED-CONFIG");  }
  else{
    //DO NOTHING
  }
}
BLYNK_WRITE(V30){AcOnFanOff  = param.asInt();
ACONFANOFF();}
BLYNK_WRITE(V11) {                //ON TIMER 1
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
On_timer[0][0] = total_minutes/60;
On_timer[0][1] = total_minutes %60;
On_timer[0][2] = startTimeInSecs &60;
terminal.print((On_timer[0][0]));
terminal.print(":");
terminal.print(On_timer[0][1]);
terminal.print(":");
terminal.println(On_timer[0][2]);
terminal.flush();
 }
BLYNK_WRITE(V12) {                //OFF TIMER 1
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
Off_timer[0][0] = total_minutes/60;
Off_timer[0][1] = total_minutes %60;
Off_timer[0][2] = startTimeInSecs % 60;
}
BLYNK_WRITE(V13) {                //ON TIMER 2
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
On_timer[1][0] = total_minutes/60;
On_timer[1][1] = total_minutes %60;
On_timer[1][2] = startTimeInSecs &60;
}
BLYNK_WRITE(V14) {               //OFF TIMER 2
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
Off_timer[1][0] = total_minutes/60;
Off_timer[1][1] = total_minutes %60;
Off_timer[1][2] = startTimeInSecs % 60;
} 

void ACONFANOFF()
{if (AcOnFanOff == 1 && DeviceState[4] == 1){
  lightOff(0);
  }else if(AcOnFanOff == 1 && DeviceState[4] == 0){
     lightOn(0);
  }
}


void SwitchState(){  
  for(int i=0;i<4;i++){
  boolean state = (digitalRead(Switch[0][i]));
  
  if ((state) == LOW ){
      Counter[i]++;
      SwitchReset[i] = false;
      
}else if ((state) == HIGH && (SwitchReset[i]) == false){
  if(Counter[i] < 25){//SHORT PRESS
   if ((DeviceState[i]) == 0){
     if(i == 0){lightOn(0); //FAN
     }else if(i == 1){lightOn(1); //MEDIA CENTER
     }else if(i == 2){lightOn(2);bridge1.virtualWrite(V2, 100);bridge1.virtualWrite(V0, 1023); //LIGHT
     }else if(i == 3){ACON();
     } 
   }else{
     if(i == 0){lightOff(0);//FAN
     }else if(i == 1){lightOff(1);// MEDIA CENTER
     }else if(i == 2){lightOff(2);bridge1.virtualWrite(V2, 0);bridge1.virtualWrite(V0, 0); //LIGHT
     }else if(i == 3){ACOFF();
     }
  }}else{
    ACOFF();// lONG PRESS
        }
       Counter[i] = 0;
    // reset flag the physical button release
    SwitchReset[i] = true;
  }
}
}

void clockDisplay(){
  RTC_HOUR = hour();
   if ( RTC_HOUR > 12 ) {
    RTC_HOUR = RTC_HOUR - 12;
    String currentTime = String(RTC_HOUR) + ":" + minute() + ":" + second() + " PM";
    Blynk.virtualWrite(V10, currentTime);  // Send PM time to the App
  } else {
    String currentTime = String(hour()) + ":" + minute() + ":" + second() + " AM"; 
    Blynk.virtualWrite(V10, currentTime);  // Send AM time to the App
  }
  String currentDate = String(day()) + "/" + month() + "/" + year();
  Blynk.virtualWrite(V31, currentDate);  // Send date to the App
if(On_timer[0][0] == hour() && On_timer[0][1] == minute()&& On_timer[0][2] == second()){
  ACON();
}else if (On_timer[1][0] == hour() && On_timer[1][1] == minute() && On_timer[1][2] == second()){
  ACON();
}else if (Off_timer[0][0] == hour() && Off_timer[0][1] == minute() && Off_timer[0][2] == second()){
  ACOFF(); 
}else if (Off_timer[1][0] == hour() && Off_timer[1][1] == minute() && Off_timer[1][2] == second()){
  ACOFF();
}
  }
BLYNK_WRITE(V25){
  String Read = param.asStr();
if(Read == String("Clear") || Read == String("clear") || Read == String("cls")){
  terminal.clear();
}else if(Read == String("status") || Read == String("Status")){
  terminal.print("connected to: ") ;terminal.println(WiFi.SSID());
  terminal.print("local ip: ") ;terminal.println(WiFi.localIP());
  
}
terminal.flush();  
}
2 Likes

@Sai_Khurana
can I have your QR code.
Thanks

Hey @jasperdog

Just out of curiosity, are you using the early code, based on the “WemoManager” library? If so, try the newer version that uses the “espalexa” library. My devices that use the newer code seem to work just fine with all aspects of Alexa, including routines. You can find the newer version above, in my April 2, 2018 post.

i am using the WemoManager library, i’ll try your suggested library now.

it works @chrome1000 , thank you, i notice now that the device is a light and not a switch and cannot be edited as a switch unlike the wemomanager option, it makes no difference to the operation of the sonoff though. The routine works as does the Blynk side too. Many thanks again.

1 Like

I’m glad we got that resolved. The real beauty of using the espalexa version is that Alexa will accept both ON/OFF and analog level commands. You can ask her to “turn on the fan,” or you can also ask her to “turn the fan on 25%.”

1 Like

@chrome1000 i just tried reflashing a second device (different blynk token). alexa discovered it but lost the first device, any ideas?
is it to do with the following line needs changing other than the obvious “bedroom” name?

espalexa.addDevice("Bedroom", UpdateBedroom); //Parameters: (device name, callback function).

both devices work in blynk

pin is
const int OutputPin = 12;

@chrome1000 so in conclusion, it appears i can only have one instance of a device using the espalexa sketch, i can even turn one device off and the other appears and vice versa but not both together, different tokens used, different names used?

@jasperdog I’ll try to figure it out.

thanks @chrome1000
just to be clearer, i don’t use wifi manager in my sketch and i have my own local Blynk server, so my sketch is a bit more adapted than the espalaxa one that you showed. although everything bar the wifi manager exists in my sketch.

@jasperdog Great news! You just need to update your espalexa library. I just tested my system with 3 devices running concurrently. Here’s the link:

great i will try it now, just for clarity, do i need to change this line name for each device?

I’m not sure. Just for clarity with my own tests, I did give each device a different name.

ok my first attempt is in progress, will update you in about 15 minutes

first device reflashed and found as bulb in Alexa App, just reflashing second device.using different name.

@chrome1000 unfortunately it didn’t work for me, in fact the second device did not come online in Alexa or Blynk, not sure why but was too tired to continue, will explore later today.

@chrome1000 i also found that using the new library, does not sync/update the buttons in the Blynk app. i only managed to connect one device to Alexa and Blynk but still no sync with buttons, i didn’t modify my sketch at all from using the old espalexa library, so i can only imagine that these issues pertain to the new library, i think i’ve exhausted all testing now and will revert back to using the original WemoManager library and sketch, all be it without being able to create routines, but it’s no big deal because it’s just as easy to say “Alexa” and whatever you want. Many thanks for your continued help, it is appreciated.