Traccar output to MQTT broker

Sumeet6 years ago

Hi
This is a great project with almost all the devices integrated! Thanks for that.

I wanted to know that after decoding the data, can it be sent to a MQTT broker as a JSON? If yes, please let me know how to do it or is there something already available. I do not want it to be saved into any DB, it just has to publish it to a configured broker.
Thanks in advance

Tony Shelver6 years ago

You may want to start with the API documentation. https://www.traccar.org/api-reference/

Sumeet6 years ago

Hi Tony
Thanks for your response. I looked at the API documentation. I am not able to find API which gives me the tracking information (Latitude, Longitude, Speed etc) of a device which sends data to traccar port.
Is it possible for me to push tracking data directly from within traccar code to mqtt? Can you point me out the file where I can do this?

Tony Shelver6 years ago

Sumeet, I am interested in exactly the same information as you, but haven't gotten very far as I am still trying to get Traccar going properly so we can test it.

We have an existing tracking system that I am not happy with, that if possible we will replace with Traccar.
It runs on Oracle, and has no APIs. On that system i hook directly into the tables and suck the data out that way.
Depending on the RDBMS you use, you can often trigger changes directly into a messaging queue.
We just wrote a monitoring system that reads the system for changes every minute or so.

This data is then loaded into Posgres for additional reporting and geolocation processing.

Not sure how advisable that approach is with Traccar, as you are not protected in changes to the data structures.

Sumeet6 years ago

Hi Tony,
Thanks for the response. I do not want another overhead of first saving the data into DB and then writing a trigger to push the same to MQTT. I think there is a way to publish the parsed packet from within traccar open source code.

Mayank6 years ago

Hey Sumeet, I think you might be interested in using the traccar Webhook event forwarding (u can enable it using config) where u can push the position data to some services which are pushing the same to MQTT. And regarding not storing the data in DB for positions, You might need to implement a custom config and based on that you can disable writing the positions data to DB. I am afraid to say to achieve this you might require to do some changes, but they will be minimal for sure.

Sumeet6 years ago

Hi Mayank
Thanks for the response.
Current status:

  1. Added a Broker config in xml and picked it in Context.java
  2. Established a one time connection to Broker
  3. I intend to use this connection and publish "position" object as JSON to the broker.

I have completed 1 and 2. But I am looking for a best place where I can implement 3.

Mayank6 years ago

Hey Sumit,

Before I can help you more, I have one question; Are u interested in Dashboard also showing the correct data for the devices or it's just you want to route the positions update coming to your MQTT broker.

Sumeet6 years ago

Hi Mayank,
We are not interested in the dashboard as we have our own custom app. We are only interested in receiving location data to Broker.

Mayank6 years ago

Hey Sumeet,

In that case, If you have some other service which can consume this json position object. You can enable the config as below to enable position forwarding;

<!-- position forwarding -->
<entry key='forward.enable'>true</entry>
<entry key='forward.json'>true</entry>
<entry key='forward.url'>http://<ip>:port/<path></entry>

If you prefer, Traccar can forward positions using query parameters: we can configure this by a disabling forward.json and specifying the parameters we’re interested in.

<entry key='forward.enable'>true</entry>
<entry key='forward.url'>http://<ip>:<port>/positions?id={uniqueId}&amp;lat={latitude}&amp;lon={longitude}</entry>
<entry key='forward.json'>false</entry>         

The list of possible query parameter values which can be interpolated I’ve taken from the source:

{name} is the name of a device
{uniqueId} its unique identifier
{protocol} the protocol through which a position was reported, e.g. "owntracks"
{fixTime} the time of fix
{latitude} and {longitude} the latitude, and longitude respectively
{altitude}, {speed}, {course}, and {accuracy} should be self-explanatory
{address} the reverse-geo-coded address if available

And then set some custom config and based on this do not write the data in the DB.

In case if you do not have the other service to consume the position json object, rather would like the traccar to handle this; In that case while saving the position object to DB, instead of saving the same to DB pass this to through the broker configured.