stop in while

void costel() { 
String message="";
String gcodex="";
String gcodey="";
char t;
int a;
String inceput= "<!DOCTYPE html><html><head><script>document.body.innerHTML ='';</script>";
inceput +="<meta name='viewport' content='width=device-width, initial-scale=1.0'></head><body><svg height='500' width='500'>";
inceput +="<g transform='scale(2)'><polyline points='";
String sfarsit ="' style='fill:white;stroke:red;stroke-width:.5' />";
sfarsit +="Sorry, your browser does not support inline SVG.</svg></body></html>";
String puncte="";

 File f = SPIFFS.open("/test.nc", "r");
 while(f.available()) {
     t = f.read();
     // G01 X123.456 Y567.890\n
     message += t;
     if(t=='X'){a=1;gcodex="";} //read line until 'X' (attention X high!!!)
     if(t=='Y'){a=2;gcodey="";} //read line until 'Y' (attention Y high!!!)
     if(t=='\n'){ //if line end
       a=0;
       puncte +=gcodex+','+gcodey+' ';
       server.send(100, "text/html", inceput+puncte+sfarsit); //on any new line response to server with svg + new coordinate
       process_string(message,message.length()); //send gcode line to gcode interpreter
       message="";       //empty gcode string
       
       }
     if(a==1) if(t !='X' && t !=' ') gcodex +=t; //write svg coordonate without 'X' and space
     if(a==2) if(t !='Y' && t !=' ') gcodey +=t; //write svg coordonate without 'X' and space
     }
f.close();
server.send(200, "text/html", "ok" );
}
while(f.available()) {

So with this subrutine , read line by line and send to gcode interpreter.

Work fine!

But i can not interrupt this rutine after to finish read file!

Esp8266 not rispond at any web command until finish read file!

I need because in case is something wrong … i not want wait until finish cut image for attend that module rispond again or

interrupt power for stop cnc!

Thanks in advance!

If you wish to be able to stop before a WHILE completes then use IF rather than WHILE and allow loop() to do the repetition.

...R

While or loop , do not let esp8266 processor to call another subroutine until while or loop ends!

example:

bool state=true;

server.on("/stop",stop);

void stop() { 
state=false;
} 

void gcode() { 
 File f = SPIFFS.open("/test.nc", "r");
 while(f.available() && state) {
.
.
 process_string(message,message.length());
.
.
 } 
f.close();
server.send(200, "text/html", "ok" ); 
server=true;   
}

If i put

"while(f.available() && state)"

I can not change state until while not finished! . . I not understand your response!

You can make a simple example?

Regards!

There is no loop() function in the code in Reply #2 so I can’t see the complete context.

What I have in mind is changing this

while(f.available() && state) {

to

if(f.available() && state) {

If you need more help please post a complete program that illustrates the problem.

…R

You can use a break statement to exit a while loop. Inside your loop, test for the condition that you wish to have interrupt, and if so, break. This will exit the while loop and continue processing with the code immediately following. There are other ways of doing it, but as most of the under 40 crowd are phobic against the use of GOTO, I'll leave the study of that up to you.

DKWatson … yes … exactly that want… but how?

Regards!

here is complete program… i have many subroutines … is not ordinated program…

the costel subrutine (that interested) is in custom.ino

so… i thing that i need a function that can call if client send a message or if client get stop function

but i dont know how get this!

The esp8266 is set as server with “WiFi.mode(WIFI_AP);”

Regards!

costycnc.wifi.2.0.zip (11.8 KB)

Don't attach a zip file, most will not download and extract it. Post your code as you have above or at leas the segment in question. As to how you do it, read #4, again.

The function is in a head of topic!

void costel() { 
String message="";
String gcodex="";
String gcodey="";
char t;
int a;
String inceput= "<!DOCTYPE html><html><head><script>document.body.innerHTML ='';</script>";
inceput +="<meta name='viewport' content='width=device-width, initial-scale=1.0'></head><body><svg height='500' width='500'>";
inceput +="<g transform='scale(2)'><polyline points='";
String sfarsit ="' style='fill:white;stroke:red;stroke-width:.5' />";
sfarsit +="Sorry, your browser does not support inline SVG.</svg></body></html>";
String puncte="";

 File f = SPIFFS.open("/test.nc", "r");
 while(f.available()) {
     t = f.read();
     // G01 X123.456 Y567.890\n
     message += t;
     if(t=='X'){a=1;gcodex="";} //read line until 'X' (attention X high!!!)
     if(t=='Y'){a=2;gcodey="";} //read line until 'Y' (attention Y high!!!)
     if(t=='\n'){ //if line end
       a=0;
       puncte +=gcodex+','+gcodey+' ';
       server.send(100, "text/html", inceput+puncte+sfarsit); //on any new line response to server with svg + new coordinate
       process_string(message,message.length()); //send gcode line to gcode interpreter
       message="";       //empty gcode string
       
       }
     if(a==1) if(t !='X' && t !=' ') gcodex +=t; //write svg coordonate without 'X' and space
     if(a==2) if(t !='Y' && t !=' ') gcodey +=t; //write svg coordonate without 'X' and space
     }
f.close();
server.send(200, "text/html", "ok" );
}

So… when client get “costel” function …

the function open gcode file and read line by line and send to gcode interpreter

when finished to read all lines …close file and rispond with 200 for close connection

problem is that esp8266 not read another client messsage until not finish to read file and close

connection!

I think that i can not apply the “if” function here… maybe a test if client send a message in this time … and if receive a message …i can close file and connection

So, you need to define what event will happen outside from the while loop that will make you want to stop what you're doing. Maybe it's a button on pinx -> if(button attached to pinx has been pushed) break; Place a test similar to this inside your loop for whatever condition(s) you need to cause the loop to abort. It could even be a timer, which is done often just in case there's and unidentified read error.

The event is when client send a message!

But how create this event?

or how create a event at this:

server.on("/stop",stop);

a event when client get stop function!

How do you know if the client has sent a message?

I first that ask this! :)

I find something in https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiEvents/WiFiEvents.ino

I study now!

Forget that you are doing anything else. If your program was sitting around doing nothing, how would you know if the client has sent you a message? For example, if we are using Serial communications we constantly check if(Serial.available()) to see if there is incoming data. There must be some similar test you make to determine if there is a message from the client.

yes...someone know ... but not want did! :) Anyway ... is not a close while ... because in this while is a "yield();" function Without this "yield();" the esp8266 crash! This "yield();" i read that is a function that read another incoming mesages from server or another things ... but for my program not happen!

Yielding

This is one of the most critical differences between the ESP8266 and a more classical Arduino microcontroller. The ESP8266 runs a lot of utility functions in the background – keeping WiFi connected, managing the TCP/IP stack, and performing other duties. Blocking these functions from running can cause the ESP8266 to crash and reset itself. To avoid these mysterious resets, avoid long, blocking loops in your sketch.

The amazing creators of the ESP8266 Arduino libraries also implemented a yield() function, which calls on the background functions to allow them to do their things.

Do you not know how to respond to web commands? That is a completely different issue.

has nothing to do with web commands!

I need a event !

costy35: The function is in a head of topic!

You have still not posted a complete program - and IMHO the problem needs to be solved in the context of the complete program.

...R

costy35:
has nothing to do with web commands!

I need a event !

If there is no event you need nothing. If there is an event, you know it. It’s your code. You want someone to fabricate an event for you? Have you no idea what your own code does?

The program is hudge and i make tons of modifications ! The program work fine ! Only that need to wait until esp8266 finish to read file . Any read line ...the motor spin for few millseconds or seconds. But i have hundred of lines in program... so until read all lines and motors spin ... maybe pass minutes ... in this time the esp8266 is blocking!!! What part need put here?

costy35: The program work fine !

Glad to hear you have sorted it out.

...R