I control 2 step motors with my android, but always after a "comand" the Esp give me a WDT error and reset
My code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <AccelStepper.h> // Responsavel pelas funções dos motores
#include <MultiStepper.h> // Controle de multiplos motores
#define interface 8
half stepper
#define motorPinD1 5 // IN1 ULN2003 driver 1
#define motorPinD2 4 // IN2 ULN2003 driver 1
#define motorPinD3 0 // IN3 ULN2003 driver 1
#define motorPinD4 2 // IN4 ULN2003 driver 1
#define motorPinD5 14 // IN1 ULN2003 driver 2
#define motorPinD6 12 // IN2 ULN2003 driver 2
#define motorPinD7 13 // IN3 LN2003 driver 2
#define motorPinD8 15 // IN4 ULN2003 driver 2
AccelStepper motor1(interface, motorPinD1, motorPinD3, motorPinD2, motorPinD4);
AccelStepper motor2(interface, motorPinD5, motorPinD7, motorPinD6, motorPinD8);
MultiStepper motores;
int motorVel = 1000;
int command = 001;
int cmdPassado;
long posicao[2];
long posicaoA[2];
const char *ssid = "MotoG";
const char *password = "tetando123";
ESP8266WebServer server(80);
/* Just a little test message. Go to http://192.168.4.1 in a web browser
connected to this access point to see it.
*/
void handleRoot() {
//server.send(200, "text/html", "<h1>You are connected</h1>");
String cmd;
cmd += "<!DOCTYPE HTML>\r\n";
cmd += "<html>\r\n";
//cmd += "<header><title>ESP8266 Webserver</title><h1>\"ESP8266 Web Server Control\"</h1></header>";
cmd += "<head>";
cmd += "<meta http-equiv='refresh' content='5'/>";
cmd += "</head>";
switch (command) {
case 010:
cmd += ("<br/>Device FRONT");
break;
case 011:
cmd += ("<br/>Device BACK");
break;
case 110:
cmd += ("<br/>Device LEFT");
break;
case 111:
cmd += ("<br/>Device RIGHT");
break;
default:
cmd += ("<br/>Device STOP");
break;
}
cmd += "<html>\r\n";
server.send(200, "text/html", cmd);
}
void handleNotFound() {
//digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
//digitalWrite(led, 0);
}
void setup() {
wdt_disable();
// Motores
motor1.setMaxSpeed(1000);
motor1.setSpeed(motorVel);
motor2.setMaxSpeed(1000);
motor2.setSpeed(motorVel);
motores.addStepper(motor1);
motores.addStepper(motor2);
Serial.begin(9600);
//WiFi.softAP(ssid, password);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/status001", []() {
server.send(200, "text/plain", "Device Stop");
command = 001;
});
server.on("/status010", []() {
server.send(200, "text/plain", "Device Frente");
command = 010;
});
server.on("/status011", []() {
server.send(200, "text/plain", "Device Tras");
command = 011;
});
server.on("/status110", []() {
server.send(200, "text/plain", "Device ESQ");
command = 110;
});
server.on("/status111", []() {
server.send(200, "text/plain", "Device DIR");
command = 111;
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("Servidor Iniciado!");
}
void loop() {
server.handleClient();
switch (command) {
case 010:
posicao[0] = 100 + posicaoA[0];
posicao[1] = 100 + posicaoA[1];
motores.moveTo(posicao);
motores.runSpeedToPosition(); // Blocks until all are in position
break;
case 011:
posicao[0] = -100;
posicao[1] = -100;
motores.moveTo(posicao);
motores.runSpeedToPosition(); // Blocks until all are in position
break;
case 110:
posicao[0] = 100;
posicao[1] = 100;
motores.moveTo(posicao);
motores.runSpeedToPosition(); // Blocks until all are in position
break;
case 111:
posicao[0] = 100;
posicao[1] = 100;
motores.moveTo(posicao);
motores.runSpeedToPosition(); // Blocks until all are in position
break;
case 001:
motor1.stop();
motor2.stop();
break;
}
posicaoA[0]+=posicao[0];
posicaoA[1]+=posicao[1];
}