GPS Logging

Beastie is built for the road. And of course, having a highly functional and reliable GPS or Global Positioning System is quite crucial to make sure we’re never lost. We also thought it’d be cool to automatically log all the places we’ve been to, and publish our live location to our blog so that friends and family can track where we are. So, we ended up installing two GPS systems:

Kenwood DMX957XR

We use the Kenwood as our dashboard navigation system. Its GPS antenna is mounted on the dashboard facing outwards. It comes with Android Auto and Apple Carplay. It’s pretty cool that Google Maps is using the car GPS and not the cell phone GPS. This saves on battery life and results in a more precise location.

Peplink Max Transit LTEA

This is our main router and cellular modem. It’s connected to a MobileMark 7in1 antenna, mounted on the roof. Besides the GPS antenna, it has 2 antennas for WiFi (2.4 and 5 Ghz) and 4 cellular antennas. We use the Peplink API to extract GPS data and store it in Home Assistant to update our Home Zone location. We then use the Home Assistant API to pull the GPS data so we can publish live location updates online.

Implementing the connection between Peplink, Home Assistant, and finally, our WordPress-hosted site took a bit of work. Here are the 4 steps we went through:

1. Peplink

First we need to make sure we can access the Peplink API. We need to create a ClientID and ClientSecret using the router’s admin login. Replace XXXXX with your admin password and generate a cookie using this command:

curl -k -c cookies.txt -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"XXXXXXXXXXXXXXXXXXXXXX"}' https://192.168.50.1/api/login

Then create ClientID and ClientSecret as follows:

curl -k -b cookies.txt -H "Content-Type: application/json" -X POST -d '{"action":"add","name":"ha","scope":"api"}' https://192.168.50.1/api/auth.client

Now we’re ready to start customizing Home Assistant to pull the data from Peplink.

2. Home Assistant

First we need to create an auth token and refresh it on HA reboot or every two days. This is what makes all other REST calls work. Add below to your Home Assistant configuration.yaml

rest:
  - resource: "https://192.168.50.1/api/auth.token.grant"
    scan_interval: 172700
    verify_ssl: false
    method: POST
    headers:
      Content-Type: application/json
    payload: '{"clientId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","clientSecret":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","scope":"api"}'
    sensor:
      - name: pep_token
        value_template: "{{ value_json.response.accessToken }}"

Then, we create a sensor that will contain all the GPS data:

rest:
  - resource_template: "https://192.168.50.1/api/info.location?accessToken={{ states('sensor.pep_token') }}&connId=2"
    scan_interval: 10
    verify_ssl: false
    sensor:
      - name: pep_gps
        value_template: "{{ value_json.response.gps }}"
        json_attributes_path: "$.response.location"
        json_attributes:
          - "latitude"
          - "longitude"
          - "altitude"
          - "heading"
          - "timestamp"
      - name: pep_speed
        value_template: "{{ ( value_json.response.location.speed ) | round(0) }}"

And finally we’re creating an automation that will refresh the location every time Beastie moves and update the Home Assistant Home Zone location:

automation:
  - alias: update_pep_gps
    trigger:
    - platform: state
      entity_id: sensor.pep_gps
    action:
    - service: device_tracker.see
      data:
        dev_id: airstream
        gps:
          - "{{ state_attr('sensor.pep_gps', 'latitude') }}"
          - "{{ state_attr('sensor.pep_gps', 'longitude') }}"
    - service: homeassistant.set_location
      data_template:
        latitude: "{{ state_attr('sensor.pep_gps', 'latitude') }}"
        longitude: "{{ state_attr('sensor.pep_gps', 'longitude') }}"

(Thanks to Rob Wolff for providing above code)

3. Google Maps API

It’s no fun to just show lat/long coordinates. After all, what does (30,-50) mean? So, the way to do this is to visualize these coordinates on a map. We decided to use Google Maps for this functionality. It’s the best map around, it’s free to use (at least at our expected volumes), and there are tons of resources available to help with the implementation. So, here are the steps to go through:

  1. Create a credential. Go to console.cloud.google.com -> API Services -> Credentials and create a new API key.
  2. On our WordPress (or other) site/blog, add following HTML code:
<div id="googleMap" style="width:100%;height:600px;"></div>

<script>
function myMap() {
const beastie = { lat: 30, lng: -50 };
var mapProp= {
  center: beastie,
  zoom:7,
};
var m1 = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var marker = new google.maps.Marker({
  position: beastie, 
  map: m1
});
}
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=ADD_YOUR_API_KEY&amp;callback=myMap"></script>

This will create a map on your web page with a marker at fixed coordinates (30, -50). Of course, this is not what you want. You want the live location to be used. So, in order to do so, you need to hook up WordPress to your Home Assistant instance to retrieve the GPS data. Here’s how you go about that:

4. Link WordPress to Home Assistant

  1. Configure Home Assistant to allow API access. Simply add
    api:
    to your configuration.yaml.
  2. Create a Home Assistant long-lived access token.
  3. Install WPgetAPI plug-in and configure it to point to your Home Assistant instance. Make sure to add the two mandatory header parameters, specify encode body as ‘JSON encode’ and Results Format as ‘PHP array data’.
  4. Install codesnippets.pro plug-in and create two html snippets, one for latitude and one for longitude. Here’s what one looks like:
  1. And finally, update your HTML and replace the static lat/long values with the snippet shortcodes created above:
const beastie = { lat: [code_snippet id=6 php=true], lng: [code_snippet id=7 php=true] };

(With lots of inspiration and help from the very active Home Assistant Community)

Et voila, it’s done. If all goes well, your GPS data will flow from your router to Home Assistant, and onwards to your blog/website. Your RV will never be lost again.

Leave a Reply

Your email address will not be published. Required fields are marked *