MQTT switch using Alexa Echo Dot

Ever wanted to control your ESP8266’s through Alexa and Echo Dot and build fancy shit?

fauxmoESP emulates the protocol used by Wemo IoT devices. Later versions emulate Philips Hue protocol and allow numeric values and allow something like „set value to 42“. Unfortunately 3.0.0 is not compatible with Echo Dot 2nd and 3rd generation.

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <fauxmoESP.h>
#include "ESP8266HTTPClient.h"
#include "ESP8266httpUpdate.h"

const char* FIRMWARE_VERSION = "1.0";
const char* ssid     = "_REMOVED_";
const char* password = "_REMOVED_";
const char* mqtt_server = "mqtt";

bool currentState = false;
char clientRSSI[50];
void mqttCallback(char* topic, byte* payload, unsigned int length);

fauxmoESP fauxmo;
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, mqttCallback, espClient);

void mqttCallback(char* topic, byte* payload, unsigned int length) {
}

void setup() {
  WiFi.mode(WIFI_STA);
  if(WiFi.status() != WL_CONNECTED) {
    reconnectWifi();
  }
 
  if (!client.connected()) {
    reconnectMQTT();
  }
  
  fauxmo.addDevice("Aussenlicht");
  fauxmo.enable(true);

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state) {
    currentState = state;

    // set any GPIO pin here
        
    if(state)
      client.publish("sensors/wemo/status", "ON");
    else
      client.publish("sensors/wemo/status", "OFF");
  });
    
  fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
    return currentState;
  });
}

void loop() {

  WiFi.mode(WIFI_STA);
  if(WiFi.status() != WL_CONNECTED)
    reconnectWifi();
 
  if (!client.connected())
    reconnectMQTT();
  
  fauxmo.handle();
  client.loop();
  delay(0);
}

void reconnectWifi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  
  String str = String(WiFi.RSSI());
  str.toCharArray(clientRSSI, str.length()+1);
}

void reconnectMQTT() {  
  while (!client.connected()) {   
    if (client.connect("wemo","sensors/wemo/health", 0, true, "offline")) {
      client.publish("sensors/wemo/rssi", clientRSSI);  
      client.publish("sensors/wemo", "started");
      client.publish("sensors/wemo/health", "online", true);
      client.publish("sensors/wemo/version", FIRMWARE_VERSION);
    } else
      delay(1000);
  }
}

 

After ESP8266 connects to your wifi you will see its published MQTT messages.

 

Next search for new IoT devices in Alexa App. Our fake Wemo devices will show up.

 

Neat! We have an MQTT enabled IoT device which can be controlled locally with Echo Dot.

 

Now the fancy shit starts. You can easily do things like:

  • Alexa, jetzt ist Kinozeit – roll down the canvas, the projector and trigger FireTV stick and use ESP as Alexa-to-GPIO bridge
  • Alexa, <do this, do that> and trigger other MQTT devices in your SmartHome, hence use the ESP only as an Alexa-to-MQTT gateway

Next steps:

  • This thing should be extended by the remote flashing capability and maybe a remote configuration for providing IoT device names more dynamically 

Have fun.