Curl Help in creating command

Hi All,

I am definitely not a natural programmer and would like some help on the Curl command.

I am trying to create a project based on:

I have followed the instructions to create the .cpp & .h tabs and created an ino file as:

 * IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
 * An IR LED must be connected to Arduino PWM pin 3.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */
 
//This #include statement was automatically added by the Spark IDE.

#include "IRremote.h"



IRsend irsend(D1);

void setup()
{
  Serial.begin(9600);
}

void loop() {
  if (Serial.read() != -1) {
    for (int i = 0; i < 3; i++) {
      irsend.sendSony(0xa90, 12); // Sony TV power code
      delay(40);
    }
  }
}

Question:
However I am not clear on how to actually run it using the Curl command. I understand the basic examples:

POST /v1/devices/{DEVICE_ID}/analogread

# EXAMPLE REQUEST IN TERMINAL
# Core ID is 0123456789abcdef01234567
# Your access token is 1234123412341234123412341234123412341234
curl https://api.spark.io/v1/devices/0123456789abcdef01234567/analogread \
  -d access_token=1234123412341234123412341234123412341234 -d params=A0

But I still don’t understand how I craft the command to my send my request.
I would appreciate if someone could help?

Best wishes.

Hi @foggy

This looks like a fun project! Right now your sketch does not have any Spark functions or variables that curl or a browser could use, so lets make some! There many ways to divide up the work to be done between core and cloud, but this way is simple and based on an integer command.

* IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
 * An IR LED must be connected to Arduino PWM pin 3.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

//This #include statement was automatically added by the Spark IDE.

#include "IRremote.h"

int commandToSend = 0;  // let's say zero means idle, don't send anything

IRsend irsend(D1);

void setup()
{
  Serial.begin(9600);
  Spark.function("irsend", commandSender);
}

void loop() {
  switch(commandToSend) {
    case 1:  //power on
      sendMyCode(0xa90);
      commandToSend = 0; //return to idle
      break;
    // more cases here
  }
}

void sendMyCode(unsigned long code) {
   for (int i = 0; i < 3; i++) {
      irsend.sendSony(code, 12); // Sony TV codes
      delay(40);
    }
}

int commandSender(String command) {
  commandToSend = command.toInt();
  return 1;  //always return success
}

so now with curl we can do:

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/irsend \
  -d access_token=1234123412341234123412341234123412341234 -d params=1

I didn’t get a chance to test this, but I hope you get the idea!

2 Likes

Thank you very much @bko Your response is greatly appreciated. It works and most importantly I understand some of what you wrote. I will do some googling to ensure I understand more of the detail.
Thank you also for // more cases here, that was another issue I was stuck on.

Question:
I think I read somewhere that I can append more than one “params=x” to the curl command - is that correct? and if so do I separate them with a “,” ?
For example if I add more case statements to blast a digit EG digit 1, digit 2 etc. I could append them to the curl command in order to bring up a channel number params=x, params=y, params=z for example (123)
Am I likely to need to put a delay in somewhere so that the 3 params are not fired together too quickly and if so where should I put it?

Once again. Thank you for your support.

Hi @foggy

Sure! You can add comma separated parameters to curl like this:

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/irsend \ -d access_token=1234123412341234123412341234123412341234 -d params=1,2,3

Then in your Spark function, you have to parse that String, in this case, “1,2,3”. The Tinker app does something just like this for instance and you can look at the Tinker source here. You could use the String method command.indexOf(",") with a loop to find all the commas and use toInt() on the substrings.

Another approach might be to have multiple functions (you can have up to four). Each function could correspond to a device or to a room or to a particular scene or setup (think “Watch TV” vs “Watch a BluRay”). You get to choose how you want to set it up.

Have fun with it!