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

ESP8266 Crashing! #9097

Open
VonalOrdu opened this issue Feb 28, 2024 · 7 comments
Open

ESP8266 Crashing! #9097

VonalOrdu opened this issue Feb 28, 2024 · 7 comments

Comments

@VonalOrdu
Copy link

VonalOrdu commented Feb 28, 2024

Platform

  • Hardware: ESP-12
  • Core Version: ??
  • Development Env: Arduino IDE
  • Operating System: Windows

Settings in IDE

  • Module: Generic ESP8266 Module
  • Flash Mode: DOUT
  • Flash Size: 1MB
  • lwip Variant: v2 Lower Memory
  • Reset Method: dtr(aka nodemcu)
  • Flash Frequency: 40Mhz
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 921600

Problem Description

what's the problem? please help

#include "config.h"

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

  pinMode(TRIGGER_PIN, INPUT);
  pinMode(wifiLed, OUTPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(webSocketLed, OUTPUT);

  digitalWrite(wifiLed, HIGH);
  digitalWrite(relayPin, LOW);
  digitalWrite(webSocketLed, LOW);

  // Bağlantı denemesi
  // WiFi.begin("mezopotamya", "password");
  WiFi.begin();

  // Bağlantı sağlanana kadar bekleyin
  int baglantiDenemesi = 0;
  while (WiFi.status() != WL_CONNECTED)
  {
    wifiLedUpdate();
    delay(1000);
    Serial.println("Bağlanılamadı. ");
    // startWiFiManager();
    if (baglantiDenemesi > 30)
    {
      baglantiDenemesi = 0;
      Serial.println("Bağlantı başarısız. WiFi Manager başlatılıyor...");
      // ESP.eraseConfig();
      delay(200);
      startWiFiManager();
      break;
    }
    baglantiDenemesi++;
  }
  Serial.println("Bağlantı başarılı!");

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(wifiLed, HIGH);
    Serial.println("WiFi connection lost. Reconnecting...");
    wifiReconnect();
  }
  else
  {
    digitalWrite(wifiLed, LOW);
  }
  // Programın ana döngüsü
  resetButton();

  if (!client.connected())
  {
    reconnect();
  }

  if (client.connected() && onlineBildirimZamani + 10000 < millis())
  {
    Serial.println("Online oldugu mesaji gonderiliyor...");
    client.publish("home/kombi/available", "online");
    onlineBildirimZamani = millis();
  }

  client.loop();
  // delay(1000);
  if (yeniVeri)
  {
    yeniVeri = false;
    // Control the relay based on temperature comparison
    if (sonDurum == 1 && veriGuncelmi() && digitalRead(relayPin) == LOW)
    {
      sonAcilma = zaman();
      // Kombiyi aç
      digitalWrite(relayPin, HIGH);
      Serial.println("Kombi opened");
      client.publish("home/kombi", "1");
    }

    else if (sonDurum == 0)
    {
      sonKapanma = zaman();
      // Kombiyi kapat
      digitalWrite(relayPin, LOW);
      Serial.println("Kombi closed");
      client.publish("home/kombi", "0");
    }
  }

  if (!veriGuncelmi())
  {
    if (digitalRead(relayPin) == HIGH)
    {
      sonKapanma = zaman();
      // Kombiyi kapat
      digitalWrite(relayPin, LOW);
      Serial.println("Kombi closed");
      client.publish("home/kombi", "0");
    }
  }
}
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

#include <PubSubClient.h>
#include <ArduinoJson.h>

const char *mqtt_server = "192.168.0.19";
const char *mqtt_username = "mqtt_user";
const char *mqtt_password = "12345";
const char *topic = "home/kombi/set";

float mevcutSicaklik = 0.0;
unsigned int sonDurum = 0;
float istenilenSicaklik = 0.0;
float tolerasDegeri = 0.0;
int maxCalisacakSure = 0;
long sonAcilma = 0;
long sonKapanma = 0;
int sonVeriOkunmaZamani = 0;
int verininGecerlilikSuresi = 60;
unsigned int onlineBildirimZamani = 0;
bool yeniVeri = false;

const int relayPin = 4;     // 4 numaralı pin
const int wifiLed = 2;      // 4 numaralı pin
const int webSocketLed = 5; // 4 numaralı pin

WiFiClient espClient;
PubSubClient client(espClient);

#define TRIGGER_PIN 0


void wifiLedUpdate()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        Serial.println("\nwifiLed LOW");
        digitalWrite(wifiLed, LOW);
    }
    else
    {
        Serial.println("\nwifiLed HIGH");
        digitalWrite(wifiLed, HIGH);
    }
}

int zaman()
{
    return millis() / 1000;
}

