Aircoookie / Espalexa

Alexa voice control for ESP8266/ESP32 (including brightness and color!)
MIT License
539 stars 135 forks source link

Web button does not communicate with alexa #136

Open AppNetDeveloper opened 3 years ago

AppNetDeveloper commented 3 years ago

Hi everyone. Thanks for this wonderful job. The truth is that I was impressed with this whole project, I have never used an arduino until now and it is the first time that I enter and it is a beautiful world.

I have a problem with an alexa voice control unification part and with web control. If you turn on the web a relay does not appear in alexa as turned on, and I looked and understand that not turning on the web does not warn alexa, and I tried something that I found, but it gives me an error.

What am I doing wrong? Thank you.

` 
#define ESPALEXA_ASYNC //it is important to define this before #include <Espalexa.h>!
#include <Espalexa.h>

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#else
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>

// define the GPIO connected with Relays
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 13 //D7
#define RelayPin4 14 //D5
#define RelayPin5 12 //D6

// prototypes
boolean connectWifi();

//callback functions
void primeraluz(uint8_t brightness);
void segundaluz(uint8_t brightness);
void terceraluz(uint8_t brightness);
void cuartaluz(uint8_t brightness);
void quintaluz(uint8_t brightness);

// Change this!!
const char* ssid = "Ljjjsjjjjs";
const char* password = "cvljkjjj";

// device names
String Device_1_Name = "Room light";
String Device_2_Name = "Blue bulb";
String Device_3_Name = "Yellow bulb";
String Device_4_Name = "Red bulb";
String Device_5_Name = "CFL bulb";

boolean wifiConnected = false;

Espalexa espalexa;
AsyncWebServer server(80);

void setup()
{
  Serial.begin(115200);

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);

  // Initialise wifi connection
  wifiConnected = connectWifi();

  if(wifiConnected){
    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
      request->send(200, "text/plain", "This is an example index page your server may send.");
    });
    server.on("/test", HTTP_GET, [](AsyncWebServerRequest *request){
      request->send(200, "text/plain", "This is a second subpage you may have.");
    });
    server.on("/remote", HTTP_GET, [](AsyncWebServerRequest *request){
      int pin_light = 13;
      int status = digitalRead(pin_light);
      if(status>=1){
        digitalWrite(RelayPin3, LOW);
      Serial.println("Device3 OFF");

      terceraluz -> setValue(255); // Tell Alexa that the device is ON
      digitalWrite(terceraluz , HIGH); // Actually turn the device ON

      request->send(200, "text/plain", "3 OFF.");
        }else{
          digitalWrite(RelayPin3, HIGH);
      Serial.println("Device3 ON");
      request->send(200, "text/plain", "3 ON.");
          }

    });
    server.on("/remoteoff", HTTP_GET, [](AsyncWebServerRequest *request){
       digitalWrite(RelayPin3, LOW);
      Serial.println("Device3 OFF");
      request->send(200, "text/plain", "3 OFF.");
    });
    server.onNotFound([](AsyncWebServerRequest *request){
      if (!espalexa.handleAlexaApiCall(request)) //if you don't know the URI, ask espalexa whether it is an Alexa control request
      {
        //whatever you want to do with 404s
        request->send(404, "text/plain", "Not found");
      }
    });

    // Define your devices here.
    espalexa.addDevice("My Light 1", primeraluz); //simplest definition, default state off
    // Define your devices here.
    espalexa.addDevice("My Light 2", segundaluz); //simplest definition, default state off
     // Define your devices here.
    espalexa.addDevice("My Light 3", terceraluz); //simplest definition, default state off
    // Define your devices here.
    espalexa.addDevice("My Light 4", cuartaluz); //simplest definition, default state off
    // Define your devices here.
    espalexa.addDevice("My Light 5", quintaluz); //simplest definition, default state off

    espalexa.begin(&server); //give espalexa a pointer to your server object so it can use your server instead of creating its own
    //server.begin(); //omit this since it will be done by espalexa.begin(&server)
  } else
  {
    while (1)
    {
      Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
      delay(2500);
    }
  }
}

void loop()
{
   espalexa.loop();
   delay(1);
}

//our callback functions
void primeraluz(uint8_t brightness) {
    Serial.print("Device 1 changed to ");

    //do what you need to do here

    //EXAMPLE
    if (brightness == 255) {
       digitalWrite(RelayPin1, HIGH);
      Serial.println("Device1 ON");
    }
    else if (brightness == 0) {
      digitalWrite(RelayPin1, LOW);
    Serial.println("Device1 OFF");
    }
    else {
      Serial.print("DIM "); Serial.println(brightness);
    }
}

//our callback functions
void segundaluz(uint8_t brightness) {
    Serial.print("Device 2 changed to ");

    //do what you need to do here

    //EXAMPLE
    if (brightness == 255) {
       digitalWrite(RelayPin2, HIGH);
      Serial.println("Device1 ON");
    }
    else if (brightness == 0) {
      digitalWrite(RelayPin2, LOW);
    Serial.println("Device1 OFF");
    }
    else {
      Serial.print("DIM "); Serial.println(brightness);
    }
}

//our callback functions
void terceraluz(uint8_t brightness) {
    Serial.print("Device 3 changed to ");

    //do what you need to do here

    //EXAMPLE
    if (brightness == 255) {
       digitalWrite(RelayPin3, HIGH);
      Serial.println("Device1 ON");
    }
    else if (brightness == 0) {
      digitalWrite(RelayPin3, LOW);
    Serial.println("Device1 OFF");
    }
    else {
      Serial.print("DIM "); Serial.println(brightness);
    }
}

//our callback functions
void cuartaluz(uint8_t brightness) {
    Serial.print("Device 4 changed to ");

    //do what you need to do here

    //EXAMPLE
    if (brightness == 255) {
       digitalWrite(RelayPin4, HIGH);
      Serial.println("Device1 ON");
    }
    else if (brightness == 0) {
      digitalWrite(RelayPin4, LOW);
    Serial.println("Device1 OFF");
    }
    else {
      Serial.print("DIM "); Serial.println(brightness);
    }
}

//our callback functions
void quintaluz(uint8_t brightness) {
    Serial.print("Device 5 changed to ");

    //do what you need to do here

    //EXAMPLE
    if (brightness == 255) {
       digitalWrite(RelayPin5, HIGH);
      Serial.println("Device1 ON");
    }
    else if (brightness == 0) {
      digitalWrite(RelayPin5, LOW);
    Serial.println("Device1 OFF");
    }
    else {
      Serial.print("DIM "); Serial.println(brightness);
    }
}

// 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 > 20){
      state = false; break;
    }
    i++;
  }
  Serial.println("");
  if (state){
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed.");
  }
  delay(100);
  return state;
}`

