ESP8266 WiFi Amazon Alexa Voice control Loxone

Amazon Voice Control


Functional Description:

  • Amazon Alexa Dot and NodeMCU work in one LAN network where they can "see".
  • The NodeMCU holds a keep-alive server on various UDP virtual ports, where each output control works. You can use 4 on / off callbacks (4 for ON, 4 for OFF).
  • One port handles one output that can be controlled by power on / off.
  • Amazon Alexa Dot can search for UDP ports on the network and can also issue commands to control them based on a command from a user in English with the appropriate parameter
  • You need to say: "Alexa, Find devices" to find devices
  •       
  • Alexa answers after a while: "The Discovery is complete, I found one smart device." - this is to verify that the NodeMCU (ESP8266) is visible in the LAN.
  •       
  • Use one of the following demo commands to control the relay: "Alexa, Turn relay one on" , "Alexa, Turn relay one off" and similarly for other outputs
  • Used hardware:

    Amazon Alexa Dot (versions 1 and 2 are compatible) Amazon Alexa Dot
  • ESP8266 / NodeMCU (v1/v2/v3) ESP8266
  • 1+ electromagnetic / SSR channel relay Elmg. relé
  • exploitation:

  • Control the lights and outputs directly from the callback
  • Control other systems (Loxone and others that support HTTP authentication and GET / POST request)
  • Request for web service
  • Libraries: Github
          
  • The ESP8266 sample DEMO programs can control one output according to the voice command above.
  • ESP8266 sketch - NodeMCU + Loxone HTTP request - ON/OFF

    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <WiFiUdp.h>
    #include <functional>
    #include "Switch.h"
    #include "UpnpBroadcastResponder.h"
    #include "CallbackFunction.h"
    #include <ESP8266HTTPClient.h>
    const char *ssid =  "meno_wifi";
    const char *password =  "heslo_wifi";
    boolean connectWifi();
    void Relay1On();
    void Relay1Off();
    boolean wifiConnected = false;
    int httpCode = 0;
    UpnpBroadcastResponder upnpBroadcastResponder;
    Switch *relay1 = NULL;
    HTTPClient http;
    void setup()
    {
      Serial.begin(9600);
      wifiConnected = connectWifi();
      if (wifiConnected) {
        upnpBroadcastResponder.beginUdpMulticast();
        relay1 = new Switch("Relay 1", 80, Relay1On, Relay1Off);
        Serial.println("Adding switches upnp broadcast responder");
        upnpBroadcastResponder.addDevice(*relay1);
      }
    }
    void loop()
    {
      if (wifiConnected) {
        upnpBroadcastResponder.serverLoop();
        relay1->serverLoop();
      }
    }
    
    void Relay1On() {
      Serial.println("Zapinam switch 1 na povel od Alexa Dot");
      String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
      http.begin(url);
      http.setAuthorization("admin", "0285");
      httpCode = http.GET();
      if (httpCode == HTTP_CODE_OK) {
    
      }
      http.end();
    }
    
    void Relay1Off() {
      Serial.println("Vypinam switch 1 na povel od Alexa Dot");
      String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
      http.begin(url);
      http.setAuthorization("admin", "0285");
      httpCode = http.GET();
      if (httpCode == HTTP_CODE_OK) {
    
      }
      http.end();
    }
    
    boolean connectWifi() {
      boolean state = true;
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      Serial.println("");
      Serial.println("Pripajanie na wifi");
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      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;
    }
          

    ESP8266 sketch - NodeMCU + Web HTTP request - ON/OFF

    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <WiFiUdp.h>
    #include <functional>
    #include "Switch.h"
    #include "UpnpBroadcastResponder.h"
    #include "CallbackFunction.h"
    #include <ESP8266HTTPClient.h>
    const char *ssid =  "meno_wifi";
    const char *password =  "heslo_wifi";
    boolean connectWifi();
    void Relay1On();
    void Relay1Off();
    boolean wifiConnected = false;
    int httpCode = 0;
    UpnpBroadcastResponder upnpBroadcastResponder;
    Switch *relay1 = NULL;
    HTTPClient http;
    void setup()
    {
      Serial.begin(9600);
      wifiConnected = connectWifi();
      if (wifiConnected) {
        upnpBroadcastResponder.beginUdpMulticast();
        relay1 = new Switch("Relay 1", 80, Relay1On, Relay1Off);
        Serial.println("Adding switches upnp broadcast responder");
        upnpBroadcastResponder.addDevice(*relay1);
      }
    }
    void loop()
    {
      if (wifiConnected) {
        upnpBroadcastResponder.serverLoop();
        relay1->serverLoop();
      }
    }
    
    void Relay1On() {
      Serial.println("Zapinam switch 1 na povel od Alexa Dot");
      String url = "http:/web.sk/zapnirele.php";
      http.begin(url);
      httpCode = http.GET();
      if (httpCode == HTTP_CODE_OK) {
    
      }
      http.end();
    }
    
    void Relay1Off() {
      Serial.println("Vypinam switch 1 na povel od Alexa Dot");
      String url = "http:/web.sk/vypnirele.php";
      http.begin(url);
      httpCode = http.GET();
      if (httpCode == HTTP_CODE_OK) {
    
      }
      http.end();
    }
    
    boolean connectWifi() {
      boolean state = true;
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      Serial.println("");
      Serial.println("Pripajanie na wifi");
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      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;
    }