void startWiFiManager()
{
    WiFiManager wm;
    Serial.println("WiFi Manager ile bağlantı sağlandı!");
    wm.setConfigPortalTimeout(30); // auto close configportal after n seconds
    bool res;
    res = wm.autoConnect(); // auto generated AP name from chipid
    if (!res)
    {
        Serial.println("Failed to connect or hit timeout. Restarting");
        ESP.restart();
    }
    else
    {
        // if you get here you have connected to the WiFi
        Serial.println("connected...yeey :)");
    }
}

void reconnect()
{
    int mqtt_baglanti_deneme_sayisi = 0;
    while (!client.connected())
    {
        if (mqtt_baglanti_deneme_sayisi > 5)
        {
            ESP.reset();
        }

        mqtt_baglanti_deneme_sayisi++;
        Serial.print("Attempting MQTT connection...");

        if (client.connect("ESP8266Client", mqtt_username, mqtt_password))
        {
            digitalWrite(webSocketLed, HIGH);
            Serial.println("Connected to MQTT broker");
            client.subscribe(topic);
        }
        else
        {
            digitalWrite(webSocketLed, LOW);
            Serial.print("Failed, rc=");
            Serial.print(client.state());
            Serial.println(" Retrying in 5 seconds");
            delay(5000);
        }
    }
}

void callback(char *topic, byte *payload, unsigned int length)
{
    Serial.println("Message arrived in topic: " + String(topic));

    // Mesajı bir String'e çevir
    String message = "";
    for (unsigned int i = 0; i < length; i++)
    {
        message += (char)payload[i];
    }

    Serial.println("Message: " + message);

    // "home/kombi/set" konusundaki mesajları kontrol et
    if (String(topic) == "home/kombi/set")
    {
        yeniVeri = true;
        if (message == "1")
        {
            sonVeriOkunmaZamani = zaman();
            sonDurum = 1;
        }
        else if (message == "0")
        {
            sonVeriOkunmaZamani = zaman();
            sonDurum = 0;
        }
    }
}

// void callback(char *topic, byte *payload, unsigned int length)
// {
//     Serial.println("Message received for topic: " + String(topic));
//     Serial.println(int(payload));

//     // // Parse JSON payload
//     DynamicJsonDocument doc(256);
//     deserializeJson(doc, payload, length);
//     power = doc["power"];
//     Serial.println(power);

// mevcutSicaklik = doc["mevcut_sicaklik"];
// istenilenSicaklik = doc["istenilen_sicaklik"];
// tolerasDegeri = doc["tolerans_degeri"];

// Serial.print("Mevcut Sıcaklık: ");
// Serial.println(mevcutSicaklik);
// Serial.print("İstenilen Sıcaklık: ");
// Serial.println(istenilenSicaklik);
// Serial.print("tolerasDegeri: ");
// Serial.println(tolerasDegeri);
// sonVeriOkunmaZamani = zaman();
// }

void resetButton()
{
    // check for button press
    if (digitalRead(TRIGGER_PIN) == LOW)
    {
        // poor mans debounce/press-hold, code not ideal for production
        delay(50);
        if (digitalRead(TRIGGER_PIN) == LOW)
        {
            Serial.println("Button Pressed");
            // still holding button for 3000 ms, reset settings, code not ideaa for production
            delay(3000); // reset delay hold
            if (digitalRead(TRIGGER_PIN) == LOW)
            {
                Serial.println("Button Held");
                Serial.println("Erasing Config, restarting");
                // ESP.eraseConfig();
                delay(500);
                ESP.restart();
            }
        }
    }
}

bool veriGuncelmi()
{
    if ((sonVeriOkunmaZamani + verininGecerlilikSuresi) < zaman())
    {
        return false;
    }
    return true;
}

void wifiReconnect()
{
    Serial.println("Connecting to WiFi...");

    // WiFi bağlantısını başlat
    WiFi.begin();

    int attempts = 0;
    while (WiFi.status() != WL_CONNECTED && attempts < 60)
    {
        wifiLedUpdate();
        delay(500);
        Serial.print(".");
        attempts++;
    }
    if (attempts > 60)
    {
        Serial.println("\nESP Reseting...");
        ESP.reset();
    }

    if (WiFi.status() == WL_CONNECTED)
    {
        Serial.println("\nConnected to WiFi");
        wifiLedUpdate();
    }
    else
    {
        Serial.println("\nFailed to connect to WiFi. Please check your credentials.");
    }
}


Debug Messages


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

wdt reset
load 0x4010f000, len 3424, room 16 
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8 
tail 0
chksum 0x2b
csum 0x2b
v000717e0
~ld


Hardware WDT reset


>>>stack>>>

