Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server is unresponsive after setState #214

Open
codaisa opened this issue Dec 27, 2022 · 0 comments
Open

Server is unresponsive after setState #214

codaisa opened this issue Dec 27, 2022 · 0 comments

Comments

@codaisa
Copy link

codaisa commented Dec 27, 2022

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant