Reputation: 39
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.
Upvotes: 1
Views: 350
Reputation: 220
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;
}
}
Upvotes: 1