Pin argument for Spark.function() is always 0 [SOLVED]

Hi all, sorry if this has been asked before but I have a problem with the following function:

int on(String args)
{
    int pin = args.toInt();

    digitalWrite(pin, HIGH);
    
    return pin;
}

The query I use has the form:

https://api.spark.io/v1/devices/" + deviceId + "/on?access_token=" + accessToken + "&args=1"

Thus I am sending ‘1’, which I hope toInt() would convert to 1, corresponding to D1 (tested by returning D1, D7 and getting 1 and 7 respectively), but I always get 0!

What am I missing?

Thanks in advance!

@ChrisLewis,

how are you calling the APIs? I’m using Spark-cli and i tested working.

int on(String args);

void setup(){
    int i;
    
     for(i=2;i<8;i++)
        pinMode(i,OUTPUT);
    Spark.function("on", on);
}

void loop(){
    

}

int on(String args)
{
    int pin = args.toInt();

    digitalWrite(pin, HIGH);

    return pin;
}

CMD output:

C:\Users\Kenneth Lim>spark function call 48ff6a065067555008342387 on "1"
1

C:\Users\Kenneth Lim>spark function call 48ff6a065067555008342387 on "7
7

C:\Users\Kenneth Lim>spark function call 48ff6a065067555008342387 on 2
2

C:\Users\Kenneth Lim>spark function call 48ff6a065067555008342387 on 4
4

C:\Users\Kenneth Lim>spark function call 48ff6a065067555008342387 on 5
5

Hi Kenneth, thanks for replying. I’m using the PebbleKit JS via XMLHTTPRequest objects. Here’s how that happens:

var REST = function (url, type, callback) {
	var xhr = new XMLHttpRequest();
	xhr.onload = function () {
			callback(this.responseText);
	};
	xhr.open(type, url);
	xhr.send();
};

And the call:

//Construct url
var url = "https://api.spark.io/v1/devices/" + deviceId + "/on?access_token=" + accessToken + "&args=1";
console.log("Sent request to: " + url);

//Send
REST(url, "POST", 
	function(text) {
		console.log("Response received: " + text);
		
		//Parse result
		parseCloudResponse(text);
	}
);

Perhaps this isn’t the best way, but it is the preferred for the Pebble JS platform.

Thanks again!

I think you must be not getting what you expect from the Pebble JS POST since the Arduino String doc says:

If no valid conversion could be performed because the string doesn’t start with a integral number, a zero is returned.

You are logging the URL to console, can you try using that URL with CURL or plain Javascript just to see if it works?

Hi bko,

After making the request using curl for Windows, I have found that the correct return_value is the same as that specified in the request. The problem must therefore be with the Pebble KS, as you proposed.

I will keep trying! Thanks again.

Chris

Substituted the XMLHTTPRequest with jQuery ajax but the problem still persists. Again, I know the Core is doing its job properly because of the Curl test. Strange…

@ChrisLewis,

I’m not at all fluent with javascript/web programming etc.

Just a suggestion, is there a way we can display the output somehow when the REST() is called?

Seems like either 0 is always returned by XMLHttpRequest() instead of the return value from calling the API.

But i’m not sure cos that’s not my area of expertise :smiley:

The output is delivered to by Ubuntu VM terminal by pebble logs. The message received using Curl looks like this (where secret_token is substituted with my real access token!):

curl https://apispark.io/v1/devices/48ff6c065067555011201587/on -d access_token=secret_token -d args=1
{
  "id": "48ff6c065067555011201587",
  "name": "morphing_hunter2",
  "last_app": null,
  "connected": true,
  "return_value": 1
}

Sending the same request with XMLHTTPRequest returns this:

[INFO    ] PebbleTinker__1/pebble-js-app.js:84 Received turn on pin: 1
[INFO    ] PebbleTinker__1/pebble-js-app.js:88 Sent request to: https://api.spark.io/v1/devices/48ff6c065067555011201587/on?access_token=secret_token&args=1
[INFO    ] PebbleTinker__1/pebble-js-app.js:93 Response received: {
  "id": "48ff6c065067555011201587",
  "name": "morphing_hunter2",
  "last_app": null,
  "connected": true,
  "return_value": 0
}

Which appears to be returning the incorrect value. Finally by jQuery:

[INFO    ] PebbleTinker__1/pebble-js-app.js:84 Received turn on pin: 1
[INFO    ] PebbleTinker__1/pebble-js-app.js:88 Sent request to: https://api.spark.io/v1/devices/48ff6c065067555011201587/on?access_token=secret_token
[INFO    ] PebbleTinker__1/pebble-js-app.js:76 Response received: {"id":"48ff6c065067555011201587","name":"morphing_hunter2","last_app":null,"connected":true,"return_value":0}

So it does appear to be an inconsistency with PebbleKit JS…

It’s weird cos the output results are sent by the API server which is the exact format of:

 "id": "48ff6c065067555011201587",
  "name": "morphing_hunter2",
  "last_app": null,
  "connected": true,
  "return_value": 0
}

It seems like the API server gave the 0 though…

Can we try to add a non-matching case in your Spark firmware to return maybe -1 if the String arg given is wrong?

I’m thinking that the Spark firmware didn’t received 1-7 via the Pebblejs…

@ChrisLewis, maybe this is the problem:

I don’t remember being able to simply add the args using the & method for functions :slight_smile:

I’m still open minded. Could you clarify a little what you mean by a non-matching case? Thanks

Hi @ChrisLewis

Have you tried &args="1"?

Maybe its a string thing.

@bko Just tried this, made no discernible difference.

@kennethlimcp In that case, would this be a suitable alternative?

url = "https://api.spark.io/v1/devices/" + deviceId + "/on?access_token=" + accessToken;

$.ajax({
  type: "POST",
  url: url,
  data: 1,
  success: success,
  dataType: "json"
});

@ChrisLewis,

I’m not the best person to ask ;D @bko what’s your say?

Doing so does look like how it’s being called in the simple python script i use!

arguments = {'arg':'D7,HIGH'}

response = requests.post(spark_api + '/v1/devices/' + device_id + '/' + function , params=payload, data=arguments)

Give it a shot :wink:

@kennethlimcp I recognise the ‘data’ being specified as a JSON dictionary, but am not familiar with Python enough to recognise the requests.post construct or module. Thanks though!

Basically it’s the same but i wrote is as [quote=“kennethlimcp, post:13, topic:3794”]
data=arguments
[/quote] :wink:

I am not an Ajax guy but I think Kenneth is right that data needs to be key value pairs.

Thanks both, it now works! The final jQuery code is thus (for pin D1 for example):

$.ajax({
  type: "POST",
  url: url,
  data: {"args":1},
  success: success,
  dataType: "json"
});

And the same value is returned by the Core in return_value!

On with the show!

2 Likes

WOOOHOO!!! pop some bottle and dance hahahaha!

What a great start to my Monday morning :wink:

Indeed! Thanks for your help! I will update my repo and blog post on the app.

1 Like

I think this is going to get many Pebble watch owner excited :smiley:

Was just checking out the watch earlier and wished i had one lying around my house :smiley:

So do hang around the forum often or even cook up a Tutorial for people to connect their awesome watch with the :spark: :spark: :spark: :spark: :spark: CORE!