Multitasking is not working with http request

Hi everyone, I am trying to do multitasking. I am using RP2040, hx711, ethernet, stepper, relay etc. I was doing great without ethernet. When I try to add ethernet to the system, multitasking is waiting a bit so multitasking is not working properly.

I was using 2 RFID, 2 steppers, 2 loadcells and 2 relay modules at the same time. For example, if two tags were read from 2 RFID reader modules, the stepper motors were working at the same time. I am doing this codes with millis() . But now I can't do multitasking, I think the problem is, ethernet library has delay.

I am using khoih-prog/EthernetWebServer library. HTTP client has these lines ;

#define kHttpWaitForDataDelay     1000L // Number of milliseconds that we wait each time there isn't any data
// available to be read (during status code and header processing)

delay(kHttpWaitForDataDelay); // We haven't got any data, so let's pause to allow some to
        // arrive

So is this the problem ?

My simple code structure is (I am trying to use only ethernet and hx711 same time with this code):

#include "defines.h"
#include <ArduinoJson.h>
#include "HX711.h"

const char serverAddress[] = "jsonplaceholder.typicode.com";  // server address
int port = 80;
EthernetClient  client;
EthernetHttpClient  httpClient(client, serverAddress, port);
class httpGetRequest
{
    unsigned long startTime = 0;
    long active_time;
  public:
    httpGetRequest(long active)
    {
      active_time = active;
    }
    void Update()
    {
      unsigned long setCurrentMillis = millis();
      if (setCurrentMillis - startTime >= active_time)
      {
        startTime = setCurrentMillis;
        String get_path = "/todos/1";
        httpClient.get(get_path);
        int statusCode = httpClient.responseStatusCode();
        Serial.println(statusCode);
        String response = httpClient.responseBody();
        Serial.print("Response: ");
        Serial.println(response);
        StaticJsonDocument<200> doc;
        deserializeJson(doc, response);
        int userId = doc["userId"];
        Serial.println(userId);   
      }
    }
};
class hx711
{
    HX711 scale;
    unsigned long startTime = 0;
    long active_time;
    int LOADCELL_DOUT_PIN ;
    int LOADCELL_SCK_PIN ;

  public :
    hx711(long active, int dout_pin, int sck_pin)
    {
      LOADCELL_DOUT_PIN = dout_pin;
      LOADCELL_SCK_PIN = sck_pin;
      active_time = active;
      scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
      scale.set_scale(1280.f);
      scale.tare();
    }
    void Update()
    {
      unsigned long setCurrentMillis = millis();
      if (setCurrentMillis - startTime >= active_time)
      {
        startTime = setCurrentMillis;
        float reading = (scale.get_units(1));
        Serial.println(reading);
      }
    }
};

httpGetRequest http_get_request(250);
hx711 hx_711(250, 14, 15);

void setup()
{
    Serial.begin (4800);
     // start the ethernet connection and the server:
  // Use DHCP dynamic IP and random mac
  uint16_t index = millis() % NUMBER_OF_MAC;
  // Use Static IP
  //Ethernet.begin(mac[index], ip);
  Ethernet.begin(mac[index]);
}

void loop()
{
hx_711.Update();
http_get_request.Update();

}

I try to use EthernetWebServer (delay(kHttpWaitForDataDelay)) like this,

unsigned long start_Time = 0;
unsigned long HttpWaitForDelay = millis();
        if(HttpWaitForDelay-start_Time>=200)
        {
            start_Time=HttpWaitForDelay;
        }

So far so good. Is this cause anything ?

you're possibly right, this line here

responseStatusCode();

is blocking, you can confirm this by introducing a word

return;

just before it ( this will stop HTTP activity though)

you can fiddle around with that function and make it 'unblocking'

OR you can do something like this

String hostServer = "jsonplaceholder.typicode.com";
int httpPort = 80;

 if ( ! client.connected() ) {
    if ( ! client.connect(hostServer.c_str(), httpPort) ) {
      Serial.println("\nserver connection failed");
      return;
    }
  }

String completePath = "/todos/1";

client.println("GET " + completePath + " HTTP/1.1\r\n" +
                  "Host: " + hostServer+ "\r\n" +
                  "Connection: close\r\n" +
                  "\r\n"
                 );

and then in your main loop you invoke some function that listens for the incoming server response

void  getHttpResponse() {
 if ( client.available() ) {
  String resp = "";
  while (client.available()) {
     resp += char(client.read());
    }
    Serial.println("server response: "+resp);
    client.stop();
  }
}
1 Like

Thank you, I will try them.

Hi again, I have another problem but I couldn't find it. When there is no internet, I started the code lines if ( ! client.connected() ) { } works after 60 second. Something has timeout like this I guess int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
but I couldn't find. I changed this 60000 to 1000 but nothing change. When I start the code lines without internet connection, code lines wait 60 seconds to start.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.