1

I am attempting to write a GPS emulator with QT c++. It will be used to send data to Traccar tracking app. Traccar uses localhost port 5055 to receive packets and uses the data from the packets to plot a location. I have not been successful in getting traccar to read the packets. Traccar expects to see the packet in HTML format. I am not sure if my program is formatting the data correctly. All of the lines starting with //\ show what I have tried.

This is the part of the program that writes data to loalhost port 5055:

// write to localhost if timer seconds is greater than or equal to desired time
if(scnds >= ui -> updateBox -> text().toDouble()) { 
 //\\   QString sendString = "/?id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$ + "\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "/?id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$;
 //\\   QString sendString = "id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$ + "\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "id=" + id$ + "&Lat=" +lat$ + "&Lon=" + lon$ + "&Speed=" + speed$ + "&Course=" + course$;
    QString sendString = "/?id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "/?id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45";
 //\\   QString sendString = "id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45\r\n\r\n\r\n\r\n";
 //\\   QString sendString = "id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45";
    QByteArray gpsdata = sendString.toUtf8();//     .toLocal8Bit(); .toUtf8(); .toLatin1();    
    socket = new QTcpSocket(this);
    socket -> connectToHost("localhost",5055);
    if (socket -> waitForConnected(3000)){
        ui -> receivedMessage -> append("Connected");
        socket -> write(gpsdata);
        socket -> waitForBytesWritten(1000);
        socket -> waitForReadyRead(3000);
        ui -> receivedMessage -> append(QString::number(socket -> bytesAvailable()) + " ");
        ui -> receivedMessage -> append(socket -> readAll());
        socket -> close();
        ui -> sentMessage -> setText(gpsdata);
    }
    else {
        ui -> receivedMessage -> append("Not connected");
        ui -> timerLabel -> setNum(0);
    }

The tracca log shows the following:

2020-01-18 18:36:41  INFO: [afa16fa9] connected
2020-01-18 18:36:41  INFO: [afa16fa9: osmand < 0:0:0:0:0:0:0:1] HEX: 2f3f69643d313233343536266c61743d33322e35343738266c6f6e3d2d39392e373435322674696d657374616d703d302668646f703d3526616c7469747564653d3130302673706565643d31303020485454502f312e31200d0a0d0a0d0a0d0a
2020-01-18 18:36:41  INFO: [afa16fa9: osmand > 0:0:0:0:0:0:0:1] HEX: 485454502f312e31203430302042616420526571756573740d0a636f6e74656e742d6c656e6774683a20300d0a0d0a
2020-01-18 18:36:41  INFO: [afa16fa9] disconnected

As you can see, the traccar log shows that the program is connecting, but the data is not being decoded. I know traccar works because I can send data from a web browser which is decoded and plotted.

Any sugestions would be appreciated.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
d_wheel
  • 39
  • 1
  • 7
  • HTTP/1.1 400 Bad Request content-length: 0 - that what exactly app answer to you. Demo request: "http://demo.traccar.org:5055/?id=123456&lat={0}&lon={1}&timestamp={2}&hdop={3}&altitude={4}&speed={5}" – Alexey Tsybin Jan 21 '20 at 06:28
  • And also it show that u send /id=123456&lat=32.5478&lon=-99.7452&timestamp=0&hdop=5&altitude=100&speed=100 HTTP/1.1 NOT "/id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45\r\n\r\n\r\n\r\n"; – Alexey Tsybin Jan 21 '20 at 06:45
  • You are correct. I did not use a log report from the exact data sent. It was picked at random because I have tried many, many times and all have ended the same way. Following is included an example of a request and log from that request – d_wheel Jan 21 '20 at 14:42
  • QString sendString = "/?id=123456&lat=32.5478&lon=-99.7452&speed=100\r\n\r\n\r\n\r\n"; Connected 2020-01-21 08:27:52 INFO: [1142130e] connected 2020-01-21 08:27:52 INFO: [1142130e: osmand < 0:0:0:0:0:0:0:1] HEX: 2f3f69643d313233343536266c61743d33322e35343738266c6f6e3d2d39392e373435322673706565643d3130300d0a0d0a0d0a0d0a 2020-01-21 08:27:52 INFO: [1142130e: osmand > 0:0:0:0:0:0:0:1] HEX: 485454502f312e31203430302042616420526571756573740d0a636f6e74656e742d6c656e6774683a20300d0a0d0a 2020-01-21 08:27:52 INFO: [1142130e] disconnected – d_wheel Jan 21 '20 at 14:45
  • use `QByteArray gpsdata = "/?id=123456&lat=40.730610&lon=-73.935600&speed=100&Course=45\r\n\r\n\r\n\r\n"` directly. no need to create a Qstring then convert back – UmNyobe Jan 22 '20 at 09:20

1 Answers1

1

Try this. Its work for me:

void MainWindow::on_pushButton_3_clicked()
{
    QString sendString = "/id=123456&lat=50.030610&lon=33.335600&speed=100&Course=45";
    QUrl theurl;
    theurl = "http://127.0.0.1:5055";

    QByteArray gpsdata = sendString.toUtf8();
    qDebug()<<gpsdata;
    socket = new QTcpSocket(this);

    socket -> connectToHost(theurl.host(),5055);
    if (socket -> waitForConnected(3000)){
        socket->write("GET /?id=123456&lat=50.030610&lon=33.335600&speed=100&Course=45 HTTP/1.1\r\n"
                      "host: " + theurl.host().toUtf8() + "\r\n\r\n");
        socket -> waitForBytesWritten(1000);
        socket -> waitForReadyRead(1000);
        QByteArray ba;
        ba.append(socket -> readAll());
        socket -> close();
        qDebug()<<ba;

    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alexey Tsybin
  • 220
  • 1
  • 6