Btraced with PHP, APACHE on a Raspberry Pi

Forum / topics about Btraced protocol and webservice (uploading gps data) Also Btraced sample code.

Btraced with PHP, APACHE on a Raspberry Pi

Postby Btraced » Sat Aug 24, 2013 3:52 pm

Btraced with PHP, APACHE2 on a Raspberry Pi

Check here for the full specs:
http://pastebin.com/TYwHUkpV

This php script works with PHP5 and APACHE2 on a Raspberry Pi. You need the APP "btraced" on your iPhone. An APP for Android should also be available meanwhile(?). Also you need remote access to your RasPi via DynDns or something and port forwarding your raspiipadress port xxx to 80 in your router.

This script will be triggered by an upload of btraced GEO data. Dependetly on the iPhone's distance to your home it will trigger switching on a home automation system like DOMOTICZ.

1. place the PHP script (name it "upload.php" below into /root/var/www on your RasPi
There should be also a file named index.html. If you find this here it seems that there is APACHE2 installed.
Owner of the uload.php script should be "pi". Don't forget to make it executable!
2. Change within the script the user and password (xxxxx and xxxx) to values you like to use
3. Insert your home location latitude and longditude values (xx.xxxxxx); use your values from DOMOTICZ set-up :-)
4. Change the distance ranges for home, away, arriving, holyday to your own values
5. Change the CURL commands to your own (idx and switchcmd to On or Off)
6. save the script again
7. Donwload btraced from itunes (1,79€)
8. configure btraced:
Server: CUSTOM Customized http://yourdyndnsadress:port/upload.php
Basic Auth disabled
Username your username in the php script
Password your password in the php script
Real Time Upload I
Upload All when Stops: I
GPS setting:
Run in Background I
Add point... 0
Unit System metric
Include point... <300m
Distance update... 300m and active
Time filter... disabled
Tracking: I

Check, if a file "btraced_status.txt" is generated after first upload of data to the RasPi. otherwise generate this file manually. Owner is pi and place is /home/pi/domoticz/scripts

Warning: when Tracking is active battery consumption is high; therefore don't change the values to any under 300m; this is the best value since there's a different method for location active; I've also set off bluethooth but WLAN active and 3G; it's recommended to charge the iPhone as often as possibly; i use it in my car always powewred by the car battery and at home I switch of btraced tracking; at work I've also it always connected to a charger... SO check for your own situation if you like it...

Code: Select all
<?php

// Get the received data from the iPhone (XML data)
$body = @file_get_contents('php://input');

// Try to load the XML
$xml = simplexml_load_string($body);

// If there was an error report it...
if ($xml == false) {
   // Error loading XML..., send it back to the iPhone
   echo '{ "id":902, "error":true, "message":"Cant load XML", "valid":true }';
}
else {
   
   // Get username and password
   $username = $xml->username;
   $password = $xml->password;
   
   // Optional: You can check the username and password against your database
   // Uncomment for hardcoded testing
   if (($username != 'xxxxx') && ($password != 'xxxx')) {
      echo '{ "id":1, "error":true, "valid":true }';
     exit();
      }
   
   // Get device identification
   $deviceId = $xml->devId;
   
   // Prepare list of points
   $goodPointsList = "";
     
   // Start processing each travel
   foreach ($xml->travel as $travel) {
     
      // Get travel common information
      $travelId = $travel->id;
      $travelName = $travel->description;
      $travelLength = $travel->length;
      $travelTime = $travel->time;
      $travelTPoints = $travel->tpoints;
     
      // Prepare the succesful points
      $goodPointsList = '';
     
      // Process each point
      foreach ($travel->point as $point) {
         
         // Get all the information for this point
         $pointId = $point->id;
         $pointDate = gmdate("Y-m-d H:i:s", trim($point->date));
         $pointLat = $point->lat;
         $pointLon = $point->lon;
         $pointSpeed = $point->speed;
         $pointCourse = $point->course;
         $pointHAccu = $point->haccu;
         $pointBatt = $point->bat;
         $pointVAccu = $point->vaccu;
         $pointAltitude = $point->altitude;
         $pointContinous = $point->continous;
         $pointTDist = $point->tdist;
         $pointRDist = $point->rdist;
         $pointTTime = $point->ttime;
           
         $goodPointsList .= $pointId.",";         
      }   
   }
   
   // Check if there was points
   if ($goodPointsList != "") {
      // Remove last comma
      $goodPointsList = substr($goodPointsList, 0, -1);

      // Send back the answer for the saved points
      echo '{"id":0, "tripid":'.$travelId.',"points":['.$goodPointsList.'],"valid":true}';
   } else {
      // Just OK, the code should never reach here as we always have points
      echo '{"id":0, "tripid":'.$travelId.',"valid":true}';     
   }

}

