Showing posts with label api. Show all posts
Showing posts with label api. Show all posts

Friday, December 30, 2022

Playing around with Philips Hue lights from the command line...

We own a bunch of Philips Hue light bulbs now, and we just bought some as a gift for one of our kids. You can use the Philips Hue app on your computer and/or mobile device, but it's also a lot of fun to play with the lights (and other Philips Hue devices) from a terminal or an app that you can write for yourself using the Philips Hue API. 

Register to get access to the Developer's API documentation

You can find the Philips Hue Developer documentation here: https://developers.meethue.com/

The Developer page has a login link. Click the Login link, and then select the Register option at the bottom of the Login form. Or, use this link to get to the Register page.


Find your Hue bridge IP address

Follow the instructions listed here to find the IP address of your Hue bridge.

The easiest method for finding your Hue bridge IP address is to make sure you're connected to the same network that the Hue bridge is connected to and then visit this address: https://discovery.meethue.com/

The response will look something like this:

[
  {
    "id": "112233aabb44cc55",
    "internalipaddress": "10.0.0.1",
    "port": 443
  }
]


Use curl to send HTTP requests

You can use curl to send requests to the Hue bridge.

If you're new to using curl, then watch this short video about curl for sending HTTP requests to a server. In our case, the server will be the Philips Hue Hub using its IP address. We'll pretend that the IP address of the Hue bridge is 10.0.0.1 for the examples.


Go to the Hue API docs

After registering/logging in on the Philips Hue Developer page select the Develop menu and then select the Hue API menu item. 


Create a username

The Hue API requires a username. The username is created and saved by your Philips Hue bridge, and the hub adds the username to a list of usernames that are allowed to send API calls to the Hue bridge.

Follow the instructions here to create a username.


Use curl to get light info from the bridge

  1. Open a terminal
  2. Set a variable in your terminal that will hold the Hue Bridge IP address you retrieved above. For example, if your Hue bridge IP address is 10.0.0.1, then you would enter the following:

    HUE_BRIDGE=10.0.0.1

  3. Set a variable in your terminal that will hold Hue username you retrieved above. For example, if your assigned username is nE1wnTaC00ki3, then you would enter:

    HUE_USERNAME=nE1wnTaC00ki3

  4. Send a curl command to your Hue bridge to get info about all of the lights you currently have associated with your bridge:

    curl -XGET http://$HUE_BRIDGE/api/$HUE_USERNAME/lights

You should get a response that is a JSON string containing information about all of your Hue lights.


Saturday, January 20, 2018

AWS IoT Button and Philips Hue API...

I bought some Philips Hue Lights, and have really enjoyed them - but I enjoy them even more now that I have the IoT button integrated with the lights. 

Here is a video showing my AWS IoT button interacting with my Philips Hue Go lamp.



Philips Hue API:

The Philips Hue REST API is really easy to use to retrieve information about the lights connected to the Hue bridge, and for controlling the lights. You can follow the instructions on this page to help you get up and running with the API.

AWS IoT Button:

I had seen the AWS IoT button on Amazon and, although I didn't have any ideas of what I would do with the button, I wanted to work on a project which would use one of the buttons. I found this fun project that also uses an AWS IoT button, and the Philips Hue API with the Go lamp. I had bought a Philips Hue Go light, as well as a number of other Philips Hue lights, so I decided to recreate the project from the youtube video above but using an AWS lambda instead of using a raspberry pi. 

Something that was pointed out to me (embarrassingly) is that this method is not secure. Sending unencrypted information to the Hue bridge, which includes the auth, would allow an attacker to send their own API calls to the bridge. One of the API calls could have a security hole that could be used by an attacker / curious person. 

A couple of ideas I've had for using the Philips Hue lights are flash lights with certain colors to indicate either a rise above, or drop below, stock or crypto currency price points, and flash lights when people are close to home (integrate with IFTTT).

However, using the IoT button to control the lights looked fun and gave me an excuse to learn a little bit about AWS Lambdas. It's worth mentioning that Philips makes a switch that can be easily programmed to control your Philips Hue lights.

Parts list :

- Philips Hue Go light, but it works with all of the Philips Hue color lights
- AWS IoT Button

