Advertisement
elythomaslumber

btraced for DOMOTICZ home automation

Aug 10th, 2013
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.97 KB | None | 0 0
  1. 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.
  2.  
  3. 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.
  4.  
  5. 1. place the PHP script (name it "upload.php" below into /root/var/www on your RasPi
  6. There should be also a file named index.html. If you find this here it seems that there is APACHE2 installed.
  7. Owner of the uload.php script should be "pi". Don't forget to make it executable!
  8. 2. Change within the script the user and password (xxxxx and xxxx) to values you like to use
  9. 3. Insert your home location latitude and longditude values (xx.xxxxxx); use your values from DOMOTICZ set-up :-)
  10. 4. Change the distance ranges for home, away, arriving, holyday to your own values
  11. 5. Change the CURL commands to your own (idx and switchcmd to On or Off)
  12. 6. save the script again
  13. 7. Donwload btraced from itunes (1,79€)
  14. 8. configure btraced:
  15. Server: CUSTOM Customized http://yourdyndnsadress:port/upload.php
  16. Basic Auth disabled
  17. Username your username in the php script
  18. Password your password in the php script
  19. Real Time Upload I
  20. Upload All when Stops: I
  21. GPS setting:
  22. Run in Background I
  23. Add point... 0
  24. Unit System metric
  25. Include point... <300m
  26. Distance update... 300m and active
  27. Time filter... disabled
  28. Tracking: I
  29.  
  30. 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
  31.  
  32. 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...
  33.  
  34. <?php
  35.  
  36. // Get the received data from the iPhone (XML data)
  37. $body = @file_get_contents('php://input');
  38.  
  39. // Try to load the XML
  40. $xml = simplexml_load_string($body);
  41.  
  42. // If there was an error report it...
  43. if ($xml == false) {
  44.    // Error loading XML..., send it back to the iPhone
  45.    echo '{ "id":902, "error":true, "message":"Cant load XML", "valid":true }';
  46. }
  47. else {
  48.    
  49.    // Get username and password
  50.    $username = $xml->username;
  51.    $password = $xml->password;
  52.    
  53.    // Optional: You can check the username and password against your database
  54.    // Uncomment for hardcoded testing
  55.    if (($username != 'xxxxx') && ($password != 'xxxx')) {
  56.       echo '{ "id":1, "error":true, "valid":true }';
  57.      exit();
  58.       }
  59.    
  60.    // Get device identification
  61.    $deviceId = $xml->devId;
  62.    
  63.    // Prepare list of points
  64.    $goodPointsList = "";
  65.      
  66.    // Start processing each travel
  67.    foreach ($xml->travel as $travel) {
  68.      
  69.       // Get travel common information
  70.       $travelId = $travel->id;
  71.       $travelName = $travel->description;
  72.       $travelLength = $travel->length;
  73.       $travelTime = $travel->time;
  74.       $travelTPoints = $travel->tpoints;
  75.      
  76.       // Prepare the succesful points
  77.       $goodPointsList = '';
  78.      
  79.       // Process each point
  80.       foreach ($travel->point as $point) {
  81.          
  82.          // Get all the information for this point
  83.          $pointId = $point->id;
  84.          $pointDate = gmdate("Y-m-d H:i:s", trim($point->date));
  85.          $pointLat = $point->lat;
  86.          $pointLon = $point->lon;
  87.          $pointSpeed = $point->speed;
  88.          $pointCourse = $point->course;
  89.          $pointHAccu = $point->haccu;
  90.          $pointBatt = $point->bat;
  91.          $pointVAccu = $point->vaccu;
  92.          $pointAltitude = $point->altitude;
  93.          $pointContinous = $point->continous;
  94.          $pointTDist = $point->tdist;
  95.          $pointRDist = $point->rdist;
  96.          $pointTTime = $point->ttime;
  97.            
  98.          $goodPointsList .= $pointId.",";        
  99.       }  
  100.    }
  101.    
  102.    // Check if there was points
  103.    if ($goodPointsList != "") {
  104.       // Remove last comma
  105.       $goodPointsList = substr($goodPointsList, 0, -1);
  106.  
  107.       // Send back the answer for the saved points
  108.       echo '{"id":0, "tripid":'.$travelId.',"points":['.$goodPointsList.'],"valid":true}';
  109.    } else {
  110.       // Just OK, the code should never reach here as we always have points
  111.       echo '{"id":0, "tripid":'.$travelId.',"valid":true}';    
  112.    }
  113.  
  114. }
  115.  
  116. //----------------------------------------------------------------------------------------------------------------
  117. // distance calculation haversine formula
  118. // use your local coordinates here
  119.  
  120. $lat_home = xx.xxxxxx;
  121. $lon_home = x.xxxxxx;
  122.  
  123. // remove the "+" in the string if there is a "-" youve to correct the code
  124. $lon_btraced = str_replace("+", "", $pointLon);
  125. $lat_btraced = str_replace("+", "", $pointLat);
  126.  
  127. $theta = $lon_home - $lon_btraced;
  128. $dist = sin(deg2rad($lat_home)) * sin(deg2rad($lat_btraced)) +  cos(deg2rad($lat_home)) * cos(deg2rad($lat_btraced)) * cos(deg2rad($theta));
  129. $dist = acos($dist);
  130. $dist = rad2deg($dist);
  131. $miles = $dist * 60 * 1.1515;
  132. $distkm = $miles * 1.609344;
  133.  
  134. $distance = round($distkm, 2);
  135.  
  136. //-----------------------------------------------------------------------------------------------------------------------
  137. // get previous status from status file
  138.  
  139. $file_name = "/home/pi/domoticz/scripts/btraced_status.txt";
  140. $bt_status = fopen($file_name, "r+");
  141. $bt_status_old = fread($bt_status, filesize($file_name));
  142. fclose($bt_status);
  143.  
  144. //-----------------------------------------------------------------------------------------------------------------------
  145. // calculate distance between home and present loaction
  146.  
  147. if ($distance <= 0.3) {$bt_status_new = 0;} //home
  148. elseif ($distance > 0.3 && $distance <= 0.6 && $bt_status_old > 1) {$bt_status_new = 1;} //arriving
  149. elseif ($distance > 0.6 && $distance <= 100) {$bt_status_new = 2;} //away
  150. else {$bt_status_new = 3;} //holiday
  151.  
  152. {
  153. //------------------------------------------------------------------------------------------------------------------------
  154. // functions to be placed here
  155.  
  156. if ($bt_status_new == 0 && $bt_status_old != 0) {
  157.  
  158. //do home functions
  159.  
  160. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=Off&level=0");
  161. curl_exec($ch);
  162. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx22&switchcmd=Off&level=0");
  163. curl_exec($ch);
  164. curl_close($ch);
  165.  
  166. // write new status to btraced_status.txt
  167. //$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
  168. //fwrite ($bt_status, $bt_status_new);
  169. //fclose ($bt_status);
  170.  
  171. }
  172.  
  173. //-----------------------------------------------------------------------------------------------------------------------
  174. if ($bt_status_new == 1 && $bt_status_old > 1) {
  175.  
  176. // do arriving functions here
  177.  
  178. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=Off&level=0");
  179. curl_exec($ch);
  180. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=22&switchcmd=Off&level=0");
  181. curl_exec($ch);
  182. curl_close($ch);
  183.  
  184. // write new status to btraced_status.txt
  185. //$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
  186. //fwrite ($bt_status, $bt_status_new);
  187. //fclose ($bt_status);
  188.  
  189. }
  190.  
  191. //-----------------------------------------------------------------------------------------------------------------------
  192. if ($bt_status_new == 2 && $bt_status_old < 2) {
  193.  
  194. // do away functions here
  195.  
  196. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=On&level=0");
  197. curl_exec($ch);
  198. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=22&switchcmd=On&level=0");
  199. curl_exec($ch);
  200. curl_close($ch);
  201.  
  202. // write new status to btraced_status.txt
  203. //$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
  204. //fwrite ($bt_status, $bt_status_new);
  205. //fclose ($bt_status);
  206.  
  207. }
  208.  
  209. //-----------------------------------------------------------------------------------------------------------------------
  210. if ($bt_status_new == 3 && $bt_status_old != 3) {
  211.  
  212. //do holiday functions
  213.  
  214. // ***INSERT HOLIDAY STATE FUNCTIONS HERE***
  215.  
  216. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=12&switchcmd=On&level=0");
  217. curl_exec($ch);
  218. $ch = curl_init("http://192.168.1.18:8080/json.htm?type=command&param=switchlight&idx=22&switchcmd=On&level=0");
  219. curl_exec($ch);
  220. curl_close($ch);
  221.  
  222.  
  223. // write new status to btraced_status.txt
  224. //$bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
  225. //fwrite ($bt_status, $bt_status_new);
  226. //fclose ($bt_status);
  227.  
  228. }
  229.  
  230. // -----------------------------------------------------------------------------------------------------------------------
  231. // write new status to btraced_status.txt
  232. $bt_status = fopen("/home/pi/domoticz/scripts/btraced_status.txt", "w+");
  233. fwrite ($bt_status, $bt_status_new);
  234. fclose ($bt_status);
  235.  
  236. }
  237.  
  238. //---------------------------------------------------------------------------------------------------------------
  239. // log and show values for debugging
  240. // in putty enter /home/pi/domoticz/scripts
  241. // type: tail -f upload.txt
  242. /*
  243. $ze = fopen("/home/pi/domoticz/scripts/upload.txt", "w+");
  244. fwrite ($ze, "Device_id_iPhone  $deviceId \r\n");
  245. fwrite ($ze, "Distance_to_home  $distance km\r\n");
  246. fwrite ($ze, "Longditude        $lon_btraced \r\n");
  247. fwrite ($ze, "Latitude          $lat_btraced \r\n");
  248. fwrite ($ze, "Old_Status        $bt_status_old \r\n");
  249. fwrite ($ze, "New_Status        $bt_status_new \r\n");
  250. fwrite ($ze, "Date              $pointDate \r\n");
  251. fwrite ($ze, "\r\n");
  252. fclose ($ze);
  253. */      
  254. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement