OBDII / OBD2 Bluetooth AdapterIn yesterday’s article, I shared with you a webserver written in Python that captures data being logged and transmitted from your vehicle’s computer using the Torque app for your cellphone. In that article, I mentioned that I would also be providing a PHP app that does the same thing. As promised, I now want to share that PHP code with you. If you haven’t read yesterday’s article, I would advise that you check it out first. In it, I introduce you to the Bluetooth Supper Mini OBD 2 / OBD II adapter which I use for reading the computer data from my truck and pass it back to my webserver for logging and future analytics. You can get the bluetooth adapter from Amazon for $20.00. That article also includes the instructions for configuring your webserver’s URL in the Torque app as well as what the Torque app is and does. Once you have finished reading that article, come back here where you can get the code for a PHP app that can be used for capturing and logging the data transmitted from the Torque app on your mobile device.

Once you have installed the Torque app and entered the URL for your webserver, append “torque.php” to the end of the URL and copy & paste the following code into a file of the same name on your webserver. Configure the database information at the top of the code to reflect your server environment and you will now be able to capture data from your vehicle’s computer and store it in a MySQL database for future (and real-time) reference.

$db_user = "root";
$db_pass = "root";
$db_name = "torque";
$db_table = "torque_data";
$link = mysql_connect('localhost', $db_user, $db_pass);
$db = mysql_select_db($db_name);

$result = mysql_query("SHOW COLUMNS FROM $db_table");
if (mysql_num_rows($result) > 0)
   while ($row = mysql_fetch_assoc($result))
      $dbfields[] = ($row['Field']);

foreach ($_GET as $key => $value)
   $keys[] = $key;
   if (preg_match("/^k/", $key))
      $values[] = $value;
      $values[] = "'" . $value . "'";

   if (!in_array($key, $dbfields))
      mysql_query("ALTER TABLE $db_table ADD $key VARCHAR(255) NOT NULL default '0';");

$sql = "INSERT INTO $db_table (" . join(",", $keys) . ") VALUES (" . join(",", $values) . ")";

print "OK!";

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Tagged with:  

Leave a Reply