My problem is here, something I can not do well or is not correct.

`server.on("/remote", HTTP_GET, [](AsyncWebServerRequest *request){
      int pin_light = 13;
      int status = digitalRead(pin_light);
      if(status>=1){
        digitalWrite(RelayPin3, LOW);
      Serial.println("Device3 OFF");

      terceraluz -> setValue(255); // Tell Alexa that the device is ON
      digitalWrite(terceraluz , HIGH); // Actually turn the device ON

      request->send(200, "text/plain", "3 OFF.");
        }else{
          digitalWrite(RelayPin3, HIGH);
      Serial.println("Device3 ON");
      request->send(200, "text/plain", "3 ON.");
          }

    });`
Aircoookie commented 3 years ago

Hi! I do not claim to fully comprehend your code, however digitalWrite(terceraluz , HIGH); is definitely wrong. terceraluz is the EspalexaDevice, not the pin number. You probably want digitalWrite(RelayPin3, HIGH); instead

AppNetDeveloper commented 3 years ago

¡Hola! No pretendo comprender completamente su código, sin embargo, digitalWrite(terceraluz , HIGH);definitivamente está mal. terceraluzes el EspalexaDevice, no el número de PIN. Probablemente quieras en su digitalWrite(RelayPin3, HIGH);lugar

RelayPin3 -> setValue(255); // Tell Alexa that the device is ON I think you were right, but now I have another problem.

` No usado: C:\Users\super\OneDrive\Documents\Arduino\libraries\Espalexa-2.4.7
exit status 1
base operand of '->' is not a pointer
`
AppNetDeveloper commented 3 years ago

Do you have any idea how I can solve it?

mrhill commented 3 years ago

RelayPin3 is not a pointer