ESP-8266 esp-12e disconnecting from wifi when using amazon Alexa

I've been working on a simple project to use an esp8266 to move a relay upon command via amazon alexa(echo dot), and although my code uploads fine, when using "discover devices" on my echo dot, it kicks my esp8266 off my network.

Here is one of the many versions of the code I have found somewhere that I have been simply using to try to get my echo to connect and run a command, even if my hardware won't do anything.

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


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

// prototypes
boolean connectWifi();

//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);

// Change this!!

// WiFi Credentials
const char* ssid = "name";
const char* password = "name";

// device names
String Device_1_Name = "Office light";
String Device_2_Name = "Studio light";
String Device_3_Name = "Yellow Bulb";
String Device_4_Name = "Red bulb";

boolean wifiConnected = false;

Espalexa espalexa;

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

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

  // Initialise wifi connection
  wifiConnected = connectWifi();

  if (wifiConnected)
  {

    // Define your devices here.
    espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
    espalexa.addDevice(Device_2_Name, secondLightChanged);
    espalexa.addDevice(Device_3_Name, thirdLightChanged);
    espalexa.addDevice(Device_4_Name, fourthLightChanged);

    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(uint8_t brightness)
{
  //Control the device
  if (brightness)
  {
    if (brightness == 255)
    {
      digitalWrite(R1, HIGH);
      Serial.println("Device1 ON");
    }
    //Serial.print("ON, brightness ");
    //Serial.println(brightness);
  }
  else
  {
    digitalWrite(R1, LOW);
    Serial.println("Device1 OFF");
  }
}

void secondLightChanged(uint8_t brightness)
{

  //Control the device 
  if (brightness)
  {
    if (brightness == 255)
    {
      digitalWrite(R2, HIGH);
      Serial.println("Device2 ON");
    }
    //Serial.print("ON, brightness ");
    //Serial.println(brightness);
  }
  else
  {
    digitalWrite(R2, LOW);
    Serial.println("Device2 OFF");
  }
}

void thirdLightChanged(uint8_t brightness)
{

  //Control the device  
  if (brightness)
  {
    if (brightness == 255)
    {
      digitalWrite(R3, HIGH);
      Serial.println("Device3 ON");
    }
    //Serial.print("ON, brightness ");
    //Serial.println(brightness);
  }
  else
  {
    digitalWrite(R3, LOW);
    Serial.println("Device3 OFF");
  }
}

void fourthLightChanged(uint8_t brightness)
{

  //Control the device 
  if (brightness)
  {

    if (brightness == 255)
    {
      digitalWrite(R4, HIGH);
      Serial.println("Device4 ON");
    }
    //Serial.print("ON, brightness ");
    //Serial.println(brightness);
  }
  else
  {
    digitalWrite(R4, LOW);
    Serial.println("Device4 OFF");
  }
}

// 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;
}

I also ran ESPE Exception decoder and got the following stack outputs:

