Json call in Cloudpebble

We had to code used during Maker Faire few months back and i’m trying to adapt it for a Spark.function which i would love to call.

The sample is:

 ajax(
    {
      url: 'https://api.spark.io/v1/devices/' + DEVICE_ID + '/params?access_token=' + ACCESS_TOKEN,
      method: 'post',
      type: 'json',
      data: { m: 'mode=' + mode }
    },

does anyone have some suggestion on how i can change the data: { m: 'mode=' + mode }?

I’m just really confused about m:. Sorry but i’m so not web-development savvy :smiley:

More information here:

my firmware:

int command = 0;

void setup(){
    Spark.function("command", cmd_process);
}

void loop(){
}


int cmd_process(String cmd){
    String value = cmd;
    command = value.toInt();
    return command;
}

Cloudpebble ajax call:

simply.on('singleClick', function(e) {
  console.log(util2.format('single clicked $button!', e));
  var mode = 1;
  
  if ('up' == e.button) {
    mode = 2;
    simply.subtitle('Lock!');
  } else {
    simply.subtitle('Unlock!');
  }
  
  ajax(
    {
      url: 'https://api.spark.io/v1/devices/' + DEVICE_ID + '/command?access_token=' + ACCESS_TOKEN,
      method: 'post',
      type: 'json',
      data: { m: mode }
    },
    function(e){ console.log('ajax success: ' + e); },
    function(e){ console.log('ajax failure: ' + e); }
  );
})

The ajax returns success but my servo is not moving. Calling it from Spark-cli works :wink:

So ajax() is the swiss-army-knife–it does everything but you can sometimes have trouble finding the exact tool you want. Have you tried the post wrapper instead?

	var requestURL = "https://api.spark.io/v1/devices/" +DEVICE_ID + "/command/";
        $.post( requestURL, { params: mode, access_token: ACCESS_TOKEN});

It doesn’t matter what you call the parameter (“m” or “params”) since the cloud strips that off before calling the core.

If you really need ajax to work, try putting the access token in the data field and adding a final “/” to the URL.

2 Likes

I did a debug with Serial.print and realized that the core is not receiving the data.

Somehow it has to be like data: { m: '0' + mode }. This probably makes it become a string instead i guess?

Now that it’s working… I’ll have something to demonstrate tomorrow! :smiley:

Thanks @bko

2 Likes

Good catch! I like this

data: { m: mode.toString(); }
1 Like

Let me test it out! :smiley:

@bko, that works! Seems like i do have to start learning web coding and make more awesome web apps for my ongoing spark projects :wink:

1 Like

What are you making? combining 2 of my favorite things by the looks of it

1 Like

If you want, take a look at my deck lights project. It can be controlled from the Pebble or a web page on a smart phone. It builds a Pebble app instead of using Simply.JS.

3 Likes

@wgbartley Do I have to host it from somewhere with Node.js?

@Hootie81, I will post more details when I have the green light. It’s a first demonstration before confirming a larger scale spark core deployment. :smiley:

1 Like

Not any more! The original code assumed you were using something like the PHP proxy I wrote. I just updated the code so that it talks directly to the Spark Cloud. You just need to make sure the CORE_ID, ACCESS_TOKEN, and API_URL variables are filled in correctly. I added API_URL so that you can point it to your own local cloud if you want!

You should be able to drop the index.html file anywhere your phone can access. Either a web server you have access to, something like Dropbox, or even a file on your phone’s filesystem (if not using an iPhone).

1 Like

@wgbartley, so basically it’s like a native pebble app?

@Hootie81, since you asked… take a look at what a Spark core and pebble watch can do!

Not the actual application but some idea of what i am up to… :smiley:

1 Like