Controlling an IoT Yunshan ESP8266 WIFI Network Relay via an Amazon Echo/Alexa

shoulders of giants

Easy-Peasy, if you’re standing on the shoulders of giants! Here’s the demo code:

/*************************************************************************
 * Title: Simple ESP-8266 Amazon Echo/yunshan wifi relay control demo
 * File: esp8266_yunshan_echo_demo.ino
 * Author: James Eli
 * Date: 1/15/2017
 *
 * This program controls a Yunshan wifi relay module communicating through 
 * an amazon echo to the onboard esp-8266-12e module. 
 * 
 * Notes:
 *  (1) Requires the following arduino libraries:
 *      ESP8266
 *      ESPAsyncTCP
 *      FauxMoESP
 *  (2) Compiled with arduino ide 1.8.1
 *  (3) As of 11/27/16 the ESP8266 release core v2.3.0 did not have the right lwip 
 *      code. If you get a complaint about udp_set_multicast_ttl not being defined, 
 *      you'll need to uninstall the ESP8266 board support, then manually install 
 *      the most recent core from https://github.com/esp8266/arduino. Essentialy 
 *      you'll need to git clone https://github.com/esp8266/Arduino.git into your 
 *      Arduino sketchbook folder under hardware/esp8266com/esp8266 and then in a 
 *      terminal shell in hardware/esp8266com/esp8266/tools run python get.py
 *************************************************************************
 * Change Log:
 *   1/15/2017: Initial release. JME
 *************************************************************************/
#include 
#include 
#include "fauxmoESP.h"

// Esp8266 pins.
#define ESP8266_GPIO2    2 // Blue LED.
#define ESP8266_GPIO4    4 // Relay control.
#define ESP8266_GPIO5    5 // Optocoupler input.
#define LED_PIN          ESP8266_GPIO2
#define WIFI_SSID       "…"
#define WIFI_PASS       "…"
#define SERIAL_BAUDRATE 9600
fauxmoESP fauxmo;

// Wifi setup
void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode( WIFI_STA );
  // Connect
  Serial.printf( "[WIFI] Connecting to %s ", WIFI_SSID );
  WiFi.begin( WIFI_SSID, WIFI_PASS );
  // Wait
  while ( WiFi.status() != WL_CONNECTED ) {
    Serial.print( "." );
    delay( 100 );
  }
  Serial.println();
  // Connected!
  Serial.printf( "[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str() );
}

void callback( uint8_t device_id, const char * device_name, bool state ) {
  Serial.print( "Device " ); 
  Serial.print( device_name ); 
  Serial.print( " state: " );
  if ( state ) {
    digitalWrite( ESP8266_GPIO4, 1 ); // Relay control pin on.
    Serial.println( "ON" );
  } else  {
    digitalWrite( ESP8266_GPIO4, 0 ); // Relay control pin off.
    Serial.println( "OFF" );
  }
}

void setup() {
  // Init serial port and clean garbage
  Serial.begin( SERIAL_BAUDRATE );
  Serial.println( "FauxMo demo sketch" );
  Serial.println( "After connection, ask Alexa/Echo to 'turn  on' or 'off'" );
  pinMode( ESP8266_GPIO4, OUTPUT );       // Relay control pin.
  pinMode( ESP8266_GPIO5, INPUT_PULLUP ); // Input pin.
  pinMode( LED_PIN, OUTPUT );             // ESP8266 module blue LED.
  digitalWrite( LED_PIN, LOW );           // Turn on LED.
  // Wifi
  wifiSetup();
  digitalWrite( LED_PIN, HIGH );          // Turn off LED.
  // Fauxmo
  fauxmo.addDevice( "relay" );
  fauxmo.onMessage( callback );
}

void loop() {
  fauxmo.handle();
}

Details

The following libraries are required:
1. ESPAsyncTCP library
2. FauxMoESP library (to download, select download repository)

References

Emulate a WeMo device with ESP8266
Easy Alexa (or Echo) Control of your ESP8266 Huzzah

Advertisements

About Jim Eli

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

One Response to Controlling an IoT Yunshan ESP8266 WIFI Network Relay via an Amazon Echo/Alexa

  1. surya kant says:

    sir, can you please tell me what does this do?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s