Blynk & Amazon Echo (Alexa) Controlled Lights via Smart Wall Switch

Works like a charm. See this post for a discussion about reverse-engineering the wifi smart wall switch and how to program it. The Blynk application uploaded to the Lyasi smart switch is below.

/*************************************************************************
 * Title: Simple ESP-8266 Amazon Echo/sonoff wifi relay control
 * File: sonoff_echo.ino
 * Author: James Eli
 * Date: 5/1/2017
 *
 * For ESP8266 #5, "kitchen".
 * Uses Blynk virtual buttons:
 *   V0 button as switch
 *   V1 an indicator LED
 *   V2 on/off timer.
 *
 * This program controls an esp-8285 wifi switch/relay module communicating
 * either through an amazon echo or the Blynk android application to the
 * onboard esp-8285 module. Amazon Echo responds to ECHO_KEY_WORD.
 *************************************************************************
 * Change Log:
 *   1/26/2017: Initial release. JME
 *   5/12/2017: Modified for Vail house. JME
 *   6/11/2017: Added support for Blynk timer widget. JME
 *************************************************************************/
#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"

bool isFirstConnect = true;       // Flag for re-sync on connection.
int relayState = LOW;             // Blynk app pushbutton status.

// Blynk app authentication code goes here.
char auth[] = "...";

boolean SwitchReset = true;       // Flag indicating that the hardware button
                                  // has been released
// Esp8285 pins.
#define ESP8266_GPIO13 13         // blue LED (LOW == ON).
#define ESP8266_GPIO0   0         // proximity switching.
#define ESP8266_GPIO12 12         // relay (HIGH == ON).
const int RELAY = ESP8266_GPIO12; // Relay switching pin.
const int LED = ESP8266_GPIO13;   // On/Off indicator LED.
const int SWITCH = ESP8266_GPIO0; // Proximity switch.

// Amazon echo response term & Wifi access point name.
#define ECHO_KEY_WORD "kitchen"

WemoManager wemoManager;
WemoSwitch *device = NULL;
SimpleTimer timer;

void setup() {
  WiFiManager wifi;               // WiFiManager intialization.

  pinMode( RELAY, OUTPUT );       // Init pin modes.
  pinMode( LED, OUTPUT );
  pinMode( SWITCH, INPUT_PULLUP );

  delay( 10 );

  digitalWrite( RELAY, LOW );        // Turn relay off.
  digitalWrite( LED, LOW );          // Turn status LED on.
  wifi.autoConnect( ECHO_KEY_WORD ); // Create AP, if necessary
  wemoManager.begin();

  WiFi.mode( WIFI_STA );          // Set WIFI module to STA mode

  // Format: Alexa invocation name, local port no, on callback, off callback
  device = new WemoSwitch( ECHO_KEY_WORD, 80, RelayOn, RelayOff );
  wemoManager.addDevice( *device );
  Blynk.config( auth );
  //ArduinoOTA.begin();
  timer.setInterval( 100, ButtonCheck );
  digitalWrite( LED, HIGH );
}

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

// Toggle the relay on
void RelayOn() {
  digitalWrite( RELAY, HIGH );
  relayState = true;
  Blynk.virtualWrite( V0, HIGH ); // Sync the Blynk button widget state
  Blynk.virtualWrite( V1, relayState*255 );
}

// Toggle the relay off
void RelayOff() {
  digitalWrite( RELAY, LOW );
  relayState = false;
  Blynk.virtualWrite( V0, LOW ); // Sync the Blynk button widget state
  Blynk.virtualWrite( V1, relayState*255 );
}

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

  if ( SwitchStatus )
    RelayOn();
  else
    RelayOff();
}

// Handle timer changes originating from the Blynk app.
BLYNK_WRITE( V2 ) {
  int SwitchStatus = param.asInt();

  if ( SwitchStatus )
    RelayOn();
  else
    RelayOff();
}

// This function runs every time Blynk connection is established.
BLYNK_CONNECTED() {
  if ( isFirstConnect ) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

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

  // toggle the switch if there's a new button press
  if ( !SwitchState && SwitchReset == true ) {
    if ( relayState )
      RelayOff();
    else
      RelayOn();

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

About Jim Eli

µC experimenter
This entry was posted in arduino, iot and tagged , , . Bookmark the permalink.

2 Responses to Blynk & Amazon Echo (Alexa) Controlled Lights via Smart Wall Switch

  1. Kareem Jay says:

    Hi Jim, love your posts! wanted to see if its possible to get some consultation on a project I’m working on. Just trying to better understand this. Let me know!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s