Sending command from Visual Studio 10

Hi

I am trying to create an application using VB in Visual Studio 2010 to remotely control a Spark Core. To test, I am using the “control an LED over the net” example. So the command is:

“curl https://api.spark.io/v1/devices/123456789/led -d access_token=abcdef123456789 -d params=l1,HIGH”

Putting this in console2 works so I know the core is set up correctly…

In VB, the commands are:

"Dim addbase as String
addbase = “https://api.spark.io/v1/devices/123456789/led -d access_token=abcdef123456789 -d params=l1,HIGH"
Dim client As New WebClient()
Dim res1 As String
res1 = client.DownloadString(addbase)”

But this gives rise to a 400 error. Replacing the command line with:

"addbase = “https://api.spark.io/v1/devices?access_token=abcdef123456789"”

works and gives a sensible return value.

I have a suspicion that the error is caused by Visual Studio replacing the spaces in the command with the URL encoded %20.

Grateful if anyone can provide any help.

From the curl man page:

   -d, --data <data>
          (HTTP) Sends the specified data in a POST request  to  the  HTTP
          server,  in  the  same  way  that a browser does when a user has
          filled in an HTML form and presses the submit button. This  will
          cause curl to pass the data to the server using the content-type
          application/x-www-form-urlencoded.  Compare to -F, --form.

         ...

          If any of these options is used more than once on the same  com‐
          mand  line,  the  data  pieces specified will be merged together
          with a separating  &-symbol.  Thus,  using  '-d  name=daniel  -d
          skill=lousy'  would  generate  a  post  chunk  that  looks  like
          'name=daniel&skill=lousy'.

So to replicate the “curl -d” results you’ll have to submit a POST request to the Spark. This code snippet from MSDN should get you started - http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-27

1 Like

Thanks for your suggestion erjm. Unfortunately, I cannot make this work either. The program lines are:

"Dim request As WebRequest = WebRequest.Create(“https://api.spark.io/”)
request.Method = "POST"
Dim postData As String = "/v1/devices/123456789/led -d access_token=abcdef123456789 -d params=l1,HIGH"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = “application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()”

Again, this throws out a 400 error on the last line. Trying the “… /devices?..” line also does not work.

I suspect this is all getting beyond my (purely amateur) programming capabilities…!

Hi @mrh1

I don’t know too much about Visual Studio but the -d is a switch to curl not to any other POST command that I know, so you have to remove that and pass the parameters a different way.

Thanks bko - that sounds like a very plausible cause. I won’t be able to try it for a day or two but I’ll post the outcome here when I do.

If you like, you can have a read of this two-part tutorial (part1, part2). It demonstrates how to remote control a Core powered RC car from a C# project.

There you can also find a link to Chris Loris’ C#ore library SparkIODotNet.zip, which did help me a lot to get my own projects up and running.

2 Likes

Some good news… the library is now up on GitHub! And I have created an EventsAPI class to handle the Spark Core’s SSE events as native .Net events. Please have a look and comment! and contribute!

1 Like

Thanks to all for your very helpful reponses. Unfortunately, I have yet to be able to make it work! However, one reason for my delay in replying is that in looking around for help I came across Krishnaraj Varma’s Smart Things and Spark Core project. Played around with this and in a very short time I now have an Iphone app that I can use to control my Spark Core. Thanks Krishnaraj for a very useful project. In time I will get back to Visual Studio and VB but for the moment Smart Things is doing exacgly what I need.

1 Like