The use of the ESP8266 in the world of IoT

User avatar
By benni_germany
#68944 Basic Infos

Node MCU 1.0 / ESP8266

ESP-12E crashes / resets Randomly.

This is not really nice because all my relays reset when this happens. :(

Code is from https://github.com/nassir-malik/IOT-ESP ... le-Devices

Or full code from Attachment

Error codes at the end in Debug Section.

Just ask if thats not enough info, but i dont know what i could add. :D

Thanks in advance ;)

Hardware

Hardware: Node MCU 1.0 / ESP8266 ESP-12E

Description

Node MCU 1.0 / ESP8266 ESP-12E crashes randomly

error see debug info at the end

Settings in IDE

Module: NodeMCU 1.0 (ESP-12E Module)
Flash Size: 4MB (3MSPIFFS)
CPU Frequency: 80Mhz
Flash Mode: ?
Flash Frequency: ?
Upload Using: OTA / SERIAL?
Reset Method: ck / nodemcu?

Sketch

Folder folder with all sketch data: attached file

All sketch data that i changed:

wemos.ini

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"

// prototypes
boolean connectWifi();

//on/off callbacks
void Steckdose1On();
void Steckdose1Off();
void Steckdose2On();
void Steckdose2Off();
void Steckdose3On();
void Steckdose3Off();
void Relay4On();
void Relay4Off();
void LEDOn();
void LEDOff();

// Change this before you flash
//#######################################
const char* ssid = "myssid"; //enter your access point/wifi router name
const char* password = "mypassword"; //enter router password
IPAddress ip(192, 168, 1, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1);

// change gpio pins as you need it.
const int relayPin1 = 5;
const int relayPin2 = 4;
const int relayPin3 = 14;
const int relayPin4 = 12;
const int relayPin5 = LED_BUILTIN;


//#######################################
boolean wifiConnected = false;

UpnpBroadcastResponder upnpBroadcastResponder;

Switch *Steckdose1 = NULL;
Switch *Steckdose2 = NULL;
Switch *Steckdose3 = NULL;
Switch *Relay4 = NULL;
Switch *LED = NULL;

void setup()
{
  Serial.begin(115200);
   pinMode(relayPin1, OUTPUT);
   pinMode(relayPin2, OUTPUT);
   pinMode(relayPin3, OUTPUT);
   pinMode(relayPin4, OUTPUT);   
   pinMode(relayPin5, OUTPUT);
   
  digitalWrite(relayPin5, HIGH);
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPin3, HIGH);
  digitalWrite(relayPin2, HIGH);
  digitalWrite(relayPin1, HIGH);
 
  // Initialise wifi connection
  wifiConnected = connectWifi();
 
  if(wifiConnected){
    upnpBroadcastResponder.beginUdpMulticast();
   
    // Define your switches here. Max 14
    // Format: Alexa invocation name, local port no, on callback, off callback
    Steckdose1 = new Switch("Steckdose eins", 80, Steckdose1On, Steckdose1Off);
    Steckdose2 = new Switch("Steckdose zwei", 81, Steckdose2On, Steckdose2Off);
    Steckdose3 = new Switch("Steckdose drei", 82, Steckdose3On, Steckdose3Off);
    Relay4 = new Switch("Relais vier", 83, Relay4On, Relay4Off);
    LED = new Switch("L E D", 84, LEDOn, LEDOff);

   
    Serial.println("Adding switches upnp broadcast responder");
    upnpBroadcastResponder.addDevice(*Steckdose1);
    upnpBroadcastResponder.addDevice(*Steckdose2);
    upnpBroadcastResponder.addDevice(*Steckdose3);
    upnpBroadcastResponder.addDevice(*Relay4);
    upnpBroadcastResponder.addDevice(*LED);   
  }
}

void loop()
{
    if(wifiConnected){
      upnpBroadcastResponder.serverLoop();
     
      Steckdose1->serverLoop();
      Steckdose2->serverLoop();
      Steckdose3->serverLoop();
      Relay4->serverLoop();
      LED->serverLoop();
    }
}

void Steckdose1On() {
    Serial.print("Steckdose eins an...\n");
    digitalWrite(relayPin1, LOW);
}

