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();
}