Hello I'm building a project that makes a connection to Processing application, I need to send a message from processing (e.g. "read") and send it via ethernet/wifi to my Arduino, process it and send a response back to the app. When doing so I noticed that since I send the message, until I receive the response the time seems to be about 125ms, a closer inspection revealed that most of the time is spent in the client.connected() instruction.
I'm using a Portenta H7 with the Portenta Breakout Board
#include <PortentaEthernet.h>
#include <Ethernet.h>
bool NDR = false;
EthernetServer server(10932);
String dataRec = "";
byte mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
bool new_data_in = false;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
while(!Ethernet.begin(mac)){ //Begin Ethernet with DHCP
delay(1000);
Serial.println("...");
}
server.begin();
}
void loop() {
EthernetClient client = server.available();
client.setTimeout(50);
if (client) {
unsigned long int time_a = millis();
while(client.connected()) {
if(client.available() > 0) {
char c = client.read();
dataRec += c;
new_data_in = true;
}
if(client.available() <= 0 && new_data_in == true){
new_data_in = false;
NDR = true;
unsigned long int time_b = millis();
printDelta(time_a, time_b, "time_a", "time_b");
}
if(NDR){
client.print(dataRec);
dataRec = "";
}
}
// close the connection:
client.stop();
}
}
void printDelta(unsigned long int start_time, unsigned long int end_time, String start_name, String end_name){
Serial.print("It took ");
Serial.print(end_time - start_time);
Serial.print(" ms from ");
Serial.print(start_name);
Serial.print(" to ");
Serial.println(end_name);
}
the output says:
It took 105 ms from time_a to time_b
How can I make the code faster or more efficient?, Is there any change I can make even if it is to the library? 100ms is way too much and a dealbreaker If I can't make it faster. I hope I'm doing something wrong and has nothing to do with the library...
Edit: I ran the same sketch adding the SPI library and CS pin on an Arduino UNO with an ethernet Shield.
#include <SPI.h>
#include <Ethernet.h>
bool NDR = false;
EthernetServer server(10932);
String dataRec = "";
byte mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
bool new_data_in = false;
void setup() {
Ethernet.init(10); // Most Arduino shields
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
while(!Ethernet.begin(mac)){ //Begin Ethernet with DHCP
delay(1000);
Serial.println("...");
}
server.begin();
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
client.setTimeout(50);
if (client) {
unsigned long int time_a = millis();
while(client.connected()) {
if(client.available() > 0) {
char c = client.read();
dataRec += c;
new_data_in = true;
}
if(client.available() <= 0 && new_data_in == true){
new_data_in = false;
NDR = true;
unsigned long int time_b = millis();
printDelta(time_a, time_b, "time_a", "time_b");
}
if(NDR){
client.print(dataRec);
dataRec = "";
}
}
// close the connection:
client.stop();
}
}
void printDelta(unsigned long int start_time, unsigned long int end_time, String start_name, String end_name){
Serial.print("It took ");
Serial.print(end_time - start_time);
Serial.print(" ms from ");
Serial.print(start_name);
Serial.print(" to ");
Serial.println(end_name);
}
This was the result:
It took 1 ms from time_a to time_b
It took 1 ms from time_a to time_b
It took 2 ms from time_a to time_b
It took 1 ms from time_a to time_b
It took 1 ms from time_a to time_b
HOW???
Thanks!