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.

sqz
  • 123
  • 2

1 Answers1

2

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.

Juraj
  • 18,264
  • 4
  • 31
  • 49
Delta_G
  • 3,391
  • 2
  • 13
  • 24