ctx: sys
sp: 3fffeb30 end: 3ffffd30 offset: 0000
3fffeb30:  3ffede50 3fffebe0 3ffee4e0 00000016  
3fffeb40:  40238a00 3fffebe0 3ffee388 3ffedd8c  
3fffeb50:  40238a61 3fffebe0 3fffebe0 40100caa  
3fffeb60:  6f7a656d 61746f70 0061796d 40100caa  
3fffeb70:  00002200 3ffe862c 3ffefbe8 40100caa  
3fffeb80:  40234d59 3ffe862c 0000001c 40100f04  
[...(stripped)]
<<<stack<<<


Hardware WDT Stack Dump - enabled

⸮⸮






Decode Debug Messages

Decoding stack results
0x40100caa: check_poison_block(umm_block*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_poison.c line 86
0x40100caa: check_poison_block(umm_block*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_poison.c line 86
0x40100caa: check_poison_block(umm_block*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_poison.c line 86
0x40100f04: umm_malloc_core(umm_heap_context_t*, size_t) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_local.c line 47
0x40215812: loop_task(ETSEvent*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 273
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x4021f0b5: _printf_i at /workdir/repo/newlib/newlib/libc/stdio/nano-vfprintf_i.c line 194
0x40100830: _write_r(_reent*, int, char*, int) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\libc_replacements.cpp line 92
0x402249a9: _fflush_r at /workdir/repo/newlib/newlib/libc/stdio/fflush.c line 278
0x40224599: __swbuf_r at /workdir/repo/newlib/newlib/libc/stdio/wbuf.c line 81
0x40100caa: check_poison_block(umm_block*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_poison.c line 86
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x401002c1: millis() at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_wiring.cpp line 176
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100164: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 236
0x402011d8: callback(char*, unsigned char*, unsigned int) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/WString.h line 115
0x4020272a: WiFiClient::available() at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\src\WiFiClient.cpp line 254
0x40211288: PubSubClient::loop() at C:\Users\KABUK\Documents\Arduino\libraries\PubSubClient\src\PubSubClient.cpp line 387
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238

Decode Debug Messages 2

Decoding stack results
0x40100caa: check_poison_block(umm_block*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_poison.c line 86
0x40100caa: check_poison_block(umm_block*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_poison.c line 86
0x40100caa: check_poison_block(umm_block*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_poison.c line 86
0x40100f04: umm_malloc_core(umm_heap_context_t*, size_t) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc/umm_local.c line 47
0x40215812: loop_task(ETSEvent*) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 273
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x40202b00: WiFiClient::connected() at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\src\WiFiClient.cpp line 329
0x40100178: ets_post(uint8, ETSSignal, ETSParam) at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 238
0x4020272a: WiFiClient::available() at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\src\WiFiClient.cpp line 254
0x40211288: PubSubClient::loop() at C:\Users\KABUK\Documents\Arduino\libraries\PubSubClient\src\PubSubClient.cpp line 387
0x4020173f: loop() at D:\Users\Google Drive\Akıllı Ev\HomeAsisstant\Moduller\TermostatV2_Arduino/TermostatV2_Arduino.ino line 65
0x40215ab8: loop_wrapper() at C:\Users\KABUK\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 258
@VonalOrdu VonalOrdu closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2024
@VonalOrdu VonalOrdu reopened this Feb 28, 2024
@d-a-v
Copy link
Collaborator

d-a-v commented Feb 28, 2024

Too much time in this loop without yield() => WDT ?

    while (WiFi.status() != WL_CONNECTED && attempts < 60)
    {
        wifiLedUpdate();
        delay(500);
        Serial.print(".");
        attempts++;
    }

@VonalOrdu
Copy link
Author

Too much time in this loop without yield() => WDT ? > > c++ > while (WiFi.status() != WL_CONNECTED && attempts < 60) > { > wifiLedUpdate(); > delay(500); > Serial.print("."); > attempts++; > } >

Is there a negative side of high delay in connection controls?

@d-a-v
Copy link
Collaborator

d-a-v commented Mar 1, 2024

Is there a negative side of high delay in connection controls?

Yes

@asturcon3
Copy link

I really hate that String+=char loop in your callback function, with an unknown length... Just guessing

@abdosn
Copy link

abdosn commented Apr 16, 2024

Too much time in this loop without yield() => WDT ?

    while (WiFi.status() != WL_CONNECTED && attempts < 60)
    {
        wifiLedUpdate();
        delay(500);
        Serial.print(".");
        attempts++;
    }

@d-a-v

does delay(500) have the effect of yield() ?

@d-a-v
Copy link
Collaborator

d-a-v commented Apr 16, 2024

Yes yield() is called from inside delay()

@abdosn
Copy link

abdosn commented Apr 17, 2024

I agree with @asturcon3 about the use of String
Also casting c string to String class consumes memory and time
so try to use string.h functions instead of String class

in the callback function
I recall that either topic or message could have some garbage at the end instead of null character

Also in void reconnect() function
Add delay() or yield() inside while (!client.connected()) loop

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

4 participants