ESP32 powered via Easy Driver

Hello all, I'm trying to create a couple of wifi enabled stepper motors with ESP32's (each motor is connected to separate ESP32's, different projects in different parts of the house). I have one working and I've been testing it before I implement the rest of them and the issue I'm noticing is that I have to unplug the power supply and plug it back in every few days. First I thought the wifi was dropping so I added a reconnect function. I know this function is working because I will unplug my router and the ESP32 does reconnect. I have the ESP32 powered by the power output of the Easy Driver so I'm wondering if that is my problem, if that power source isn't 100% reliable. Anyone have any experience with this or any ideas what's happening?

It could be the line of code after the last line of code that is before the other line of code about the thing. On the other hand the thing before the other thing's code may not be the proper code for the first thing or the third thing. Of course, without your properly formatted code in code tags, the world may never know.

You probably need more data about what happens that causes you to need to reset including a better idea of the up time.
Since it's already online you can log a lot of this data.
You could have one of many issues, but I don't think there's enough data yet to determine it.

Of course the wiring schematics the OP provided are unreadable.

Hello, sorry about the lack of details. My code is as follows

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char* ssid = "MySpectrumWiFiEB-2G";
const char* password = "password";

WebServer server(80);

const int led = 2;
int dirPin  = 22;
int stepPin = 23;
int enPin   = 12;
String str;
String str1;
String forwardX;
int iforwardX;
String backwardX;
int ibackwardX;
String delayx;
int idelayx;
unsigned long previous_time = 0;
unsigned long delayi = 20000;  // 20 seconds delay
unsigned long current_time;
String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void setup(void) {
    pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, HIGH);
  digitalWrite(stepPin, HIGH);
    digitalWrite(enPin, LOW);
  digitalWrite(stepPin, LOW);
  digitalWrite(dirPin, LOW);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  }

    server.on("/godoor", []() {
    str1 = server.arg("todo");
      str= str1.substring(0,str1.length());
      if(str.indexOf("move")>=0) {
        server.send(200, "text/plain", "Motor should have moved ");
        forwardX =getValue(str,',',1);
        iforwardX = forwardX.toInt();
        backwardX =getValue(str,',',2);
        ibackwardX = backwardX.toInt();
        delayx = getValue(str,',',3);
        idelayx = delayx.toInt();
  digitalWrite(enPin, LOW);
  Serial.println("start");
  for (int i = 0; i < iforwardX; i++) {
    digitalWrite(stepPin, HIGH);
    delay(idelayx);
    digitalWrite(stepPin, LOW);
    delay(idelayx);
  }
  Serial.println("switch");
  digitalWrite(dirPin,HIGH);
  for (int i = 0; i < ibackwardX; i++) {
    digitalWrite(stepPin, HIGH);
    delay(idelayx);
    digitalWrite(stepPin, LOW);
    delay(idelayx);
  }
  Serial.println("stop");
  digitalWrite(dirPin,LOW);
  digitalWrite(enPin, HIGH);
  //Serial.println("loop");   // read it and send it out Serial (USB)
}
    
  });
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  current_time = millis(); // number of milliseconds since the upload
  if(current_time - previous_time >=delayi) {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print(millis());
    Serial.println("Reconnecting to WIFI network");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    previous_time = current_time;
  }
  }
  server.handleClient();
  delay(2);//allow the cpu to switch to other tasks
}

My wiring is as follows
From the ESP's pin 22, to the Easy Driver's dir pin.
From the ESP's pin 23, to the Easy Driver's step pin.
From the Gnd pin that is next to the step pin on the Easy Driver to the Gng pin on the ESP.
From the ESP's pin 12, to the Easy Driver's enable pin.
From the ESP's vin pin, to the Easy Driver's 5v output pin.
From the ESP's Gnd pin, to the Easy Driver's Gnd pin.
From the Easy Driver's 4 motor control pins to a stepper motor.
Then I have a 12v power supply running from the wall to the Easy Driver.

