ESP8266 restarting wtd rst cause:4

er3016
Posts: 2
Joined: Wed Feb 21, 2024 10:38 am

ESP8266 restarting wtd rst cause:4

Postby er3016 » Wed Feb 21, 2024 11:16 am

I'm trying to get into the world of WiFi microcontrollers, but I'm having quite a lot of issues with my ESP8266. I was able to program it using my Arduino Uno using the following configuration:
Screenshot 2024-02-21 at 10-52-03 Circuit design Copy of Wifi Module ESP8266 Tinkercad.png
Screenshot 2024-02-21 at 10-52-03 Circuit design Copy of Wifi Module ESP8266 Tinkercad.png (92.01 KiB) Viewed 3725 times
To upload this code:

Code: Select all

// V2
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Esp.h>
#define machineId 1

const char *ssid = "Wifi ssid";
const char *password = "Wifi password";
const char *serverUrl = "my api";

const int relayPin = 2;
bool cutDetected = false;
WiFiClient wifiClient;

void ICACHE_RAM_ATTR countCut() {
  static unsigned long lastInterruptTime = 0;
  unsigned long currentDebounceTime = millis();
  // Debounce
  if (currentDebounceTime - lastInterruptTime > 2000) {
    lastInterruptTime = currentDebounceTime;
    cutDetected = true;
  }
}

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

  pinMode(relayPin, INPUT_PULLUP);

  // Connect to WiFi
  Serial.println();
  Serial.println("Connecting to WiFi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: " + WiFi.localIP().toString());

  // Attach interrupt
  attachInterrupt(digitalPinToInterrupt(relayPin), countCut, FALLING);
  delay(5000);
}

void loop() {
  if (cutDetected) {
    cutDetected = false;

    // JSON Payload
    String json = "{";
    json += "\"id\": \"" + String(machineId) + "\"";
    json += "}";

    // Setup HTTP request
    HTTPClient http;
    http.begin(wifiClient, serverUrl);
    http.addHeader("Content-Type", "application/json");

    // Setup HTTP request
    int httpResponseCode = http.POST(json);
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response Code: ");
      Serial.println(httpResponseCode);
    } else {
      Serial.print("HTTP POST request failed. Error code: ");
      Serial.println(httpResponseCode);
      Serial.println(http.errorToString(httpResponseCode));
    }

    http.end();
  }
}
And after uploading, I changed my connections as such:
Screenshot 2024-02-21 at 11-10-15 Circuit design Copy of Wifi Module ESP8266 Tinkercad.png
Screenshot 2024-02-21 at 11-10-15 Circuit design Copy of Wifi Module ESP8266 Tinkercad.png (93 KiB) Viewed 3725 times
To simulate the signal, I am just touching that leftmost blue wire to the ground, thus simulating a closing of a switch on the pullup resistor. However, when I do the same thing using a relay from the actual machine I want to detect, my ESP sometimes restarts with the error:
load 0x4010f000, len 3424, room 16
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8
tail 0
chksum 0x2b
csum 0x2b
v00046d40
~ld
����n�r��n|��l�rlc��|r�l�n��n�l`��r�l�l��

And then connects to wifi again.

I even tried using a second relay, to isolate the signal. But when I connect one of the relay pins to ground, it sometimes starts restarting again.

User avatar
Inq720
Posts: 38
Joined: Fri Dec 22, 2023 1:36 pm
Location: North Carolina, USA
Contact:

Re: ESP8266 restarting wtd rst cause:4

Postby Inq720 » Thu Feb 22, 2024 4:27 pm

Well, again... you are making things harder on yourself than is necessary. No one that I know of uses an Arduino to program a ESP8266. Most people use a development board version since they have a USB connection to your computer and they have more pins available to do more in-depth projects. But if you insist on using the ESP-01 board, you can simply get a programmer like https://www.amazon.com/ESP8266-ESP-01S- ... 099JBC3RB/. Note some like this one break-out the pins making it easy to make connections to your components. Some do not!

Good luck.

er3016
Posts: 2
Joined: Wed Feb 21, 2024 10:38 am

Re: ESP8266 restarting wtd rst cause:4

Postby er3016 » Thu Feb 22, 2024 8:20 pm

Programming the board isn't really the issue here, at least as far as I can tell. I admit that I would have also preferred to use what you recommended, but I managed to find a way to program the ESP. However, my problem is after the ESP is in functioning mode, not programming mode. The error shows up when I detect the signal. However, it appears the problem is not even related to the input pin. I'm currently testing using a relay without connecting to the GPIO2 pin, as shown in the image
Screenshot 2024-02-22 at 20-18-28 Circuit design Copy of Wifi Module ESP8266 Tinkercad.png
Screenshot 2024-02-22 at 20-18-28 Circuit design Copy of Wifi Module ESP8266 Tinkercad.png (145.01 KiB) Viewed 3690 times
However, when I close the connection by activating the relay, I still get the same problem.

ESP_Sprite
Posts: 9772
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP8266 restarting wtd rst cause:4

Postby ESP_Sprite » Fri Feb 23, 2024 2:40 am

Yeah, don't switch a relay directly, that's an inductive load and will mess with your ESP chip. Look for a circuit that uses a mosfet or transistor to switch it and make sure it has a flyback diode.

eriksl
Posts: 116
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: ESP8266 restarting wtd rst cause:4

Postby eriksl » Fri Apr 05, 2024 12:51 pm

You can even use a MOSFET driver (without the actual MOSFET), they're quite good at supplying a big charge at once and preventing fly back currents. Something like IR4427, they're quite cheap.

Indeed never attach something with a coil (relay, motor) directly to an I/O pin.

Another possible cause for a watchdog reset is the system code doesn't get called frequently enough. This can be caused by a hard crash or by bad code. It looks like you're using Arduino and I guess that Arduino takes care of this, I am not sure though.

Who is online

Users browsing this forum: No registered users and 8 guests