I want to control a stepper motor by a web gui on an ESP32.
That is overall working, but I have the issue, taht the stepper motor needs to run for a long time.
My web-gui is not available for the time the stepper motor is running.
is it possible to have the motor running in background and have the webgui available, so it would be possible to stop the stepper motor?
}
if (request.indexOf("/door=open") != -1){
//digitalWrite(ledPin, LOW);
Serial.println("Oeffne Tuer.");
// value = LOW;
stop();
int i=1;
while(i<turns){
motorright(highSpeed);
i++;
}
stop();
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("");
client.println("");
client.println("Click <a href="/door=close">here Close
");
client.println("Click <a href="/door=open">here Open
");
client.println("");
thanks for your comments.
i use the delay for throtteling the loop. does that make sense anyway?
however, i found a library "espasynctcp" which supports listening for events in the webgui, so i don't need to use a loop.
so i can react on events like "open" or "close" or "stop motor"(if it is running and i want to top any movement).
will that work (explanation of what i need to know follows)?
open the webinterface and request "open"
within the action i will start the motor. it should run until a gpio state is changed (that is not the problem here)
request to "stop" the motor via webgui (!)
the last point is something where i am unsure if that could work, because the action within the "open"-request would still run - will an espasynctcp event reach the arduino during this action?
If I tell you to tap dance for 5 minutes or until I tell you to stop, can YOU do that? If you can't tap dance, choose some other activity that you enjoy, and substitute that in place of tap dancing.
If you write non-blocking code, it is possible make something happen, like stepping a stepper motor or advancing a servo, while listening for commands. If you write blocking code, it isn't. So, don't write blocking code.
thanks a lot. If I understnad correctly, then blocking code = loop(), non blocking code working by interrupts (hope it is the right vocabulary)?
then my qustion: when using espasynctcp, which means non blocking code (right?), and i start the motor which takes some minutes to complete, will i be able to send other commands (e.g. to stop the motor)? or is that blocking code again, because, the motor is running (and i need to switch the pins all the time)?
If I understnad correctly, then blocking code = loop(), non blocking code working by interrupts (hope it is the right vocabulary)?
No, loop() is not necessarily blocking code. delay() is a blocking function, because nothing else can happen during the delay. A for loop is often a blocking statement, because the for statement needs to complete all the iterations before anything else can happen.
Whether espasynctcp is blocking, or not, is impossible to say, because you didn't post a link to the library. I'd guess not, though. You should be able to tell the stepper to stop tap dancing even if the time you told it to tap dance has not elapsed. It depends on how you write your part of the code.
int i=1;
while(i<turns){
motorright(highSpeed);
i++;
}
is blocking code, because nothing else happens until that loop is complete.
The loop() function loops. Let it handle all the looping needed. On every pass through loop(), it may, or may not, be time to take a step. There may, or may not, be steps still to take. Call motorright() (or it's replacement with a camelCase name) only when there is still another step to take, and it is time to step.