apreb / leafspy-live

Process and visualize real-time Leaf Spy data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leaf Spy Pro (LS) is a mandatory app for every Nissan Leaf owner. Some of this APP potential is hidden away in CSV generated files that can be hard to evaluate and handle over time. Leaf Spy Live captures live data from LS and automatically processes many of the available data to facilitate its visualization. It is possible to generate the following information:

  • Live trip on map with live statistics, SOC, GIDS, kWh, Battery temperature, etc.
  • Browse trip history
  • Send charging statistics to emoncms for energy visualization
  • Keep track of latest Km, Battery temperature, AHr, location of the vehicle, etc.

Prerequisites

  • One OBD II compatible bluetooth dongle
  • Leaf Spy Pro
  • Dedicated smartphone with bluethooth, GPS and mobile data plan
  • One Raspberry Pi with emonSD image

Leaf Spy Pro Settings

Go to LS Setings, Server section and fill the info below:

  • Enable
  • Send interval = 5 sec
  • URL: 192.168.1.1:1880/ls (change to your own IP address)

It is possible to use https, however a valid certificate, issued by a trusted authority is needed.

Now make sure Leaf Spy is connected to the OBD Dongle in the car so the data is sent to the server. Leaf Spy will complain if the response from the server is not adequate.

One of the main features is live trip tracking, so reachability between Leaf Spy and the server is needed. There are many ways to achieve this and are out of the scope of this blog post:

  • Use a VPN (OpenVPN) between the remote smartphone and Rasperry Pi
  • Use a free dynamic DNS service to resolve your public ip address and create a port forward rule to your Raspberry Pi.
Server side - Raspberry Pi

Bits of software required to parse the live streams:

  • Emoncms
  • MongoDB to store trip data
  • Node-RED with the following extra nodes:
    • node-red-node-mongodb
    • node-red-contrib-mongodb2
    • node-red-node-emoncms

To install mongoDB simply access the bash of the rpi and copy-paste the following commands

rpi-rw && sudo su
apt-get update && apt-get upgrade
apt-get install mongodb-server

Before running the DB we must change the directory of the data and disable logging:

mkdir /home/pi/data/mongodb
chown -R mongodb:mongodb /home/pi/data/mongodb
vi /etc/mongodb.conf

and replace the contents with the info below:

bind_ip = 127.0.0.1
port = 27017
quiet = true
dbpath = /home/pi/data/mongodb
journal = true

Save, permit the mongoDB port in the firewall and reboot

ufw allow 27017
reboot

Reboot and check if the mongoDB is running by trying to connect

root@emonpi(rw):pi# mongo 
MongoDB shell version: 2.4.10
connecting to: test
Server has startup warnings: 
Wed Nov  1 17:38:56.658 [initandlisten] 
Wed Nov  1 17:38:56.659 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Nov  1 17:38:56.659 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Wed Nov  1 17:38:56.659 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Wed Nov  1 17:38:56.660 [initandlisten] 
> 

Copy (ctrl+c) the flows from the repository here

Open the Node-RED dash http://ip_of_rpi:1880 and paste them to a new tab:

Some nodes may need additional configuration, such as MongoDB, MQTT, Emoncms. The flows are broken down into sections which are explained in more detail below.

Leaf Spy Server

First http Node listens for Leaf Spy data at http://ip_of_rpi:1880/ls. A debug node can be attached direcly to check incoming messages. The arrival of data triggers a response back to the server to keep LeafSpy happy. The data passes through a function node to prepare the data for three main purposes:

  • Together with a timestamp the original data is posted to a persistent MQTT topic.
  • Charging statistics are gathered to be posted to Emoncms.
  • Filter trip data and save it ot the mongoDB.

If the trip data has valid GPS coordinates and the car is ON, the trip point is eligible to be stored in a array of trip data. However the data must be trimmed at the end of a trip to keep the statistics coherent.

To uniquely identify a trip, Leaf Spy sends a "Trip" variable that increments every time there is a new trip. When the car is turned on ( ON or "Ready to Drive") this trip value is not yet incremented and reamins with the previous value. The increment happens when the car starts moving so there is no need to trim the beginning of the trip. The actual trip ends when the driver stops and turns the car off. A variable on the data signals the status of the car and trip data is not updated. However, when the driver starts the next trip, or if it turns the car to the ON position, the trip data is resumed with the old trip number. In order to avoid this effect, every datapoint of the trip must be checked against the last correct trip data and must not be more than 30 seconds apart. There are caveats associated with this method:

  • When the trip ends, the driver should wait 30 seconds before turning the car ON again in order to maintain trip statistics coherent
  • If during a trip communication fails for more than 30 seconds, the trip will no longer be logged

To trim the trip data, the function stores the data in memory and queries the mongoDB for the last saved trip entry. The Epoch timestamp of boths entries is compared and stored if they differ no more than 30 seconds.

If the "LIVE" switch is enabled on the user interface, the valid trip entries trigger updates to a unique MQTT topic. The websockets section is listening on this topic and updates the map in real time.

Live Map HTML and WS:

Pointing a web browser to http://ip_of_rpi:1880/map loads a map with some javascript that opens a websocket connection to Node-RED at ws://ip_of_rpi:1880/ws/location to draw the latest trip over a map. Open the Map template and replace the ip address with the real ip address of the raspberry pi.

Every WS request triggers a mongoDB query for the last trip index. A second query of the DB deliver the particular trip's data back to the browser. It is also possible to query direclty the DB for a paricular trip using MQTT. Every request is also sent to a persistent MQTT topic. MQTT enables building the user interface that is described on the next section.

User Interface (UI)

Two pages are created, the first shows the raw data coming from the APP. The second page shows a map and statistics associated with trips. The map can go live to follow the real time trip or can be used to browse trip history. The URL of the LiveMap template need to be replaced with the real IP of the rasberry Pi.

To access the UI, browse to http://ip_of_rpi:1880/ui

User interface pictures:

Live Trip Live Info
)

Some early examples of the Leaf Spy Live in action:

Live Map #1

Live Map #2

About

Process and visualize real-time Leaf Spy data