2

I have a Arduino UNO WiFi Rev2 board that runs WiFiServer serving as a web server. The server listens and accepts clients using server.available(). I also want a loop that controls a servo in concurrent of the server.

I looked at ArduinoThread and tried to implement my web server with it.

class WebServer : public Thread
{
private:
    // Some method definition omitted 
    WiFiServer server;
    void accept_and_process();

public:
    void run() override;
}

void WebServer::accept_and_process()
{
    Serial.println("Waiting for client");
    // listen for incoming clients
    WiFiClient client = server.available();
    // Process client
}

void WebServer::run()
{
    this->accept_and_process();
    this->runned();
}

But the code does not run in parallel with code in void loop() it only ran code inside loop function and does not accept client as expected
This is the code I ran the server with

void setup()
{
    Serial.begin(9600);
    WebServer webserver = WebServer();;

    webserver.setInterval(100);
    webserver.run();
}

void loop()
{
    Serial.println("Hello");
    delay(1000);
}

How would I run the server concurrent to another function.

5
  • There is no “parallel” on a single core arduino. All you need to do is program both things in a non blocking fashion. Don’t use delay or while loops waiting on things and you can do both things so fast it seems like they are parallel. There are tons of tutorials on non blocking code. Or post some actual compilable code here and maybe someone can help you see how. – Delta_G Aug 26 '20 at 17:06
  • @Delta_G thanks for the suggestion, I moved server.available() into void loop() to unblock the execution. I'm still experimenting with it but now everything should work as expected. – sqz Aug 26 '20 at 17:40
  • I never expected server.available() to be non-blocking because I assume it's basically socket.accept() in other languages which for most of the time, is blocking. – sqz Aug 26 '20 at 17:45
  • No. server.available doesn’t wait for anything. It just immediately returns whether or not there is a connection available. If there’s not then it returns false and moves on. Don’t assume. There’s a great adage about what happens when you assume things. Look it up if you don’t know it. – Delta_G Aug 26 '20 at 18:25
  • @Delta_G hey do you mind posting your comments as answer so I can accept it? – sqz Aug 26 '20 at 19:10
3

There is no “parallel” on a single core arduino. All you need to do is program both things in a non blocking fashion. Don’t use delay or while loops waiting on things and you can do both things so fast it seems like they are parallel. Put a call to server.available() in loop and then if it returns an invalid client object then the rest of the loop runs. If it returns a valid client object then you can quickly do what you need with the server and then get back to everything else.

As noted in the comments, server.available is not blocking. It doesn't wait for anything to come, it simply returns whether there is a connection available or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.