void Steckdose1Off() {
    Serial.print("Steckdose eins aus...\n");
    digitalWrite(relayPin1, HIGH);
}

void Steckdose2On() {
    Serial.print("Steckdose zwei an...\n");
    digitalWrite(relayPin2, LOW);
}

void Steckdose2Off() {
  Serial.print("Steckdose zwei aus...\n");
  digitalWrite(relayPin2, HIGH);
}

void Steckdose3On() {
    Serial.print("Steckdose drei an...\n");
    digitalWrite(relayPin3, LOW);
}

void Steckdose3Off() {
    Serial.print("Steckdose drei aus...\n");
    digitalWrite(relayPin3, HIGH);
}

void Relay4On() {
    Serial.print("Relais 4 an...\n");
    digitalWrite(relayPin4, LOW);
}

void Relay4Off() {
  Serial.print("Relais 4 aus...\n");
  digitalWrite(relayPin4, HIGH);
}

void LEDOn() {
    Serial.print("Eingebaute LED an... \n");
    digitalWrite(relayPin5, LOW);
}

void LEDOff() {
  Serial.print("Eingebaute LED aus... \n");
  digitalWrite(relayPin5, HIGH);
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
  boolean state = true;
  int i = 0;
  WiFi.config(ip, dns, gateway, subnet);
  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 > 10){
      state = false;
      break;
    }
    i++;
  }
 
  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.");
    digitalWrite(relayPin5, LOW);
  }
 
  return state;
}



switch.cpp

Code: Select all#include "Switch.h"
#include "CallbackFunction.h"


       
//<<constructor>>
Switch::Switch(){
    Serial.println("default constructor called");
}
//Switch::Switch(String alexaInvokeName,unsigned int port){
Switch::Switch(String alexaInvokeName, unsigned int port, CallbackFunction oncb, CallbackFunction offcb){
    uint32_t chipId = ESP.getChipId();
    char uuid[64];
    sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
          (uint16_t) ((chipId >> 16) & 0xff),
          (uint16_t) ((chipId >>  8) & 0xff),
          (uint16_t)   chipId        & 0xff);
   
    serial = String(uuid);
    persistent_uuid = "Socket-1_0-" + serial+"-"+ String(port);
       
    device_name = alexaInvokeName;
    localPort = port;
    onCallback = oncb;
    offCallback = offcb;
   
    startWebServer();
}


 
//<<destructor>>
Switch::~Switch(){/*nothing to destruct*/}

void Switch::serverLoop(){
    if (server != NULL) {
        server->handleClient();
        delay(1);
    }
}

