Hello,
I have a project that uses Wemos D1 mini, a4988 driver and Nema 17 motor.
when i run the end point from browser, it runs ok. But when I use it via http widget, using android, it struggles, doesnt move much . with browser, if it rotates 7 times, with http widget - 20 degrees.
please can you tell, what is the reason? and solution
If you need connections, let me know. 5v supplied to wemos, 12v to a4988
"when i run the end point from browser, it runs ok. But when I use it via http widget, using android, it struggles, doesnt move much . with browser, if it rotates 7 times, with http widget - 20 degrees."
You might print the received data out to the serial monitor for analysis. There may be differences between how the widget and the browser are sending request, which may make a difference how the request are captured and handled.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
const char* ssid = "..";
const char* password = "...";
ESP8266WebServer server(80);
#define DIR_PIN D1
#define STEP_PIN D2
//const int dirPin = 5; //This pin corresponds to GPIO5 (D1)
//const int stepPin = 4; //This pin corresponds to GPIO4 (D2)
int steps = 0;
int stepDelay = 0;
bool dir = HIGH; //Rotation direction. HIGH is clockwise.
void setup() {
pinMode(DIR_PIN, OUTPUT); // Pins are outputs
pinMode(STEP_PIN, OUTPUT);
delay(1000);
Serial.begin(115200); //I can debbug through the serial port
// Configure NODEMCU as Access Point
Serial.println("Configuring access point...");
WiFi.softAP(ssid); //Password is not necessary
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRootPath); //I specify which code to execute when an HTTP request is performed to each path.
server.on("/Init", handleInit); //Associate the handler function to the path "/Init".
server.begin(); //Let's call the begin method on the server object to start the server.
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handleRootPath() {
server.send(200, "text/plain", "Ready, player one.");
}
void handleInit() {// Handler. 192.168.XXX.XXX/Init?Dir=HIGH&Delay=5&Steps=200 (One turn clockwise in one second)
steps = 0; //Motor stopped if the arguments are wrong.
stepDelay = 0;
String message = "Initialization with: ";
if (server.hasArg("Dir")) {
digitalWrite(DIR_PIN, server.arg("Dir") == "HIGH"); //This is a cunning way of checking the value of the argument Dir.
message += "Direction: ";
message += server.arg("Dir");
}
if (server.hasArg("Delay")) {
stepDelay = (server.arg("Delay")).toInt(); //Converts the string to integer.
message += " Delay: ";
message += server.arg("Delay");
}
if (server.hasArg("Steps")) {
steps = (server.arg("Steps")).toInt();
message += " Steps: ";
message += server.arg("Steps");
}
server.send(200, "text/plain", message); //It's better to return something so the browser don't get frustrated+
for (int i = 0; i < steps; i++) { //Create a square wave signal with the incoming data.
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(stepDelay);
yield();
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(stepDelay);
server.send(200, "text/plain", "count: "+i);
}
}
You may also notice that accurate and regular timing (regular pulses are required for the smooth running of a stepper) is not exactly one of the ESP8266's strong points, to say it nicely.
You may also notice that accurate and regular timing (regular pulses are required for the smooth running of a stepper) is not exactly one of the ESP8266's strong points, to say it nicely.
Interesting. I guess I was sort of thinking of the ESP8288 on the WeMos as a peripheral to an Atmel processor, but looking at the board, I don't see an Atmel processor. Good to know.
wvmarle:
You may also notice that accurate and regular timing (regular pulses are required for the smooth running of a stepper) is not exactly one of the ESP8266's strong points, to say it nicely.
I was using yield() in the for loop, so thought it is ok(? as it stopped restarting wemos)
I want to remotely control stepper motor Nema 17, please advice
yield() is important, it gives time to the required background processes, and saves you those lovely WDT RESET errors.
That are the same processes that may mess up your timing - e.g. if you do a web site request you lock up the WeMOS for a moment as it deals with it. So long as you don't step more than a couple hundred (micro)steps a second, you're probably fine, though PWM on the ESP8266 is prone to glitches. If you're trying to do >20k steps (above human hearing, makes a stepper less noisy) you're likely to see glitches. That's just the nature of the thing.