Brand New to CURL -- How to Get Started?

A question from a total beginner: I’ve never used “curl” before, and so am totally stymied by these type of examples:

EXAMPLE REQUEST IN TERMINAL
Core ID is 0123456789abcdef01234567
Your access token is 1234123412341234123412341234123412341234
curl https://api.spark.io/v1/devices/0123456789abcdef01234567/digitalwrite
-d access_token=1234123412341234123412341234123412341234 -d params=D0,HIGH

Break this down for me like I’m in kindergarten.

  1. If I’m on a windows machine, am I right that my “terminal” is the “cmd.exe” window? Is that the only option? (AKA, can I do this from a browser?)

  2. Do I need to download something? When I try to parrot an example in the cmd window, I get: ‘curl’ is not recognized as an internal or external command, operable program or batch file."

Sorry for the remedial questions, but I’m not finding the “Getting started” documentation be of much help on this at all.

4 Likes

I’m assuming you want to try using Spark.functions and Spark,variables.

There’s a simple web app you play and try out here:

http://jflasher.github.io/spark-helper/

As for CURL on windows, cmd won’t do the job…

We need something like for example python to do so.

I use like python and some simple script written by fellow :spark: friends to do the API calls.

Excellent—thanks, Kenneth. I definitely plan on putting the Spark Helper to use in the future. But as for now, say I just want to complete the very first cloud-involved example on http://docs.spark.io/#/examples

If I want to “Control LEDs over the ‘net,” I flash over the code and then the examples page tells me my “API request” must look something like this:

POST /v1/devices/{DEVICE_ID}/led

// EXAMPLE REQUEST IN TERMINAL
// Core ID is 0123456789abcdef01234567
// Your access token is 1234123412341234123412341234123412341234
curl https://api.spark.io/v1/devices/0123456789abcdef01234567/led
-d access_token=1234123412341234123412341234123412341234
-d params=l1,HIGH

Unfortunately, the example page gives me absolutely no hint about how to get started with making API requests. Is there a plug and play solution? Is the best way to complete the example by downloading Python? If I try the Python route, will I need to know a lot more than what Spark provides in the sample API request script?

As you can tell, I’m eager to get started… just need a little help in the orientation department.

So you would probably want to use the Tinker example and use API request to toggle stuff?

You can use your web browser to do that for a start. Some API requests is not simple as just a http address.

Let’s try by pasting the codes below in a web browser:

List all your devices:

https://api.spark.io/v1/devices/?access_token=xxxxxx

Call the digitalWrite function:

https://api.spark.io/v1/devices/core_id/digitalwrite/?access_token=xxxxxxx&args=“D7,HIGH”

I don’t have a core with me right not and cannot determine if the above call works.

I need to take a look at the examples and give you the equivalent http addresses.

Are you trying to learn curl specifically or just the details of the API calls?
If the latter, I recommend using a chrome app called Advanced Rest Client

Here’s what a digitalWrite with the tinker firmware flashed to light up the LED looks like.

@bfalsey if you used before Wordpress you can send data from your website.

Look this link : https://community.spark.io/t/spark-core-plugin-for-wordpress/3553

Kareem — this looks like a solid option. I’m not particularly wedded to curl – I didn’t even know what it was until it showed up in the Spark.io example code – I just want to start using the cloud-based features of the Spark Core, and I gather that means learning to make API calls.

If all that’s right, it looks like the Chrome app you’ve suggested could fit the bill.

Great.
If you’re looking for more cloud features and less coding, take a look my web service project. Depending on what you’re trying to do, it might fit the bill.

http://sccb.azurewebsites.net

YOU can use http://www.hurl.it/ to make HTTP GET or POST Requests to your core as well.

http://bartertronics.com/images/hurlit_http_request.jpg

Php Works with Curl to set Spark Core pin parameters, & HIGH or LOW.

If you already know some PHP it is easy to use Curl and HTML to control your Spark Core.

The Advanced Rest Client worked for me in the example shown. I was able to turn the LED at D7 on and off. However, when I tried this with digitalread on D7, it takes D7 to the LOW state and reports it is LOW. Here’s what I was doing:

