I wrote a really basic example here that showed how to plot some GPS points onto map. The example works well but you may have noticed that it relies on the data in the Mqtt message payload formatted in a particular way. Remember what it looked like?
If you’ve worked with GPS devices before you know that the GPS data that ends up at the Mqtt broker won’t look like that. That’s far too simple!
MqttCogs provides a Filter Hook to allow you change the incoming payload before it is persisted to the database. This allows you to reshape the JSON in the payload to a form that can be used by the MqttCogs shortcodes.
To use the hook you need to be able to write some php. I used My Custom Functions wordpress plugin. This allows you to quickly add some php to your site without messing around with functions.php or any other part of your site. I highly recommend this plugin.
My code looks something like this. It reads the payload, changes the format of the incoming payload data. The data is then persisted as normal.
add_filter('mqttcogs_msg_in_pre', 'mqttcogs_msg_in_pre', 10, 3);
function mqttcogs_msg_in_pre($publish_object, $utc)
{
//do nothing if we are not interested in the topic
if ($publish_object.getTopic() != 'sometopicthatcontainslnglat") {
return;
}
//reshape the JSON data
$payload = $publish_object.getMessage();
$payload = reshapethedatafunction($payload);
$publish_object.setMessage($payload);
}
To extract and plot the values from my json payload I need to instruct MqttCogs where to get them from. Have a read of visualizing mqtt json payload this provides a worked example of how to show your lng lats on a map.
Check out Visualizing OwnTracks GPS Data this uses this principle to show owntracks GPS data on a map!
No Comments
You can leave the first : )