C# Controlling LEDs over the Internet with wunderground.com

I’m working on a project the reads data from wunderground.com API from C# and I’m having problems passing data to the spark core.

I’m able to call the spark core function from c# but am unable to pass the correct data to the function.

---------------Here is the spark core code------------------

// name the pins
int led = D0;
int led2 = D7;
int delaytime = 10;

// This routine runs only once upon reset
void setup()
{
   //Register our Spark function here
   Spark.function("led", ledControl);

   // Configure the pins to be outputs
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);

   // Initialize both the LEDs to be OFF
   digitalWrite(led, LOW);
   digitalWrite(led2, LOW);
}


// This routine loops forever
void loop()
{
   digitalWrite(led2, LOW);
  digitalWrite(led, HIGH);   
  delay(delaytime);            
  digitalWrite(led2, HIGH); 
  digitalWrite(led, LOW);   
  delay(delaytime);    
   
}


int ledControl(String command)
{
    
    if(command == "0")
        delaytime = 1000;
    else if(command == "1")
        delaytime = 2000;
    
    else if(command == "2")
        delaytime = 3000;
    
    else if(command == "3")
        delaytime = 4000;
    else
        delaytime = 50;//error did not get command
    return 1;
   
}

---- Here is the C# code-----------------------

private void PrepConditionToSend(string con)         
        {

            switch (con)
            {
                case "Chance of a Thunderstorm":
                    SendtoCore("0");
                    break;
                case "Mostly Cloudy":
                    SendtoCore("1");
                    break;
                case "Partly Cloudy":
                    SendtoCore("2");
                    break;
                case "Clear":
                    SendtoCore("3");
                    break;
            }

        }

private byte[] SendtoCore(string command)
  {
            WebClient client = new WebClient();
            byte[] response = client.UploadValues("https://api.spark.io/v1/devices/00000000000000000000000/led", new NameValueCollection()
            {
                { "access_token", "0000000000000000000000000000000000" },
                { "parms", command }
            });

    return response;
  }
1 Like

Try it with {"args", command} and your LEDs will shine :sunny:

1 Like

Thanks rastapasta, I gave it a try and still no luck. wish I could output the value to a console from the core, this would make it a breeze to debug.

Hummm, thought it would just be the argument naming in this case.

If you hook yourself up to the core via USB, you actually are able to do Serial debugging to your console.

Check out http://docs.spark.io/firmware/#communication-serial to see how the Serial implementation works and either use spark-cli’s serial monitor or any other serial software to get the output to your console!

Good luck!

The param name (“parms” or “arg” etc.) is completely ignored by the cloud and never reaches the core in any case.

Have you tried curl and that works OK? You core code looks OK to me but it is always good to test it.

1 Like

Eii, didn't know that the name is completely ignored while the request is being parsed @api.spark.io, interesting + thanks for sharing! A bit uncommon though :wink: Would be nice to have that in the documentation as well, currently it describes "args" as parameter field name in the cURL example.

I got the curl to work but still no luck from C#
Also tried from http://www.hurl.it/ and was also succesfull so there is somthing wrong with my C# code.

1 Like

– edit: wrong info, me will be silent about a language I’m not comfortable with :wink: — good luck!

I found the problem, it was a typo on my submit button. was sending the button value and not the text box value. Duh! slaps face

4 Likes