https://api.spark.io/v1/devices/(device ID)/digitalread?access_token=(access token)

with
params=D7

Why would digitalread change the state of D7?

I hit the same issue when I first played with it.
It’s how the tinker firmware is built.

When you want to change a pin from an input to an output, you use the pinMode function which sets the value to low as a side effect.

You wouldn’t normally build your firmware with a pin switching between input and output. This is just an artifact of the demo nature of the tinker firmware.

Is there a listing of Tinker Functions and Variables ? Or is the complete Tinker firmware on Github somewhere ?

Thanks,

bobby

It’s all on github. Can’t easily get to it quickly from my phone now but a quick search for sparkcore should do the trick.

I looked before but, I will try again at github. THanks, Bobby

Src/application.cpp will contain it

yes, just found it at github under Core-Firmware/Src/application.cpp

Thank You very much @kareem613 and @kennethlimcp

:blush:

Here you go - Very Simple Approach of others work, spark team included.

IMPORTANT NOTICE: Posting the PHP code to a non-secure server directory will allow someone else to control your Spark Core should they obtain your access_token number used in the CURL statement. I use Joomla and Apache Directory (Password) Protection when I run this code but, you can run it; test it and then change your access_token in Spark IDE later.

Spark IDE - Digital / Analog Write only:
coded starts next lines—>

/* Includes ------------------------------------------------------------------*/
#include “application.h”

/* Function prototypes -------------------------------------------------------*/
int tinkerDigitalWrite(String command);

/* This function is called once at start up ----------------------------------*/
void setup()
{
//Setup the Tinker application here

//Register all the Tinker functions
Spark.function(“digitalwrite”, tinkerDigitalWrite);
}

/* This function loops forever --------------------------------------------*/
void loop()
{
//This will run in a loop
}

/*******************************************************************************

  • Function Name : tinkerDigitalWrite
  • Description : Sets the specified pin HIGH or LOW
  • Input : Pin and value
  • Output : None.
  • Return : 1 on success and a negative number on failure
    *******************************************************************************/
    int tinkerDigitalWrite(String command)
    {
    bool value = 0;
    //convert ascii to integer
    int pinNumber = command.charAt(1) - ‘0’;
    //Sanity check to see if the pin numbers are within limits
    if (pinNumber< 0 || pinNumber >7) return -1;

if(command.substring(3,7) == “HIGH”) value = 1;
else if(command.substring(3,6) == “LOW”) value = 0;
else return -2;

if(command.startsWith(“D”))
{
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, value);
return 1;
}
else if(command.startsWith(“A”))
{
pinMode(pinNumber+10, OUTPUT);
digitalWrite(pinNumber+10, value);
return 1;
}
else return -3;
}

<-----code ended above line

PHP File code next line---->

<?php $ch = curl_init("https://api.spark.io/v1/devices/48ff6f065067555028091087/digitalwrite?access_token=78eb37d1eb1ad33610b907c329d49009dc033079"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "params=D7,HIGH"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); $output = curl_exec($ch); curl_close($ch); $ch = curl_init("https://api.spark.io/v1/devices/48ff6f065067555028091087/digitalwrite?access_token=78eb37d1eb1ad33610b907c329d49009dc033079"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "params=D6,HIGH"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); $output = curl_exec($ch); curl_close($ch); ?>

<— Php file code ended above line

Name your php file something like D6and7_high.php and upload to a webpage/server and run it.


Let me know if I need to clarify more. No need to become a Android App builder when you can control all Analog / Digital pins via PHP & Curl.

Also feel free to PM me so not to high jack this thread (Not much support for PHP & Curl but, I will help best I can).

Bobby

@bfalsey I highly recommend Chrome plugins like those suggested here. I sometimes use one called Postman. Any such plugin will do what you need.

If you decide you want to try curl, to copy-paste specific examples from our docs, here’s a StackOverflow thread that shows you where to download curl and various people’s suggestions on the best ways to use it in Windows.

Happy Hacking!