This post describes how to visualize Mqtt data from the OwnTracks App.
For this experiment I installed OwnTracks onto my phone and filled in my credentials for my Mqtt broker.
Problem 1: My Mqtt broker uses port 14027. Outgoing data to this port wasn’t allowed on the particular Wifi network I was using. This took me ages to work out and was solved simply by turning off Wifi and using the phone data connection.
When this happens OwnTracks won’t fall back and use a different data connection so you might get delayed tracking data when a user phone moves in and out of particular Wifi networks.
The OwnTracks format is well documented, see here. It is a little verbose but it does contain longitude, latitude and speed. Be aware that the data can change depending on your phone platform. By default the topic is in the format owntracks/user/device. Both user and device can be set in the App. I actually changed this to owntracks/device.
Here’s an example of the two types of packets that I received from my Android device :
{ "_type":"location",
"acc":8,
"alt":56,
"batt":75,
"conn":"m",
"lat":53.8165549,
"lon":-1.6013796,
"tid":"1",
"tst":1557255979,
"vac":8,
"vel":0
}
{ "_type":"lwt",
"tst":1557286504
}
The lwt type is a special ‘last will and testament’ type. It is sent when the device disconnects from the Mqtt broker. This can happen often, particularly if you are travelling!
Problem 2: I need to combine the deviceid from the topic and the lng/lats and timestamp from the payload to get a meaningful piece of data to plot.
Problem 3: What do I do with the lwt (last will and testament) message? This tells me when the device disconnected, although I can’t find a corresponding ‘device has connected’ message.
I can change or shape the data by using a mqttcogs_msg_in_pre filter hook. This is called just before the data is persisted to the wordpress database. I’m going to do aim to do the following:
- Ignore messages that aren’t of type ‘location’
- Use the GPS datetime rather than the server time in the utc field
- Add a new field ‘Desc’ that I actually never use. This is simply to demonstrate how the payload could be manipulated.
add_filter('mqttcogs_msg_in_pre', 'mqttcogs_msg_in_pre', 10, 3);
function mqttcogs_msg_in_pre($publish_object, $datetime)
{
$topic = $publish_object->getTopic();
//filter by topic
if (!(substr( $topic, 0, 9 ) === "owntracks")) {
return $publish_object;
}
$mqttmsg = $publish_object->getMessage();
$payload = json_decode($mqttmsg);
//only do this to correct type of messages
if ($payload->_type != 'location') {
return NULL;
}
//update utc time to GPS datetime
if (property_exists($payload, 'tst')) {
$datetime->setTimestamp((int) $payload->tst);
}
//Now manipulate the payload. This extra attribute isn't actually
//used!
$speed = $payload->vel;
$nicedatetime = $datetime->format('Y-m-d H:i:s');
$payload->Desc = 'Speed:'.$speed.'kph At:'.$nicedatetime;
$publish_object->setMessage(json_encode($payload));
return $publish_object;
}
….and here is the result. I’ve included the shortcode below. Remember that as of version 2.2 the longitude and latitude will be automatically extracted for the Map Visualization. Map looks good but the Google Map Visualization isn’t set up to refresh very nicely. When you set the refresh_secs=”60″ and the data is refreshed, this particular visualization draws the Map tiles and resets the zoom level. Not very useful!
[ mqttcogs_drawgoogle
refresh_secs="120"
charttype="Map"
options="{height:300,zoomLevel:12,showTooltip: true,showInfoWindow:true,showLine:true}"]
[ mqttcogs_data
limit="1"
order="DESC"
topics="owntracks/ckandroid"]
[ /mqttcogs_drawgoogle]
2 Comments
Hi,
Trying to use your mqttcogs plugin for wordpress and i’m getting the following from the broker (which is correct):
tayport/logger{“photovoltaicmeter”:0,”mcp01meter”:13979,”sportspitchlightingmeter”:0,”externallightingmeter”:104,”dblp1meter”:13620,”dblp2meter”:3806,”dbk2meter”:3518,”carchargemeter”:0,”platformliftmeter”:0,”dbk1meter”:8,”mainelectricmeter”:0,”maingasmeter”:868,”mainwatermeter”:1230,”dhwboostercwsmeter”:152,”kitchencwsmeter”:0,”kitchenboostercwsmeter”:0,”oat”:0.7,”dhwsecondaryflowtemp”:29.7,”dhwsecondaryreturn”:7.9,”incomingcwstemperature”:4.8,”boostedcwstemperature”:4.8,”cwstank1temperature”:5,”cwstank2temperature”:5,”roomtemperature”:10.5,”site”:”b827eb4cd926″,”timestamp”:”2020/03/02 21:36:02″}
I’m not grasping how I then use your shortcodes to pull data from that string – say for example the “roomtemperature”:10.5 data and produce a graph over time of that variable?
Could use a little help if you could spare the time. Trying to build a dashboard on a WordPress page that will show the data above in a meaninful manner.
Many thanks,
Mark.
I’m just replying here for completeness, as we’ve had quite a long chat about this.
Issue identified so far is related to versions of mysql. MySQL 5.7 is required for the JSON_EXTRACT function.
Chris