0

I'm trying to change the light color according to the temperature. I can obtain the temperature value, but I can't change the color basing on its value.

I am using this code.

private async void button6_Click_1(object sender, EventArgs e) {
    string weburl = "http://api.openweathermap.org/data/2.5/weather?q=" + textBox1.Text + "&mode=xml&units";
    var client = new System.Net.WebClient();
    var xml = await new WebClient().DownloadStringTaskAsync(new Uri(weburl));

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    string szTemp = doc.DocumentElement.SelectSingleNode("temperature").Attributes["value"].Value;
    double temp = (double.Parse(szTemp) - 32) * 5 / 9;              
    label1.Text = temp.ToString("N1") + " Celcius"; 
    double degree = temp;

    if (degree >= 20 && degree <30)
    {
        MessageBox.Show("hot");
        var command = new LightCommand();
        command.TurnOn().SetColor("000000");
        command.Brightness = 128;
        var result = await client.SendCommandAsync(command);
    }
}

When I debug it, it gives me the following error.

WebClient does not contain a definition for 'SendCommandAsync' and no extension method 'SendCommandAsync' accepting a first argument of type 'WebClient' could not be found(are you missing a using directive or an assembly reference?)

How I can solve this error?

Under the button 6 code, can I make another button (let's say button 7) which changes color to red, and make this button 7 execute automatically when the temperature is greater than 20 degree Celsius?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Pearl Fall
  • 21
  • 5
  • Are you aware of the System.Net.HttpClient class? You might find with it's GET/POST/PUT/DELETE methods it's more helpful for sending and receiving messages from the web api you are working with... – Neil Hibbert Jun 15 '17 at 12:30
  • It seems to me that you are using `System.Net.WebClient` which is **not** a Philips Hue WebClient. You're using [Microsofts WebClient](https://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx) and it does not have a SendCommandAsync method, as the error message states. – default Jun 15 '17 at 12:53
  • @Default Hello. Yes I'm using System.Net.Webclient. – Pearl Fall Jun 15 '17 at 13:16
  • @Default Then, what do I Need to use instead of System.Net.Webclient? – Pearl Fall Jun 15 '17 at 13:17
  • @Default However, private void button1_Click(object sender, EventArgs e) { var command = new LightCommand(); command.TurnOn().SetColor("BBEE88"); command.Brightness = 128; client.SendCommandAsync(command); } This Code works. Even if I'm using System.Net.Webclient – Pearl Fall Jun 15 '17 at 13:18
  • @NeilHibbert Hello, I tried to use System.Net.HttpClient but it still has same error. :( – Pearl Fall Jun 15 '17 at 13:21
  • Then you have defined `client` as a member in your class and that is *not* a System.Net.WebClient. Otherwise I don't see how that code would compile – default Jun 15 '17 at 13:41
  • @Default Do you have any idea how Webclient can contain Definition for SendCommandAsync? Cuz if i try to just Change Color withtout weather Information, my Code works. – Pearl Fall Jun 15 '17 at 13:48
  • Sorry no - you would have to provide a [MCVE](https://stackoverflow.com/help/mcve) for me to figure that out. – default Jun 15 '17 at 13:55
  • Did you read my comment about defining `client` somewhere else? – default Jun 15 '17 at 13:58
  • @Default I arleady define client in my Code. Actually I don't know what to do further. Can you see my Code if I send it to you? – Pearl Fall Jun 15 '17 at 14:04
  • remove this line: `var client = new System.Net.WebClient();` – default Jun 15 '17 at 14:17
  • @Default Oh mh god!!!!!!!!!!!!!!!!!!!! It works!!! Oh my goddddd!! Thank you so much!!!!!!!!!!!!!!! – Pearl Fall Jun 15 '17 at 14:19

0 Answers0