peter-murray / node-hue-api

Node.js Library for interacting with the Philips Hue Bridge and Lights
Apache License 2.0
1.18k stars 144 forks source link

Example doesn't work #224

Closed dmzoneill closed 1 year ago

dmzoneill commented 1 year ago

Hey,

I tried the sample solution three times after pressing the button on the hue bridge.. The URL contacted returns 429.
The result below and the curl commands show it correctly detected the hue bridge. Any idea on how to fix?

curl -k -v https://192.168.0.50/api/config
*   Trying 192.168.0.50:443...
> GET /api/config HTTP/2
> Host: 192.168.0.50
> user-agent: curl/7.85.0
> accept: */*
> 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 429 
< server: nginx
< date: Tue, 27 Sep 2022 19:10:32 GMT
< content-type: text/html
< content-length: 596
< etag: "61030684-254"
< 
* TLSv1.2 (IN), TLS header, Supplemental data (23):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>hue personal wireless lighting</title>
    <link rel="stylesheet" type="text/css" href="/index.css">
</head>
<body>
    <div class="philips-header">
      <img src="/philips-blue.png" class="philips-logo" alt="Philips" />
    </div>
    <div class="header">
      <img class="header-logo" src="/hue-logo.png" alt="hue personal wireless lighting" />

      <img src="/hue-color-line.png" class="colorline" />

    </div>
    <div class="error">Oops, there appears to be no lighting here</div>
</body>
</html>
* Connection #0 to host 192.168.0.50 left intact
const v3 = require('node-hue-api').v3
  , discovery = v3.discovery
  , hueApi = v3.api 
;

const appName = 'node-hue-api';
const deviceName = 'example-code';

async function discoverBridge() {
  const discoveryResults = await discovery.nupnpSearch();

  if (discoveryResults.length === 0) {
    console.error('Failed to resolve any Hue Bridges');
    return null;
  } else {
    // Ignoring that you could have more than one Hue Bridge on a network as this is unlikely in 99.9% of users situations
    return discoveryResults[0].ipaddress;
  }
}

async function discoverAndCreateUser() {
  const ipAddress = await discoverBridge();

  // Create an unauthenticated instance of the Hue API so that we can create a new user
  const unauthenticatedApi = await hueApi.createLocal(ipAddress).connect();

  let createdUser;
  try {
    createdUser = await unauthenticatedApi.users.createUser(appName, deviceName);
    console.log('*******************************************************************************\n');
    console.log('User has been created on the Hue Bridge. The following username can be used to\n' +
                'authenticate with the Bridge and provide full local access to the Hue Bridge.\n' +
                'YOU SHOULD TREAT THIS LIKE A PASSWORD\n');
    console.log(`Hue Bridge User: ${createdUser.username}`);
    console.log(`Hue Bridge User Client Key: ${createdUser.clientkey}`);
    console.log('*******************************************************************************\n');

    // Create a new API instance that is authenticated with the new user we created
    const authenticatedApi = await hueApi.createLocal(ipAddress).connect(createdUser.username);

    // Do something with the authenticated user/api
    const bridgeConfig = await authenticatedApi.configuration.getConfiguration();
    console.log(`Connected to Hue Bridge: ${bridgeConfig.name} :: ${bridgeConfig.ipaddress}`);

  } catch(err) {
    if (err.getHueErrorType() === 101) {
      console.error('The Link button on the bridge was not pressed. Please press the Link button and try again.');
    } else {
      console.error(`Unexpected Error: ${err.message}`);
    }
  }
}

// Invoke the discovery and create user code
discoverAndCreateUser();
**************************************************************************************************
Deprecated Function Usage: require('node-hue-api').v3.discovery.nupnpSearch()

  Use require('node-hue-api').discovery.nupnpSearch()

  Function will be removed from node-hue-api in version 6.x
**************************************************************************************************
/home/dave/src/whatsbot/node_modules/node-hue-api/dist/cjs/api/http/HttpClientFetch.js:104
                throw new HttpError_1.default(res.status, res.url, res.headers.raw(), data);
                      ^

HttpError: HTTP error status: 429; "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n<html>\n<head>\n    <title>hue personal wireless lighting</title>\n    <link rel=\"stylesheet\" type=\"text/css\" href=\"/index.css\">\n</head>\n<body>\n    <div class=\"philips-header\">\n      <img src=\"/philips-blue.png\" class=\"philips-logo\" alt=\"Philips\" />\n    </div>\n    <div class=\"header\">\n      <img class=\"header-logo\" src=\"/hue-logo.png\" alt=\"hue personal wireless lighting\" />\n\n      <img src=\"/hue-color-line.png\" class=\"colorline\" />\n\n    </div>\n    <div class=\"error\">Oops, there appears to be no lighting here</div>\n</body>\n</html>\n"
    at /home/dave/src/whatsbot/node_modules/node-hue-api/dist/cjs/api/http/HttpClientFetch.js:104:23
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async discoverAndCreateUser (/home/dave/src/whatsbot/hue-discovery.js:25:30) {
  status: 429,
  url: 'https://192.168.0.50/api/config',
  headers: [Object: null prototype] {
    server: [ 'nginx' ],
    date: [ 'Tue, 27 Sep 2022 19:05:22 GMT' ],
    'content-type': [ 'text/html' ],
    'content-length': [ '596' ],
    connection: [ 'close' ],
    etag: [ '"61030684-254"' ]
  },
  data: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">\n' +
    '<html>\n' +
    '<head>\n' +
    '    <title>hue personal wireless lighting</title>\n' +
    '    <link rel="stylesheet" type="text/css" href="/index.css">\n' +
    '</head>\n' +
    '<body>\n' +
    '    <div class="philips-header">\n' +
    '      <img src="/philips-blue.png" class="philips-logo" alt="Philips" />\n' +
    '    </div>\n' +
    '    <div class="header">\n' +
    '      <img class="header-logo" src="/hue-logo.png" alt="hue personal wireless lighting" />\n' +
    '\n' +
    '      <img src="/hue-color-line.png" class="colorline" />\n' +
    '\n' +
    '    </div>\n' +
    '    <div class="error">Oops, there appears to be no lighting here</div>\n' +
    '</body>\n' +
    '</html>\n'
}

Node.js v18.7.0
okdevko commented 1 year ago

Try using the mdns discovery. There's an example in the examples dir.

dmzoneill commented 1 year ago

i was able to turn on the light with the minor refactoring. thanks very much!!!!!