I'm trying to control a robot wirelessly through an Arduino (using a X360 controller on a computer), which requires very low latency. I chose Wifi for this reason (and the fact I'll be streaming video), and after a little test it turns out I have a huge lag using TCP. Is this normal (with 54Mbits/s, it shouldn't!)? How can I reduce it to be controllable?
Server code (Arduino sketch):
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x48, 0x0D };
byte ip[] = { 192, 168, 0, 11 };
byte gateway[] = { 192, 168, 0, 254 };
byte subnet[] = { 255, 255, 255, 0 };
byte localPort = 99;
EthernetServer server = EthernetServer(localPort);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
// start listening for clients
server.begin();
Serial.println("Server ready");
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
Serial.println("Received:");
byte received = 0;
while((received = client.read()) != -1) {
Serial.println(received);
server.write(received);
}
Serial.println("Over\n");
}
}
Client code (PC, QtCreator):
#include <QTextStream>
#include <QTCPSocket>
QString arduinoIP = "192.168.0.11";
char arduinoPort = 99;
int main(void)
{
QTcpSocket socket;
QTextStream in(stdin);
QTextStream out(stdout);
out << "Connection... "; out.flush();
socket.connectToHost(arduinoIP, arduinoPort);
if(!socket.waitForConnected(5000)) {
out << socket.errorString() << "\n";
}
else {
out << "OK\n"; out.flush(); //I don't know why \n doesn't flush
out << "Type a message to send to the Arduino or quit to exit\n"; out.flush();
QString command;
in >> command;
while(command != "quit") {
QByteArray bufOut = command.toUtf8();
socket.write(bufOut);
socket.waitForReadyRead(1000); //Wait for answer (temp)
out << "Answer: " << socket.readAll() << "\n";
}
}
return 0;
}
Thank you in advance for your help.
Regards, Mister Mystère