//----------------------------------------------------------------------------------------------------------------
// distance calculation haversine formula
// use your local coordinates here

$lat_home = xx.xxxxxx;
$lon_home = x.xxxxxx;

// remove the "+" in the string if there is a "-" youve to correct the code
$lon_btraced = str_replace("+", "", $pointLon);
$lat_btraced = str_replace("+", "", $pointLat);

$theta = $lon_home - $lon_btraced;
$dist = sin(deg2rad($lat_home)) * sin(deg2rad($lat_btraced)) +  cos(deg2rad($lat_home)) * cos(deg2rad($lat_btraced)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$distkm = $miles * 1.609344;

$distance = round($distkm, 2);

//-----------------------------------------------------------------------------------------------------------------------
// get previous status from status file

$file_name = "/home/pi/domoticz/scripts/btraced_status.txt";
$bt_status = fopen($file_name, "r+");
$bt_status_old = fread($bt_status, filesize($file_name));
fclose($bt_status);

//-----------------------------------------------------------------------------------------------------------------------
// calculate distance between home and present loaction

if ($distance <= 0.3) {$bt_status_new = 0;} //home
elseif ($distance > 0.3 && $distance <= 0.6 && $bt_status_old > 1) {$bt_status_new = 1;} //arriving
elseif ($distance > 0.6 && $distance <= 100) {$bt_status_new = 2;} //away
else {$bt_status_new = 3;} //holiday

{
//------------------------------------------------------------------------------------------------------------------------
// functions to be placed here

if ($bt_status_new == 0 && $bt_status_old != 0) {

//do home functions

$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=Off&level=0");
curl_exec($ch);
$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx22&switchcmd=Off&level=0");
curl_exec($ch);
curl_close($ch);

// write new status to btraced_status.txt
//$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
//fwrite ($bt_status, $bt_status_new);
//fclose ($bt_status);

}

//-----------------------------------------------------------------------------------------------------------------------
if ($bt_status_new == 1 && $bt_status_old > 1) {

// do arriving functions here

$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=Off&level=0");
curl_exec($ch);
$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=22&switchcmd=Off&level=0");
curl_exec($ch);
curl_close($ch);

// write new status to btraced_status.txt
//$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
//fwrite ($bt_status, $bt_status_new);
//fclose ($bt_status);

}

//-----------------------------------------------------------------------------------------------------------------------
if ($bt_status_new == 2 && $bt_status_old < 2) {

// do away functions here

$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=On&level=0");
curl_exec($ch);
$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=22&switchcmd=On&level=0");
curl_exec($ch);
curl_close($ch);

// write new status to btraced_status.txt
//$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
//fwrite ($bt_status, $bt_status_new);
//fclose ($bt_status);

}

//-----------------------------------------------------------------------------------------------------------------------
if ($bt_status_new == 3 && $bt_status_old != 3) {

//do holiday functions

// ***INSERT HOLIDAY STATE FUNCTIONS HERE***

$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=On&level=0");
curl_exec($ch);
$ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=22&switchcmd=On&level=0");
curl_exec($ch);
curl_close($ch);


// write new status to btraced_status.txt
//$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
//fwrite ($bt_status, $bt_status_new);
//fclose ($bt_status);

}

// -----------------------------------------------------------------------------------------------------------------------
// write new status to btraced_status.txt
$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
fwrite ($bt_status, $bt_status_new);
fclose ($bt_status);

}

//---------------------------------------------------------------------------------------------------------------
// log and show values for debugging
// in putty enter /home/pi/domoticz/scripts
// type: tail -f upload.txt
/*
$ze = fopen("/home/pi/domoticz/scripts/upload.txt", "w+");
fwrite ($ze, "Device_id_iPhone  $deviceId \r\n");
fwrite ($ze, "Distance_to_home  $distance km\r\n");
fwrite ($ze, "Longditude        $lon_btraced \r\n");
fwrite ($ze, "Latitude          $lat_btraced \r\n");
fwrite ($ze, "Old_Status        $bt_status_old \r\n");
fwrite ($ze, "New_Status        $bt_status_new \r\n");
fwrite ($ze, "Date              $pointDate \r\n");
fwrite ($ze, "\r\n");
fclose ($ze);
*/       
?>
User avatar
Btraced
Site Admin
 
Posts: 60
Joined: Sat Dec 10, 2011 3:03 pm

Return to Btraced Webservice Protocol And Sample code etc.

Who is online

Users browsing this forum: No registered users and 1 guest

cron