Set up IoT Button:

I used the "Getting Started" guide to set up the IoT button.  It walks you through registering your device, creating and activating a device certificate, creating and attaching an IoT policy to the device certificate, attach the certificate to a "Thing" (the button), and configuring your IoT button to know how to connect to your WiFi.

One of the last steps in the "Getting Started" guide is configuring and testing rules. The example has the IoT button pushes send an SNS message that gets sent as a text message to your phone. I decided to have the SNS message trigger a lambda, and use the lambda to send the REST calls to my Philips Hue bridge.

AWS Lambda:

Here is the AWS Lambda code that I used:


I have my router configured to use Dynamic DNS, and then I have a port forwarding rule to forward to the Philips Hue bridge.  The lambda figures out if the button click was a single click, a double click, or a long click.  The double clicks will turn the light on and off, the single click will increment the hue to set the light to, and a long click will set the light to use the color loop effect.

I hope you find this post useful! Please leave links to any projects you feel like sharing using AWS IoT buttons and/or Philips Hue lights in the comment section below.

Friday, August 14, 2015

Add and remove fields from Solr schema using Schema API...

Okay - I know what you're thinking. "How can I quickly update my Solr schema without having to go into the schema config file - perhaps using a rest API?" You can use the Solr Schema API, that's how! 

I recently noticed that there is a Schema API in Solr 5.X that can be used to update the Solr schema. You need to have the schemaFactory set to use the "ManagedIndexSchemaFactory", and have the mutable property set to true.  If you want to stop allowing the schema from being updated via the API, then you can change the mutable property to false.

Here are a few of the things that you can do with the schema API:

View the schema for a collection:

http://localhost:8983/solr/yourcollectionname/schema

View all of the fields in the schema:

http://localhost:8983/solr/yourcollectionname/schema/fields

Example output:

{
  "responseHeader":{
    "status":0,
    "QTime":101
  },
  "fields":[
  {
      "name":"_text_",
      "type":"text_general",
      "multiValued":true,
      "indexed":true,
      "stored":false},
    {
      "name":"_version_",
      "type":"long",
      "indexed":true,
      "stored":true},
    {
      "name":"id",
      "type":"string",
      "multiValued":false,
      "indexed":true,
      "required":true,
      "stored":true,
      "uniqueKey":true},
    {
      "name":"somefieldname",
      "type":"lowercase",
      "indexed":true,
      "stored":true},
    {
      "name":"title",
      "type":"strings"
    }
  ]
}


View a specific field in the schema:

http://localhost:8983/solr/yourcollectionname/schema/fields/somefieldname

Example output:

{
  "responseHeader":{
    "status":0,
    "QTime":1},
  "field":{
    "name":"somefieldname",
    "type":"lowercase",
    "indexed":false,
    "stored":true
  }
}

Now add a new field called "anotherfield" that is of type "text_en", stored, and indexed:

curl -X POST -H 'Content-type:application/json' --data-binary '{"add-field":{"name":"anotherfield","type":"text_en","stored":true,"indexed":true }}' http://localhost:8983/solr/yourcollectionname/schema


Now let's see that the field exists:

http://localhost:8983/solr/yourcollectionname/schema/fields/anotherfield

{
  "responseHeader":
  {
    "status":0,
    "QTime":1
  },
  "field":
  {
    "name":"anotherfield",
    "type":"text_en",
    "indexed":true,
    "stored":true
    }
}

Now let's delete the field:

curl -X POST -H 'Content-type:application/json' --data-binary '{"delete-field" : { "name":"anotherfield" }}' http://localhost:8983/solr/yourcollectionname/schema

And check to see that it is deleted:

http://localhost:8983/solr/yourcollectionname/schema/fields/anotherfield

{
  "responseHeader":
  {
    "status":404,
    "QTime":2
  },
  "error":
  {
    "msg":"Field 'anotherfield' not found.",
    "code":404
  }
}

There are other actions that you can do using the Schema API. Here are a few of the things that you can do using the Schema API:
- replace a field
- add and remove dynamic field patterns
- view dynamic fields
- and and remove field types
- view field types