[SOLVED] Using Tasker for Android to POST an API Request to Spark Core

Hi Guys,

Just getting started here, and loving the Core so far.

Objective:

Use the Tasker app (on Android) to trigger an action on the Spark Core.
I’m using the “Control LEDs over the 'Net” code from here to test things.

Attempt / Problem:

I’ve pasted the following as a “Shell script” in Tasker (with the correct credentials), however it doesn’t seem to work. I’m probably doing something majorly wrong…

POST /v1/devices/{DEVICE_ID}/led

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/led \
  -d access_token=1234123412341234123412341234123412341234 \
  -d params=l1,HIGH

Could someone please point me in the right direction?

Thanks!
R

Try taking out the first line. POST /v1/devices/{DEVICE_ID}/led That’s just an example, and not a valid command.

I purchased tasker a long time ago. I’ll install it and see if I can get it working later today. If you get it figured out before then, please let us know! Sounds like that would be very handy.

3 Likes

Thanks Hypnopompia!

I did actually try that too.

(As a newbie, its very confusing when some descriptive text isn’t commented out in the Spark documentation. I’ve noticed capitals being used at titles there, but in this case i initially assumed that “POST” was a command since its usually written in all caps).

Unfortunately, including just the 3 lines beginning at curl didn’t work either.

Stupid question: is curl a command or a program? Should/would it work if included in a shell script in Tasker?

Thanks,
R

1 Like

Hi @Hypnopompia Any assistance you could give would be really appreciated. I have been trying to get this working all afternoon.

Best wishes.

Okay, so I think I’ve taken a step in the right direction here:

Instead of using “Run Shell Script” in Tasker, I’m using “HTTP Post” - is this correct?

This is what the setup looks like (i tried data both with an & between em, and on separate lines):

Given that its httpS, i assume “trust any certificate” needs to be checked?:

Let me know what i’m doing wrong!

Thanks,
R

This is the error that i sometimes get:

curl is a command line program. I’m not sure if it’s included with android by default or not. I think your approach to having tasker itself to the POST is a good one. I’ll try it that way as well.

1 Like

Unfortunatley curl is not a part of Android and has to be installed manually, requiring root access. Unless you are willing to do so, the script will yield an error.

You’re propably right about using “HTTP Post” but I know nothing about that. Personally, I installed curl (multiple times - every new CM nightly breaks it again…) and used SL4A to execute a shell script using curl. It is not particulary pretty but it works.

OK, so apparently that exception error is actually an android 4.2 bug. I just tried setting tasker up exactly how you did, @Rehaan, and I’m getting the same error. A quick google search shows that lots of people are having the same problem:

https://www.google.com/search?q=tasker+http+post+input%2Foutput

I’ll keep playing with this and see if I can find a work around.

@rehaan, what version of android are you using?

2 Likes

@Sydenth - thanks, i might use your method as a last resort, since the eventual setup will be on a rooted phone running CM! Though would like to test on my phone first.

@Hypnopompia - I’m currently doing this on Android 4.4 on a stock Nexus 4.

Have just downloaded curl for windows and am seeing if i can at least prove that it works that way…!

1 Like

another option is to put a script on a webserver that you could do a GET call to, and it would do a post to the spark cloud. maybe a GET in tasker would work better?

1 Like

Well i finally managed to get it working via Windows curl. (No luck with Tasker yet).

I had to make 2 changes to the example. Not sure if its because of something I’m doing wrong, or because there were errors in the example. Mentioning them here in case its the latter, so that the documentation can be corrected:

  1. Curl didn’t like the SSL certificate. I had to use the -k flag, else it would give this error:

    C:>curl https://api.spark.io/v1/devices/53xxxxxxxxxxxxxxxxxxxx87/led
    curl: (60) SSL certificate problem: self signed certificate in certificate chain

    More details here: http://curl.haxx.se/docs/sslcerts.html

    curl performs SSL certificate verification by default, using a "bundle"
    of Certificate Authority (CA) public keys (CA certs). If the default
    bundle file isn’t adequate, you can specify an alternate file
    using the --cacert option.
    If this HTTPS server uses a certificate signed by a CA represented in
    the bundle, the certificate verification probably failed due to a
    problem with the certificate (it might be expired, or the name might
    not match the domain name in the URL).
    If you’d like to turn off curl’s verification of the certificate, use
    the -k (or --insecure) option.

  2. For the second issue, here’s a snippet of Spark’s example code i used :

    int ledControl(String command)

    {
    int state = 0;
    //find out the pin number and convert the ascii to integer
    int pinNumber = (command.charAt(1) - ‘0’) - 1;
    //Sanity check to see if the pin numbers are within limits
    if (pinNumber < 0 || pinNumber > 1) return -1;

    // find out the state of the led
    if(command.substring(3,7) == "HIGH") state = 1;
    else if(command.substring(3,6) == "LOW") state = 0;
    else return -1;
    
    // write to the appropriate pin
    digitalWrite(pinNumber, state);
    return 1;
    

    }

See the 2nd last line.
Wont pinNumber be just “0” or any single digit instead of the required “D0” or “A0” ? Is this a bug in the program?

I hard coded it to D7 and then the curl commands worked :smile:

[EDIT: I guess a single digit works too, but for some reason the example program has a sanity check to only allow a 0 or 1 - which i guess is relevant to its initial choice of pins]

Anyway - now back to Tasker!

Thanks,
R

I got tasker working by changing it to a GET request that hits a page on my server, which then does a curl POST request to the cloud. It’s an extra step and requires another server though.

Looks like tasker is having a bad day with POSTs. :frowning:

Nice! Though that certainly could work for be, I’m trying to avoid additional complexity - especially since this will eventually be running on my tech challenged father’s phone.

Thanks so much for all the help Hypno! I’ve just got it working with tasker. Will put the solution in the next post so its more visible.

!! SOLVED !! :smile:

I downloaded this free plug-in for Tasker : RESTask (access RESTful APIs)

Set-up was fairly straightforward :

  1. You’ll find it in Tasker actions in the “Plugin” section
  2. The app is a little different from the screenshots, so note the following

You can then save it and press the Play button on the top right. Pressing play here itself (rather than in Tasker) has the advantage of showing you the full returned response for further debugging.

Have updated the title.

Cheers & Thanks for the help.
R

2 Likes

If anyone does end up trying this, please let me know how to get the return code into Tasker. In the plugin i save it as %RTCODE, but Tasker prints that literally ‘%RTCODE’.

Thank you very much @Rehaan This was driving me nuts. Sorry I can’t answer your supplementary question.

Best wishes.

1 Like

There’s no need for a tasker plugin, just correct configuration of the HTTP POST action:
Server:Port
https://api.spark.io

Path
/v1/devices/your_device_code/led?access_token=your_access_token

Data/File
params=l1,HIGH
or whatever command you want!

Leave all other settings to default

The response is returned in %HTTPR (200 for success!) and the data is supposed to be returned in %HTTPD…but I’ve yet to see it!

5 Likes

Oh snap! That works too - thanks :slight_smile:

I am running Android 4.4.3 with the latest version of Tasker and am getting that Java I/O error. =_=

Server:Port
https://api.spark.io

Path
/v1/devices/xxxxxxxxxxxxxx/exposed_func?access_token=zzzzzzzzzzzzzzzzzzzzzzzzzzzz

Data/File
args=mode=2,flag_one=0

Will have to see later if I have any better luck with RESTask.

1 Like