Connected Car Device

Thanks! Just ordered 1 to play with, I will order 4 more if we can get openhab support.

NOTE: This is a pre-order version of the AutoPi 4G/LTE Edition. The pre-order discount is 10%. Estimated shipping is May 2018.

May is still far away. Even though I’m very interested, I’m going to wait and see.

I just ordered mine. Should be shipping this month. They are still offering the 10% discount.

nice device, but 160secs boot-time :S (see https://community.autopi.io/t/device-boot-up-time/309)

Got my 3 units today and get them setup, now I just need to tie them to openhab!

they published recently a new software release.

Hello,

I discovered AutoPi.io with this discussion. It seems great.
What is your experience with the dongle and software ?
Boot time is better ?

Regards, stephan

1 Like

Hi,
I came across this thread in a desperate search for an answer on how to connect Traccar to Openhab.
Unfortunately a direct connection of Traccar to openhab does not work (for unknown reasons) therefore I wrote a small python script which converts the Traccar event/position POST messages to MQTT messages:

The python script only includes basic location messages. I will add more as I go (waiting for my car tracker)

server.py
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
    ./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import paho.mqtt.client as mqtt #import MQTT client
import logging
import json
import time


class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
        self._set_response()
        self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))

    def do_POST(self):
        delay = 0.1
        broker_address="192.168.1.4"
        client = mqtt.Client("Traccar") #create new instance
        client.connect(broker_address)
        content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
        post_data = self.rfile.read(content_length) # <--- Gets the data itself
        #logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", str(self.path), str(self.headers), post_data.decode('utf-8'))
        Traccarjson = json.loads(post_data.decode('utf-8'))
        if "position" in Traccarjson:
           logging.info("Received Position for Device %s (Name: %s, Status: %s), Lat: %s, Lon: %s, Alt: %s, Acc %s ",
               str(Traccarjson["position"]["deviceId"]), str(Traccarjson["device"]["name"]), str(Traccarjson["device"]["status"]), str(Traccarjson["position"]["latitude"]),
               str(Traccarjson["position"]["longitude"]), str(Traccarjson["position"]["altitude"]), str(Traccarjson["position"]["accuracy"]))
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/Latitude", Traccarjson["position"]["latitude"])
           time.sleep(delay)
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/Longitude", Traccarjson["position"]["longitude"])
           time.sleep(delay)
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/Location", str(Traccarjson["position"]["latitude"])+"," +str(Traccarjson["position"]["longitude"]))
           time.sleep(delay)
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/Altitude", Traccarjson["position"]["altitude"])
           time.sleep(delay)
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/Accuracy", Traccarjson["position"]["accuracy"])
           time.sleep(delay)
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/LastUpdate", Traccarjson["device"]["lastUpdate"])
           time.sleep(delay)
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/Name", Traccarjson["device"]["name"])
           time.sleep(delay)
           client.publish("Traccar/Position/"+str(Traccarjson["position"]["deviceId"])+"/Status", Traccarjson["device"]["status"])
           time.sleep(delay)
           #logging.info("Sent Position via MQTT")

        elif "event" in Traccarjson:
           logging.info("Received Event for Device %s (Name: %s, Status: %s), Event: %s",
               str(Traccarjson["event"]["deviceId"]), str(Traccarjson["device"]["name"]), str(Traccarjson["device"]["status"]), str(Traccarjson["device"]["status"]))
           client.publish("Traccar/Event/"+str(Traccarjson["event"]["deviceId"])+"/Type", Traccarjson["event"]["type"])
           time.sleep(delay)
           client.publish("Traccar/Event/"+str(Traccarjson["event"]["deviceId"])+"/LastUpdate", Traccarjson["device"]["lastUpdate"])
           time.sleep(delay)
           client.publish("Traccar/Event/"+str(Traccarjson["event"]["deviceId"])+"/Name", Traccarjson["device"]["name"])
           time.sleep(delay)
           client.publish("Traccar/Event/"+str(Traccarjson["event"]["deviceId"])+"/Status", Traccarjson["device"]["status"])
           time.sleep(delay)
           #logging.info("Sent Event via MQTT")
        else:
           logging.info("Unknown POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", str(self.path), str(self.headers), post_data.decode('utf-8'))
           client.publish("Traccar/Debug",post_data.decode('utf-8'))
           #logging.info("Sent Unknown Message")

        self._set_response()
        self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))

def run(server_class=HTTPServer, handler_class=S, port=8080):
    logging.basicConfig(level=logging.INFO)
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    logging.info('Starting httpd...\n')

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    logging.info('Stopping httpd...\n')

if __name__ == '__main__':
    from sys import argv
    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()

Edit: to make this script run as a service I had to add delays between the client.publish functions …

traccar configuration (goes in traccar.xml)
<entry key='event.forward.enable'>true</entry>
<entry key='event.forward.url'>http://127.0.0.1:8099</entry>

<entry key='forward.enable'>true</entry>
<entry key='forward.url'>http://127.0.0.1:8099</entry>
<entry key='forward.json'>true</entry>

please note that I run the python script on the same server as traccar.
Everything runs on a Raspi Zero.

I am not really good at programming … any improvements to the script are very welcome :slight_smile: