Aircoookie / Espalexa

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

Server is unresponsive after setState #214

Open codaisa opened 1 year ago

codaisa commented 1 year ago

Hello, I'm tring to integrate Esp32 with espalexa but after i set a state to alexa i receive in app "server is unresponsive", have something wrong with this code?

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>

#define R1 15
#define R2 2
#define R3 4

// prototypes
boolean connectWifi();

//callback functions
void firstLightChanged(EspalexaDevice* dev);

// WiFi Credentials
const char* ssid = "SA NET (EXTERNO)";
const char* password = "*******";

boolean wifiConnected = false;
EspalexaDevice* coffeDevice;
Espalexa espalexa;

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

  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);

  // Initialise wifi connection
  wifiConnected = connectWifi();

  if (wifiConnected) {

    // Define your devices here.
    coffeDevice = new EspalexaDevice("Cafeteira", firstLightChanged, EspalexaDeviceType::onoff); 
    espalexa.addDevice(coffeDevice);                                                             
    espalexa.begin();

  }

  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 firstLightChanged(EspalexaDevice* d) {

  digitalWrite(R1, HIGH);
  delay(1000);
  digitalWrite(R1, LOW);

  Serial.print("A changed to ");
  Serial.print(coffeDevice->getState());
  coffeDevice->setState(false);
}

// 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.");
  }
  return state;
}