void Switch::startWebServer(){
  server = new ESP8266WebServer(localPort);

  server->on("/", [&]() {
    handleRoot();
  });
 

  server->on("/setup.xml", [&]() {
    handleSetupXml();
  });
 
  server->on("/index.html", [&]() {
      String response = "<html><body>LED: <a href=\"/LED_ON.html\">An</a> / <a href=\"/LED_OFF.html\">Aus</a> <br> 1: <a href=\"/1_ON.html\">An</a> / <a href=\"/1_OFF.html\">Aus</a> <br>2: <a href=\"/2_ON.html\">An</a> / <a href=\"/2_OFF.html\">Aus</a> <br>3: <a href=\"/3_ON.html\">An</a> / <a href=\"/3_OFF.html\">Aus</a> <br>4: <a href=\"/4_ON.html\">An</a> / <a href=\"/4_OFF.html\">Aus</a> </body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to index.html ... ########\n");
      digitalWrite(LED_BUILTIN, LOW);
      delay(200);
      digitalWrite(LED_BUILTIN, HIGH);
  });
 
  server->on("/index2.html", [&]() {
      String response = "<html><body>LED: <a href=\"/LED_ON.html\">An</a> / <a href=\"/LED_OFF.html\">Aus</a> <br> 1: <a href=\"/1_ON.html\">An</a> / <a href=\"/1_OFF.html\">Aus</a> <br>2: <a href=\"/2_ON.html\">An</a> / <a href=\"/2_OFF.html\">Aus</a> <br>3: <a href=\"/3_ON.html\">An</a> / <a href=\"/3_OFF.html\">Aus</a> <br>4: <a href=\"/4_ON.html\">An</a> / <a href=\"/4_OFF.html\">Aus</a> </body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to index.html ... ########\n");
  });

 
  server->on("/LED_ON.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to LED_ON.html ... ########\n");
      digitalWrite(LED_BUILTIN, LOW);
  });
  server->on("/LED_OFF.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to LED_OFF.html ... ########\n");
      digitalWrite(LED_BUILTIN, HIGH);
  });

 
  server->on("/1_ON.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 1_ON.html ... ########\n");
      digitalWrite(5, LOW);
  });
  server->on("/1_OFF.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 1_OFF.html ... ########\n");
      digitalWrite(5, HIGH);
  });

 
  server->on("/2_ON.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 2_ON.html ... ########\n");
      digitalWrite(4, LOW);
  });
  server->on("/2_OFF.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 2_OFF.html ... ########\n");
      digitalWrite(4, HIGH);
  });
 
  server->on("/3_ON.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 3_ON.html ... ########\n");
      digitalWrite(14, LOW);
  });
  server->on("/3_OFF.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 3_OFF.html ... ########\n");
      digitalWrite(14, HIGH);
  });
 
  server->on("/4_ON.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 4_ON.html ... ########\n");
      digitalWrite(12, LOW);
  });
  server->on("/4_OFF.html", [&]() {
      String response = "<html><meta http-equiv=\"refresh\" content=\"2; URL=index2.html\" /><body>OK</body></html>";
       server->send(200, "text/html", response.c_str());
      Serial.println(" ########## Responding to 4_OFF.html ... ########\n");
      digitalWrite(12, HIGH);
  });


   
  server->on("/upnp/control/basicevent1", [&]() {
    handleUpnpControl();
  });

  server->on("/eventservice.xml", [&]() {
    handleEventservice();
  });

  //server->onNotFound(handleNotFound);
  server->begin();

  Serial.println("WebServer started on port: ");
  Serial.println(localPort);
}
 
void Switch::handleEventservice(){
  Serial.println(" ########## Responding to eventservice.xml ... ########\n");
 
  String eventservice_xml = "<?scpd xmlns=\"urn:Belkin:service-1-0\"?>"
        "<actionList>"
          "<action>"
            "<name>SetBinaryState</name>"
            "<argumentList>"
              "<argument>"
                "<retval/>"
                "<name>BinaryState</name>"
                "<relatedStateVariable>BinaryState</relatedStateVariable>"
                "<direction>in</direction>"
              "</argument>"
            "</argumentList>"
             "<serviceStateTable>"
              "<stateVariable sendEvents=\"yes\">"
                "<name>BinaryState</name>"
                "<dataType>Boolean</dataType>"
                "<defaultValue>0</defaultValue>"
              "</stateVariable>"
              "<stateVariable sendEvents=\"yes\">"
                "<name>level</name>"
                "<dataType>string</dataType>"
                "<defaultValue>0</defaultValue>"
              "</stateVariable>"
            "</serviceStateTable>"
          "</action>"
        "</scpd>\r\n"
        "\r\n";
         
    server->send(200, "text/plain", eventservice_xml.c_str());
}
 
void Switch::handleUpnpControl(){
  Serial.println("########## Responding to  /upnp/control/basicevent1 ... ##########");     
 
 for (int x=0; x <= server->args(); x++) {
  Serial.println(server->arg(x));
  }
 
  String request = server->arg(0);     
  Serial.print("request:");
  Serial.println(request);

  if(request.indexOf("<BinaryState>1</BinaryState>") > 0) {
      Serial.println("Got Turn on request");
      onCallback();
  }

  if(request.indexOf("<BinaryState>0</BinaryState>") > 0) {
      Serial.println("Got Turn off request");
      offCallback();
  }
 
  server->send(200, "text/plain", "");
}

void Switch::handleRoot(){
  server->send(200, "text/html", "<meta http-equiv=\"refresh\" content=\"2; URL=index.html\" /> <title>Node MCU / ESP8266 ESP-12e</title><strong>Node MCU / ESP8266 ESP-12e</strong><br>Say Alexa discover devices. / Alexa Suche nach ger&auml;ten.");
  Serial.println(" ########## Responding to root file ... ########\n");

}

