Saturday, May 25, 2013

[Tasker] How to sync data between devices with Tasker

Sometimes it's handy to have the same data on both devices, or to inform a device about some status of an other device. I want to show you an easy way to send information from one device to another with Tasker and a small PHP file on a web server.

What is possible with this?
  • Display the position of another device directly on the home screen within a widget (I use it for this)
  • React to certain states of another device (like low battery)
  • Send information from android to computer
 What do we need?
  • Tasker
  • Webserver with PHP support
On the server side we need a PHP file named "receive.php" with the following content:
<?php
$file = 'received_data.log';
$handle = fopen($file, 'w') or die('Cannot open file: '.$file);
$data = "";

foreach ($_POST as $key => $value)
{
  $data = $data.$value."\n";
}
fwrite($handle, $data);

fclose($handle);
print("0");
?>
  

Make sure the file has the permission "rw-rw-r--". This script receives data send through a PHP_POST and writes the passed values into a file called "received_data.log". Values of different POST variables are seperated by a newline.

Now Tasker needs to send the desired variables to this php file. The important actions are:
Senddata
A1: HTTP Post [ 
    Server:Port:http://myserver.com/mysite/receive.php
    Path:
    Data / File:data1=hello data2=test data3=%taskervar1 data4=%taskervar2
                data5=%taskervar3 data6=%taskervar4 data7=%taskervar5
    Cookies:
    Timeout:20
    Content Type:application/x-www-form-urlencoded
    Output File:Tasker/data/senddata.log ]
A2: Read File [ File:Tasker/data/senddata.log To Var:%error ]
A3: Popup [ Title: Text:%error
            Background Image:
            Layout:
            Popup Timeout (Seconds):1
            Show Over Keyguard:On ]
In A1 we send the words "hello", "test" and some values stored in the Tasker variables %taskervar1 to %taskervar5 to the PHP script. The PHP script will write the passed values into a new file, each value seperated by a new line. It will return "0" if no errors occurred. So we can check the success of the operation by comparing the local Tasker variable "%error" to "0".

Of course the sending doesn't succeed always, the phone could have no connection at the moment or the server could be down. I came up with the following solution: Try to send the data five times in a row until one sending succeeds, if none got through - Tasker waits a few minutes and tries again five times to send the data. This procedure is repeated until it succeeds. I created two Tasks for this. One for preparing the data and one to send it.
I will show you an example how to send the last seven lines of a file with this method. The task "Data write" is called with a variable passed to it (stored afterwards in %par1). It appends this value to a file if the value isn't already the last value stored there and sends the last seven lines of the file to the server:
Data write
Run Both Together
A1: If [ %par1 !~ %LASTLOG ]
  A2: Variable Set [ Name:%LASTLOG To:%par1 Do Maths:Off Append:Off ]
  A3: Run Shell [ Command:tail -n7 /mnt/sdcard/Tasker/log/Blackbox.log Timeout (Seconds):0 Use Root:Off Store Result In:%log ]
  A4: Write File [ File:Tasker/log/Blackbox.log Text:%log %TIME %par1 Append:Off Add Newline:On ]
  A5: Write File [ File:Tasker/log/BlackboxFull.log Text:%TIME %par1 Append:On Add Newline:Off ]
  A6: Zoom Element Visibility [ Element:Protokoll11.w / logs Set:On ]
  A7: If [ %AIR !~ on ]
    A8: Variable Set [ Name:%num To:1 Do Maths:Off Append:Off ]
    <Post>
    A9: Perform Task [ Name:Data send Stop:Off Priority:7 Parameter 1 (%par1):%num Parameter 2 (%par2): Return Value Variable:%post_success ]
    A10: Wait [ MS:0 Seconds:0 Minutes:3 Hours:0 Days:0 ]
    A11: If [ %post_success !~ 1 ]
      A12: Wait [ MS:0 Seconds:0 Minutes:8 Hours:0 Days:0 ]
      A13: Variable Add [ Name:%num Value:1 Wrap Around:0 ]
      A14: Goto [ Type:Action Label Number:1 Label:Post ]
    A15: End If
    A16: Notify Cancel [ Title:send log Warn Not Exist:Off ]
  A17: End If
A18: End If
A19: Return [ Value:1 Stop:On ]
As you can see, it calls a second task which actually sends the information:
Data Send
Abort Existing Task
A1: Delete File [ File:Tasker/log/Blackboxout.log Shred Level:0 Use Root:Off Continue Task After Error:On ]
A2: Run Shell [ Command:tail -n7 /mnt/sdcard/Tasker/log/Blackbox.log Timeout (Seconds):10 Use Root:Off Store Result In:%log ]
A3: Variable Split [ Name:%log Splitter:%NEWLINE Delete Base:Off ]
A4: Variable Set [ Name:%log4 To:  Do Maths:Off Append:Off ] If [ %log4 ! Set ]
A5: Variable Set [ Name:%log5 To:  Do Maths:Off Append:Off ] If [ %log5 ! Set ]
A6: Variable Set [ Name:%log6 To:  Do Maths:Off Append:Off ] If [ %log6 ! Set ]
A7: Variable Set [ Name:%log7 To:  Do Maths:Off Append:Off ] If [ %log7 ! Set ]
A8: Variable Set [ Name:%post_ok To:0 Do Maths:Off Append:Off ]
A9: For [ Variable:%num Items:1:5 ]
  A10: Variable Set [ Name:%counter To:%par1*10+%num Do Maths:On Append:Off ]
  A11: Notify [ Title:sendlog Text:%LASTLOG Icon:ipack:crystalhd:easymoblog Number:%counter Permanent:On Priority:3 ]
  A12: HTTP Post [ Server:Port:http://myserver.com/mysite/receive.php
       Path:
       Data / File:log1=%log1 log2=%log2 log3=%log3
                   log4=%log4 log5=%log5 log6=%log6 log7=%log7
       Cookies:
       Timeout:20
       Content Type:application/x-www-form-urlencoded
       Output File:Tasker/log/Blackboxout.log
       Continue Task After Error:On ]
  A13: Wait [ MS:0 Seconds:10 Minutes:0 Hours:0 Days:0 ]
  A14: Read File [ File:Tasker/log/Blackboxout.log To Var:%post_ok ]
  A15: Wait [ MS:0 Seconds:1 Minutes:0 Hours:0 Days:0 ]
  A16: If [ %post_ok ~ 1 ]
    A17: Notify Cancel [ Title:send log Warn Not Exist:Off ]
    A18: Return [ Value:1 Stop:On ]
  A19: End If
  A20: Wait [ MS:0 Seconds:10 Minutes:0 Hours:0 Days:0 ]
A21: End For
A22: Return [ Value:2 Stop:On ]
The global variable %NEWLINE has a newline, a return character stored in it. Please create this one, we need it to separate each new line in the file.

If you are looking for an easier solution to send information directly from one android device to another android device, you should have a look at AutoRemote. Unfortunately this feature isn't free.

4 comments:

  1. Hi Benny.

    I found out your "blackbox log" on lifehacker and searched up on how to achieve it. It is quite an innovative way to use Tasker. I am new to Tasker and I am trying to replicate the thing. If you have a Tasker App for it that you can provide, that would be awesome.

    I do have my own PHP server.

    ReplyDelete
  2. Thanks for this insight. Could you elaborate more on the 'display the position of another device directly on the home screen within a widget' part please?

    ReplyDelete
  3. Good blog post real information ty no top 10 tech tips lol

    ReplyDelete
  4. Good blog post real information ty no top 10 tech tips lol

    ReplyDelete