I think it very important to limit String size by using String.reserve(), especially when you are not using freeRTOS.

Before connecting to WiFi issue a WiFi.disconnect(), WiFi.disconnect() does 2 things. One thing is to disconnect from a WiFi connection, if connected. The 2nd thing WiFi.disconnect() does is reset the WiFi stack. You can look up how important it is to have a nicely set default stack before making w WiFI connection on your own.

delay(2), what other tasks?

The ESP32 is a 32bit MCU. millis() does not need a 64 bit number unless you want to use the more accurate ESP32 micros counter.

Does Arduino ESP have console output like ESP-IDF? It would be useful to monitor this with a computer for a few days and see if there's something that comes up.

Serial.prints to the monitor.

I woke up today to my ESP32 not connected to my wifi again so I plugged it into my computer, I didn't unplug the power to the Easy Driver, and without doing anything else, the ESP32 connected to the wifi and all was good again. I'm definitely a noob to wiring circuit boards so I'm just shooting in the dark here but could the Easy Driver not be delivering consistent power?

I suggest sticking a flash, or microsd onto your project and logging what is going on. Pay attention to how much ram you have. Troubleshooting in the dark is often less productive and more frustrating.

Hello all, just in case it's of any use to anyone, this is the code that has seemed to do the trick.

#include <esp_task_wdt.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char* ssid = "MySpectrumWiFiEB-2G";
const char* password = "Password";

WebServer server(80);
int retries =0;
const int led = 2;
int dirPin  = 22;
int stepPin = 23;
int enPin   = 12;
String str;
String str1;
String forwardX;
int iforwardX;
String backwardX;
int ibackwardX;
String delayx;
int idelayx;
unsigned long last;
unsigned long previous_time = 0;
unsigned long delayi = 20000;  // 20 seconds delay
unsigned long current_time;
String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void setup(void) {
    esp_task_wdt_init(30, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch
  delay(6000);
    pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, HIGH);
  digitalWrite(stepPin, HIGH);
    digitalWrite(enPin, LOW);
  digitalWrite(stepPin, LOW);
  digitalWrite(dirPin, LOW);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    retries++;
    if(retries>40) {
  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  retries=0;
    }
  }
    server.on("/godoor", []() {
    str1 = server.arg("todo");
      str= str1.substring(0,str1.length());
      if(str.indexOf("move")>=0) {
        server.send(200, "text/plain", "Motor should have moved ");
        forwardX =getValue(str,',',1);
        iforwardX = forwardX.toInt();
        backwardX =getValue(str,',',2);
        ibackwardX = backwardX.toInt();
        delayx = getValue(str,',',3);
        idelayx = delayx.toInt();
  digitalWrite(enPin, LOW);
  for (int i = 0; i < iforwardX; i++) {
    digitalWrite(stepPin, HIGH);
    delay(idelayx);
    digitalWrite(stepPin, LOW);
    delay(idelayx);
  }
  digitalWrite(dirPin,HIGH);
  for (int i = 0; i < ibackwardX; i++) {
    digitalWrite(stepPin, HIGH);
    delay(idelayx);
    digitalWrite(stepPin, LOW);
    delay(idelayx);
  }
  digitalWrite(dirPin,LOW);
  digitalWrite(enPin, HIGH);
}
    
  });
  server.begin();
}

void loop(void) {
  current_time = millis(); // number of milliseconds since the upload
    if (millis() - last >= 28000) {
      esp_task_wdt_reset();
      last = millis();
  }
  if(current_time - previous_time >=delayi) {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.disconnect();
    ESP.restart();
    //WiFi.begin(ssid, password);
    previous_time = current_time;
  }
  }
  server.handleClient();
  delay(2);//allow the cpu to switch to other tasks
}

I figured out that if the wifi network isn't available when the ESP32 powers up, it gets hung up so while connecting or reconnecting, every twenty seconds, I have it disconnect and establish a new connection. I also implemented watchdog. It has been going strong for about a week now.

1 Like

Excellent. Thanks for the working code follow-up.

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