void Switch::handleSetupXml(){
  Serial.println(" ########## Responding to setup.xml ... ########\n");
 
  IPAddress localIP = WiFi.localIP();
  char s[16];
  sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
  Serial.println("localiP:");
  Serial.println(WiFi.localIP());
  String setup_xml = "<?xml version=\"1.0\"?>"
        "<root>"
         "<device>"
            "<deviceType>urn:Belkin:device:controllee:1</deviceType>"
            "<friendlyName>"+ device_name +"</friendlyName>"
            "<manufacturer>Belkin International Inc.</manufacturer>"
            "<modelName>Emulated Socket</modelName>"
            "<modelNumber>3.1415</modelNumber>"
            "<UDN>uuid:"+ persistent_uuid +"</UDN>"
            "<serialNumber>221517K0101769</serialNumber>"
            "<binaryState>0</binaryState>"
            "<serviceList>"
              "<service>"
                  "<serviceType>urn:Belkin:service:basicevent:1</serviceType>"
                  "<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>"
                  "<controlURL>/upnp/control/basicevent1</controlURL>"
                  "<eventSubURL>/upnp/event/basicevent1</eventSubURL>"
                  "<SCPDURL>/eventservice.xml</SCPDURL>"
              "</service>"
          "</serviceList>"
          "</device>"
        "</root>\r\n"
        "\r\n";
       
    server->send(200, "text/xml", setup_xml.c_str());
   
    Serial.print("Sending :");
    Serial.println(setup_xml);
}

String Switch::getAlexaInvokeName() {
    return device_name;
}

void Switch::respondToSearch(IPAddress& senderIP, unsigned int senderPort) {
  Serial.println("");
  Serial.print("Sending response to ");
  Serial.println(senderIP);
  Serial.print("Port : ");
  Serial.println(senderPort);

  IPAddress localIP = WiFi.localIP();
  char s[16];
  sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);

  String response =
       "HTTP/1.1 200 OK\r\n"
       "CACHE-CONTROL: max-age=86400\r\n"
       "DATE: Sat, 26 Nov 2016 04:56:29 GMT\r\n"
       "EXT:\r\n"
       "LOCATION: http://" + String(s) + ":" + String(localPort) + "/setup.xml\r\n"
       "OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
       "01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
       "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
       "ST: urn:Belkin:device:**\r\n"
       "USN: uuid:" + persistent_uuid + "::urn:Belkin:device:**\r\n"
       "X-User-Agent: redsonic\r\n\r\n";

  UDP.beginPacket(senderIP, senderPort);
  UDP.write(response.c_str());
  UDP.endPacket();                   

   Serial.println("Response sent !");
}


Other files are default from this source: https://github.com/nassir-malik/IOT-ESP8266-ESP12E-Alexa-Multiple-Devices

Debug / Error Messages


Code: Select allException 9: LoadStoreAlignmentCause: Load or store to an unaligned address
Decoding 5 results
0x40202777: Switch::serverLoop() at C:\Users\myname\AppData\Local\Temp\arduino_build_489203\sketch/Switch.cpp line 36
0x40203454: loop at F:\myname\Documents\GitHub\IOT-ESP8266-ESP12E-Alexa-Multiple-Devices\wemos/wemos.ino line 97
0x4020797c: loop_wrapper at F:\myname\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/core_esp8266_main.cpp line 56
0x40100718: cont_norm at F:\myname\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/cont.S line 109



Not encoded:


Code: Select allException (9):
epc1=0x40202777 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4f480a0d depc=0x00000000

ctx: cont
sp: 3fff0f80 end: 3fff1160 offset: 01a0

>>>stack>>>
3fff1120:  3fffdad0 00000000 3fff0124 3fff012c 
3fff1130:  3fffdad0 00000000 3fff0124 40203454 
3fff1140:  3fffdad0 00000000 3fff0124 4020797c 
3fff1150:  feefeffe feefeffe 3fff0140 40100718 
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
Last edited by benni_germany on Mon Aug 21, 2017 1:00 pm, edited 3 times in total.