Chat freely about anything...

User avatar
By emvee
#24456 First post here! :D

So I am running the latest modified Electrodragon AT firmware and using the Arduino IDE. I am using the ESP8266WEBSERVER example and have some html buttons I made that use jquery to turn on a neopixel ring. Each button is for a different animation, for example Rainbow. I made functions with infinite while loops which call the animation functions. these are mapped with server.on() So when I hit a button, the animation starts but it gets caught in the infinite loop and the webserver hangs because of the loop.

How can I run these infinite loops in the background but accept another request for another animation? Basically I want to change animations and when changed have them loop infinitely until its changed again.
User avatar
By emvee
#24631 Got it to work! Needed to stop the wifi client and start it back up within the loop (made a function for this), create a status variable, create an if statement that breaks the loop if its not equal to the status for any animation. This also now lets me support multiple connections somewhat in ESP8266 Web Server works fine with simple GET requests..
User avatar
By batman masterson
#24655 I'm planning a similar project and considered doing the same thing. What impact does starting/stopping the connection and checking for HTTP calls inside your animation loop have on the speed of the animations themselves? Can you share the code to show how you implemented this?
User avatar
By emvee
#24697 It restarts pretty much instantaneously, I don't see any lag and it break out of the loop into another animation very quickly. Animations themselves run smooth and are unaffected. The ESP8266WebServer itself can be little bit buggy at times though and lag but not very often. I hear adding small delays the main loop helps. You can use the Hello Web Server sketch as a foundation, I'm sure you could normalize some of this code to make it more efficient but it works well so far. Add as many animations as you want using this. The only issue I see with resetting the client is that if another client was uploading large data to the esp or having a session, they would be disconnected, but for simple GET requests it works fine. The ESP8266WebServer was only supposed to support one connection at a time but this is a workaround for multiple connections

Here is the code:

Code: Select all//declare client
WiFiClient client;

//declare status with default value
String status = "null";

//create reset client function
void resetClient() {
client.stop();
server.begin();
server.handleClient();
}

//Handle Animation1
void handleAnimation1() {
status = "animation1";
while (status == "animation1") {
animation1();
}
}

\\Animation Function
void animation1(){
for loop here {
//this resets the client so it can listen for incoming connection while in the loop
resetClient();
if (status != "animation1") {
break;
}
}

//Listen for "/animation1" GET request and run HandleAnimation1
server.on("/animation1" handleAnimation1);