ESP8266-12 - use with Alexa
d. bodnar 11-28-2016

 

Objective - program the ESP8266 to operate with Amazon's Alexa - it should be able to operate multiple devices

 

Using the modules from DX  http://www.dx.com/p/esp-12-esp8266-serial-wifi-wireless-module-w-pcb-antenna-adapter-board-for-arduino-raspberry-pi-379296 Need to remove 0 ohm resistor in center to use voltage regulator
 
Needed to do this first to get it ready for programming

http://www.instructables.com/id/Get-Your-ESP8266-12-Ready-for-AT-Commands/?ALLSTEPS

Then programmed using the code from here:

https://github.com/kakopappa/arduino-esp8266-alexa-multiple-wemo-switch  - had pin 0 jumpered to ground to program

 
To program connect GPIO0 (zero) to ground and program with Arduino IDE - might need to repeat after a hard reset - allow GPIO zero to float after - should see something like this:
`s
Connecting to WiFi...
Connecting ...............
Connection failed.
I%_1
Connecting to WiFi...
Connecting ............
Connected to main2
IP address: 192.168.1.33
Begin multicast ..
Udp multicast server started at 239.255.255.250:1900
WebServer started on port: 
80
WebServer started on port: 
81
WebServer started on port: 
82
WebServer started on port: 
83
Adding switches upnp broadcast responder
Adding switch : basement lights index : 0
Adding switch : garage lights index : 1
Adding switch : bench lights index : 2
Adding switch : desk lights index : 3

 

This code was modified to support four LEDs called
  • basement lights
  • garage lights
  • bench lights
  • desk lights

Very cool!

// working d. bodnar 11-27-2016 with program (pin 0) floating - stayed on overnight
//  REMEMBER to jumper pin "0" to ground when programming
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
int LED4 = 4;
int LED5 = 5;
int LED14 = 14; // 3rd LED for testing
int LED12 = 12; // 4th LED for testing

// prototypes
boolean connectWifi();

//on/off callbacks
void basementLightsOn();
void basementLightsOff();
void garageLightsOn();
void garageLightsOff();
void benchLightsOn();
void benchLightsOff();
void deakLightsOn();
void deskLightsOff();



// Change this before you flash
const char* ssid = "main2";
const char* password = "*******8";

boolean wifiConnected = false;

UpnpBroadcastResponder upnpBroadcastResponder;

Switch *basement = NULL;
Switch *garage = NULL;
Switch *bench = NULL;
Switch *desk = NULL;

void setup()
{
  pinMode(LED5, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED14, OUTPUT);
  pinMode(LED12, OUTPUT);
  Serial.begin(9600);

  // Initialise wifi connection
  wifiConnected = connectWifi();

  if (wifiConnected) {
    upnpBroadcastResponder.beginUdpMulticast();

    // Define your switches here. Max 14
    // Format: Alexa invocation name, local port no, on callback, off callback
    basement = new Switch("basement lights", 80, basementLightsOn, basementLightsOff);
    garage = new Switch("garage lights", 81, garageLightsOn, garageLightsOff);
    bench = new Switch("bench lights", 82, benchLightsOn, benchLightsOff);
    desk = new Switch("desk lights", 83, deskLightsOn, deskLightsOff);


    Serial.println("Adding switches upnp broadcast responder");
    upnpBroadcastResponder.addDevice(*basement);
    upnpBroadcastResponder.addDevice(*garage);
    upnpBroadcastResponder.addDevice(*bench);
    upnpBroadcastResponder.addDevice(*desk);

  }
}

void loop()
{
  if (wifiConnected) {
    upnpBroadcastResponder.serverLoop();

    garage->serverLoop();
    basement->serverLoop();
    bench->serverLoop();
    desk->serverLoop();
  }
}

void basementLightsOn() {
  Serial.print("Switch 1 turn ON ...");
  digitalWrite(LED4, HIGH);
}

void basementLightsOff() {
  Serial.print("Switch 1 turn OFF ...");
  digitalWrite(LED4, LOW);
}

void garageLightsOn() {
  Serial.print("Switch 2 turn ON ...");
  digitalWrite(LED5, HIGH);
}

void garageLightsOff() {
  Serial.print("Switch 2 turn OFF ...");
  digitalWrite(LED5, LOW);
}

void benchLightsOn() {
  Serial.print("Switch 14 turn ON ...");
  digitalWrite(LED14, HIGH);
}

void benchLightsOff() {
  Serial.print("Switch 14 turn OFF ...");
  digitalWrite(LED14, LOW);
}

void deskLightsOn() {
  Serial.print("Switch 12 turn ON ...");
  digitalWrite(LED12, HIGH);
}

void deskLightsOff() {
  Serial.print("Switch 12 turn OFF ...");
  digitalWrite(LED12, LOW);
}



// connect to wifi  returns true if successful or false if not
boolean connectWifi() {
  boolean state = true;
  int i = 0;

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi...");

  // Wait for connection
  Serial.print("Connecting ...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 10) {
      state = false;
      break;
    }
    i++;
  }

  if (state) {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("");
    Serial.println("Connection failed.");
  }

  return state;
}