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?
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.
(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?
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.
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:
@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…!
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?
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:
Curl didn’t like the SSL certificate. I had to use the -k flag, else it would give this error:
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.
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
[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]
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.
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.
In the Parameters tab, add the new parameters. (eg. Name: access_token Value:423423423424242342424234)
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.
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’.