Decoding stack results
0x401004e4: malloc at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\umm_malloc\umm_malloc.c line 1668
0x4020ead8: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c line 180
0x4020ab59: _printf_common at ../../../.././newlib/libc/stdio/nano-vfprintf_i.c line 94
0x4020ead8: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c line 180
0x4020af14: _printf_i at ../../../.././newlib/libc/stdio/nano-vfprintf_i.c line 241
0x4021920a: ip4_output_if at core/ipv4/ip4.c line 761
0x4020ef38: _svfprintf_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c line 641
0x402095dc: operator delete(void*) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\abi.cpp line 54
0x40204b69: WiFiClient::write(unsigned char const*, unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src\include/ClientContext.h line 330
0x4010020c: _umm_free at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\umm_malloc\umm_malloc.c line 1295
0x4020c919: _vsnprintf_r at ../../../.././newlib/libc/stdio/vsnprintf.c line 73
0x4020ead8: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c line 180
0x4020ab59: _printf_common at ../../../.././newlib/libc/stdio/nano-vfprintf_i.c line 94
0x4021849c: etharp_output_to_arp_index at core/ipv4/etharp.c line 768
0x4020ead8: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c line 180
0x4020af14: _printf_i at ../../../.././newlib/libc/stdio/nano-vfprintf_i.c line 241
0x4020c95c: vsnprintf at ../../../.././newlib/libc/stdio/vsnprintf.c line 42
0x401004e4: malloc at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\umm_malloc\umm_malloc.c line 1668
0x40209929: vsnprintf_P(char*, size_t, char const*, va_list) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\pgmspace.cpp line 285
0x4020996a: sprintf_P(char*, char const*, ...) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\pgmspace.cpp line 265
0x40208c94: String::operator=(String const&) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 220
0x402056dc: EspalexaDevice::getName() at C:\Users\thoma\OneDrive\Documents\Arduino\libraries\Espalexa\src\EspalexaDevice.cpp line 56
0x40202e23: Espalexa::deviceJsonString(EspalexaDevice*, char*) at C:\Users\thoma\OneDrive\Documents\Arduino\libraries\Espalexa\src/Espalexa.h line 167
0x40208eab: String::concat(char const*, unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 288
0x40208f78: String::concat(char) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 306
0x40203882: Espalexa::handleAlexaApiCall(String, String) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/WString.h line 121
0x40219828: mem_free at core/mem.c line 152
0x40212592: memp_free at core/memp.c line 496
0x40100575: realloc at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\umm_malloc\umm_malloc.c line 1499
0x40208b0f: String::changeBuffer(unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 156
0x40208b5b: String::reserve(unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 146
0x40208b8d: String::copy(char const*, unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 175
0x40100575: realloc at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\umm_malloc\umm_malloc.c line 1499
0x40100575: realloc at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\umm_malloc\umm_malloc.c line 1499
0x40208b0f: String::changeBuffer(unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 156
0x40208b5b: String::reserve(unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 146
0x40208b8d: String::copy(char const*, unsigned int) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 175
0x40208bda: String::String(char const*) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 36
0x40208c94: String::operator=(String const&) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\WString.cpp line 220
0x4020397b: Espalexa::serveNotFound() at C:\Users\thoma\OneDrive\Documents\Arduino\libraries\Espalexa\src/Espalexa.h line 201
0x402039bc: std::_Function_handler ::_M_invoke(std::_Any_data const&) at c:\users\thoma\onedrive\documents\arduinodata\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2073
0x40206ca2: std::function ::operator()() const at c:\users\thoma\onedrive\documents\arduinodata\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2/functional line 2465
0x40206dca: ESP8266WebServer::_handleRequest() at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WebServer\src\ESP8266WebServer.cpp line 598
0x401065f8: millis at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\core_esp8266_wiring.c line 183
0x40205098: WiFiServer::available(unsigned char*) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src\WiFiServer.cpp line 111
0x402061f8: FunctionRequestHandler::canHandle(HTTPMethod, String) at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WebServer\src\detail/RequestHandlersImpl.h line 20
0x40206fb4: ESP8266WebServer::handleClient() at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WebServer\src\ESP8266WebServer.cpp line 303
0x40203d3b: Espalexa::loop() at C:\Users\thoma\OneDrive\Documents\Arduino\libraries\Espalexa\src/Espalexa.h line 363
0x402096a8: esp_yield() at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\core_esp8266_main.cpp line 91
0x4020151a: delay at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\core_esp8266_wiring.c line 54
0x40203dcc: loop() at C:\Users\thoma\OneDrive\Documents\Arduino\sketches\sample_that__I_found/sample_that__I_found.ino line 96
0x40209734: loop_wrapper() at C:\Users\thoma\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266\core_esp8266_main.cpp line 125

I have no idea why trying to connect to the alexa breaks everything, and its driving me crazy. (also as a sidenote I know for the ssid and password it just says name, I did that here on purpose). Any help would be awesome!

Do you have the latest boards file for the ESP?

Alexa broadcasts a UDP request on your local wifi network, scanning for compatible devices.
Reference

I just use ESPHome (on Home Assistant) or Tasmota on my ESP devices and Alexa Discovery never fails.

Hope this helps.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.