Over the Christmas break, I have been working on several new apps for Google Glass. One of those apps includes viewing real-time OBDII data from my truck using an OBDII bluetooth adapter and my Google Glass. To do that, I am using the Bluetooth Supper Mini OBD 2 / Mini OBD II adapter which I got from Amazon for $20.00. It’s basically a small device that plugs into your vehicle’s computer (usually under the driver-side dash) and transmits data to another device such as a computer, cellphone, or tablet. When I received this ODBII adapter, I learned that there is a cool program available for Android (possibly iOS as well, but I’m not 100% sure) that allows you to view the data from the adapter & your vehicle’s computer in real-time on your cellphone. One of the coolest things about that app (aside that it’s free) is that it allows you to upload your vehicle’s data to a webserver in real-time as well. Just make sure you have a service plan that includes data. The app is called “Torque“. The example URL for the webserver that comes pre-populated in the Settings page of Torque points to a webserver operated by the app’s creator. However, it allows you to change that URL to point to a webserver of your own. Not knowing exactly what the data would look like coming from the Torque app, I decided to write a small Python app that would act as a webserver and would collect the data from the Torque app & store it in a database for future reference. Since I’m sure there are probably plenty of others out there that would like something like this, I’ve decided to share that Python app with you.
Before jumping into the Python app, I want to first mention some of the things that the Torque app is capable of. Using the Torque app and the OBD2 Adapter mentioned above, you can do things like viewing & resetting engine fault codes, view engine performance, view your current speed, current & average MPG, coolant temperature, and a lot more. Since the Torque app runs on your cellphone, if your cellphone includes GPS, Torque is also capable of recording your physical location which can be exported to things like Google Maps for tracking & viewing your driving locations. Aside from uploading your vehicle’s data to a webserver, the Torque app is also capable of logging your data to your cellphone which you can email to yourself (or your mechanic) at any time.
As mentioned earlier, the Torque app is capable of transmitting your vehicle’s data to a webserver. In another article, I will show you how to capture the data using PHP or Java. For now, I want to show you a simple standalone app I wrote in Python that acts as a webserver which logs incoming data from the Torque app. To use it, you will first need a MySQL database. If you do not want to log the data, you can remove the database calls in the code. If you do not want to use MySQL, it’s simple enough to replace the MySQL parts with other databases such as SQLite or even replace those calls with methods that write the data to log files. If you do decide to use MySQL (or another database), you will need to create a single table which will store all of the incoming data. For this example, I have named my database “torque” and its only table “torque_data”. If you are using MySQL, you will be expected to create your table with at least 1 column. I decided to create the table with a single column called “id” which auto increments and is the primary key. Here’s the SQL for that.
create table torque_data (id int primary key auto_increment);
Once you have your database and single table setup, the Python app will take care of the rest. As data is sent from Torque to your Python webserver, each new field in the data will be created in the database and the data values stored along with it. For example, if Torque sends a new field called “mpg” to your Python webserver and your Torque table does not include a column for “mpg”, the Python app will create a new “mpg” column for you. The data that comes along with the “mpg” field from Torque will then be stored in the newly created “mpg” column. Since I don’t know the data types of all the incoming fields from Torque, I have decided to simply create all new columns as VARCHAR with size 255 and “0” as the default values. I can then use other apps such as charting apps to view this data. After I get my Google Glass app completed, I will revisit this Python app where I will include charting capabilities as well as Google Maps integration so that you can view all of your data from the same Python webserver that is collecting the data.
Before using this code, make sure you set the MySQL fields at the beginning of the code to match your environment and set the port number at the bottom of the code to you want the app to listen for new requests on. You will also need to make sure that the listening port is accessible from the outside world since Torque will be sending you data over the open web. For example, if I had this code running on the same physical server as this website, I would need to open port 8888 so that Torque could access the Python webserver.
Once you have your server running and available to the outside world, go into your Torque app, go to More > Settings and scroll down to “Web upload preferences” section. Make sure “Upload to webserver” is checked. Then click on “Webserver URL” and put the URL for your Python webserver in there. As mentioned before, if I had the Python webserver running on this same physical server, my URL would be “http://www.prodigyproductionsllc.com:8888/”. Since the Python code isn’t looking for any specific file or action, there’s no need to put anything additional at the end of the URL. In my PHP version of this app, we will include the filename which will be “http://[YOUR URL]/torque.php”. But, for now, just enter the URL with the server name and port. The only thing that the Torque app expects in return is a response of “OK!” with a 200 response code.
That’s it. You’re now ready for the Python code. So, here it is. Enjoy!
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler from urlparse import urlparse import MySQLdb mysql_server = '127.0.0.1' mysql_port = 3306 mysql_user = 'root' mysql_pass = 'root' mysql_db = 'torque' mysql_table = 'torque_data' class Database(): def __init__(self): self.db = MySQLdb.connect ( host = mysql_server, port = mysql_port, user = mysql_user, passwd = mysql_pass, db = mysql_db) def close(self): self.db.close() def cursor(self): return self.db.cursor() def execute(self, sql): cursor = self.cursor() cursor.execute(sql) self.db.commit() return cursor class BaseHandler(BaseHTTPRequestHandler): def do_GET(self): db_fields =  db = Database() cursor = db.execute('SHOW COLUMNS FROM %s' % mysql_table) while (1): row = cursor.fetchone() if row == None: break col_name = row db_fields.append(col_name) cursor.close() query = urlparse(self.path).query if '?' in self.path: keys =  values =  fields = dict(qc.split('=') for qc in query.split('&')) for key in fields.keys(): val = fields[key] keys.append(key) values.append('"' + val + '"') print key, '=', val if key not in db_fields: db.execute('ALTER TABLE %s ADD %s VARCHAR(255) NOT NULL DEFAULT "0";' % (mysql_table, key)) sql = 'INSERT INTO ' + mysql_table + ' (' + ','.join(keys) + ') VALUES (' + ','.join(values) + ')' print sql db.execute(sql) db.close() self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write('OK!') if __name__ == '__main__': serveraddr = ('', 8888) srvr = HTTPServer(serveraddr, BaseHandler) srvr.serve_forever()
PayPal will open in a new tab.