0

I want to turn my lights off with Java using the Philips HUE Api. For this, I have the following API: http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/

This returns:

{
  "state": {
    "on": true,
    "bri": 178,
    "hue": 41044,
    "sat": 56,
    "effect": "none",
    "xy": [
      0.3313,
      0.3447
    ],
    "ct": 181,
    "alert": "select",
    "colormode": "ct",
    "mode": "homeautomation",
    "reachable": true
  },
  "swupdate": {
    "state": "noupdates",
    "lastinstall": "2020-07-03T12:17:24"
  },
  "type": "Extended color light",
  "name": "Hue color spot 1",
  "modelid": "LCG002",
  "manufacturername": "Signify Netherlands B.V.",
  "swconfigid": "9FD98F72"
}

Now I want to modify the "on: true" to "on: false" with Java. For that, I have this code:

    URL url = new URL("http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("PUT");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setConnectTimeout(60000);
    connection.setReadTimeout(60000);
    connection.setRequestProperty("Content-Type", "application/json");
    JSONObject requestBody = new JSONObject();
    requestBody.put("on", false);

    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(requestBody.toString());
    writer.flush();
    writer.close();

    // Debug
    int response = connection.getResponseCode();
    System.out.println(requestBody.toString());
    System.out.println(response);

This returns '200' and {"on":false} which would be perfect. But for some reason, it just does nothing. The light remains on even though the connection exists.

Any ideas why?

WhySoToxic
  • 155
  • 1
  • 9
  • What do you mean exactly by "it doesn't send"? – tgdavies Aug 22 '20 at 10:37
  • When you try to curl it via command line, both for on:true and on:false, what is the response code? e.g : curl -X PUT -d '{"on":true}' http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/ – planben Aug 22 '20 at 10:52
  • I executed the command `curl -X PUT -d '{"on":false}' 192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/state` both in the Windows Shell and the git bash (Linux). It worked in the git bash but not in the cmd. – WhySoToxic Aug 22 '20 at 11:20
  • @tgdavies I edited and tried to explain it better. – WhySoToxic Aug 22 '20 at 11:22
  • You need to have another api (Link) for edit. – Wowo Ot Aug 22 '20 at 11:28
  • No. I use this documentation: https://developers.meethue.com/develop/get-started-2/#findme2 And according to this, this has to work. – WhySoToxic Aug 22 '20 at 11:49
  • When you say "it worked in the git bash...". What's 'git bash'? And do you mean that the lightbulb turned off? – tgdavies Aug 23 '20 at 01:20
  • With git bash I mean the command shell that you get when you instal git. Its just a normal linux command shell, so the curl command would most likely work in any linux shell. Also yes, I mean the lightbulb turned off. – WhySoToxic Aug 23 '20 at 12:03

2 Answers2

1

You are using the wrong URL for the PUT request.

A GET request to http://192.168.0.53/api/.../lights/1 will return the current state

But to modify you need a PUT request to http://192.168.0.53/api/.../lights/1/state

miknik
  • 5,748
  • 1
  • 10
  • 26
0

I am trying to do the same thing, but don't know the Java part.

I got curl to work at the command level with triple quotes:

curl -X PUT -H "Content-Type: application/json" -d {"""on""":false} http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/state

This code will work with your hue lights, Please share the Java code with me when you get it to work. mike@toggle.is

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Mike H
  • 1
  • 2