Simulate gps data with Qt for use with traccar


d_wheel

I am trying to write a GPS simulator using QT c++. It will be used to send data to the Traccar tracking app. Traccar uses localhost port 5055 to receive packets and plot the location using the data in the packets. I can't successfully read the packets through the tracking car. Traccar expects to see packets in HTML format. I'm not sure if my program formats the data correctly. All lines starting with //\ show what I've tried.

This is 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 logs show 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 logs show that the program is connecting, but the data has not yet been decoded. I know how Traccar works because I can send decoded and drawn data from a web browser.

Any advice would be greatly appreciated.

Alexey Tsybin

try this. it worked 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;

    }
}

Related