-->
Page 1 of 1

Call Hue Scenes from a Harmony IR remote

PostPosted: Thu Mar 31, 2016 3:18 pm
by eqsOne
This is my attempt on calling my favorite Philips Hue Scenes from a Harmony IR remote without using the dedicated 'Harmony Hub'. Of course almost any other IR remote will do, too.
A simple IR receiver (TSOP 4838) is attached to an ESP8266's GPIO2. On receiving defined IR code, the ESP sends a command to the Hue bridge, calling a certain Hue Scene.

This blog post has actually been a great help, adapted and learned a lot of it.


Happy tinkering.

Code: Select all/////////////////////////////////////
// Call HUE Scenes from IR remote ///
/////////////////////////////////////
/*
Adapted from Arduino code by James Bruce (http://www.makeuseof.com/tag/control-philips-hue-lights-arduino-and-motion-sensor/)
and Gilson Oguime (https://github.com/oguime/Hue_W5100_HT6P20B/blob/master/Hue_W5100_HT6P20B.ino).
*/

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <IRremoteESP8266.h>

// IR
int IR_PIN = 2; //data-pin (TSOP 4838) at GPIO2
IRrecv irrecv(IR_PIN);
decode_results results;

//  Hue constants
const char hueHubIP[] = "00.00.00.00";  //Hue bridge IP
const char hueUsername[] = "xxxxxxxxxxxxxxxxxx";  //Hue username
const int hueHubPort = 80;

//  Hue variable
String hueCmd;  //Hue command

unsigned long buffer=0;  //buffer for received data storage
unsigned long addr;
 
//  WiFi
WiFiClient client;
const char* ssid     = "Your_WiFi_name";
const char* password = "Your_WiFi_password";

//  Setup
void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn(); //start IR receiver
 
  // connect WiFi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA); //Client mode only, no Access Point
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print("_");
  }

// Serial output when connected
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("Current IP: ");
  Serial.println(WiFi.localIP());
  delay(1000);
  Serial.println("Ready.");
  Serial.println("");
}

// Main
void loop()
{
    if(irrecv.decode(&results))
    {
      switch (results.value) {
        case 0x58E9: // Uncomment line 95 to show received code in terminal. Insert 0x58E9 for IR code '58E9' i.e.
        Serial.println("Scene name #1"); //type in Hue Scene name
        String command = "{\"scene\":\"123456789-on-0\"}"; //replace with the 9-digit number of your Hue Scene
        setHue(command);
        break;
      }
      switch (results.value) {
        case 0x0002:
        Serial.println("Scene name #2");
        String command = "{\"scene\":\"123456789-on-0\"}";
        setHue(command);
        break;
      }
      switch (results.value) {
        case 0x0003:
        Serial.println("Scene name #3");
        String command = "{\"scene\":\"123456789-on-0\"}";
        setHue(command);
        break;
      }
      switch (results.value) {
        case 0x0004:
        Serial.println("Scene name #4");
        String command = "{\"scene\":\"123456789-on-0\"}";
        setHue(command);
        break;
      }
 
  //Serial.println(results.value, HEX); //show received IR code
  irrecv.resume(); //receive next value
  delay(100);
  }
}

  /*
    Since I only have one single lightgroup to talk to (Group 0),
    I made that fix for the 'setHue' string.
    If you have different groups or lights to manage, you'll have
    to add a group- and/or light-number parameter. Check out this
    blog post to see how you might do so:
    (http://www.makeuseof.com/tag/control-philips-hue-lights-arduino-and-motion-sensor/)
  */

boolean setHue(String command)
{
  if (client.connect(hueHubIP, hueHubPort))
  {
    if (client.connected())
    {
      client.print("PUT /api/");
      client.print(hueUsername);
      client.print("/groups/0"); //calls Scenes within 'Group 0'
      client.println("/action HTTP/1.1");
      client.println("keep-alive");
      client.print("Host: ");
      client.println(hueHubIP);
      client.print("Content-Length: ");
      client.println(command.length());
      client.println("Content-Type: text/plain;charset=UTF-8");
      client.println();  //blank line before body
      client.println(command);  //Hue command
    }
    client.stop();
    return true;  //command executed
  }
  else
    return false;  //command failed
}


Some research in order to make it work:

I used Arduino 1.6.5, the sketch requires the IRremoteESP8266 library, which can be found here.


Hue related

The Hue bridge IP can be found within the Hue App's settings. A fixed bridge IP should also be set, otherwise you'll have to change the sketch anytime the bridge gets a new IP via DHCP.

To get a valid Hue-username (according to this documentation):

- While in your home network go to http://<bridge ip address>/debug/clip.html
- In the 'Body' field type in {"devicetype":"my_hue_app#iphone peter"} and replace 'iphone peter' with your device name
- Click 'Post' > receive an Error
- Now press the bridge's link button, then again click 'Post' > receive your username

To find a Hue Scene's ID in your bridge (according to that documentation):

- While in your home network go to http://<bridge ip address>/debug/clip.html
- In the 'URL' section after /api/ type in <your_username>/scenes
- Click 'GET'
- Search for a particular scene and copy/paste it's unique 9-digit ID (i.e. "12ab56c89-on-0\")


IR related

To read IR code from the buttons of your favorite IR-remote, you might uncomment line 95 in this sketch and run it. Any received IR code will be shown in terminal.


Harmony related

In my Harmony remote I added a new device that I don't actually own (some Sony TV), renamed it 'Philips Hue' and read out the transmitted IR code for several buttons (green, blue, yellow & red in my case). Then I added this new 'Hue' device to my 'Actions' and linked it's color buttons to all of them. Now each color button calls one of my favorite Scenes.

Additionally you could also tell your Harmony to call the related Scene straight on tapping a particular 'Action' i.e. 'watch a movie'.

In case you need the chosen buttons for a different device at the same time (like the TV), you might use long press and short press commands in the Harmony setup.

Since I use a wall mounted Hue Switch to turn off all lights at the end of a day, there's no 'all off' command declared in here.


Current supply:

My ESP is wired as shown below with a LD1117 (3.3V) + 100µF cap wich gets powerded through my amplifier's USB port, as this device is included in all of my Harmony 'Actions'. As the amp gets switched off, the ESP will be shut down, too. You may handle this differently and have it running all the time:

IR Receiver Board.jpg


This is the wiring of my ESP progamming board. For me it works with a 5V FTDI (PL-2303) on El Capitan as well as on Win7. VCC and TX are being converted to 3.3V. To take the ESP into programming mode press and hold the reset button, then press and hold the GPIO1 button, then release reset, then release GPIO1:

Programming Board.jpg


Everything assembled:

IMG_5578.JPG


IMG_5581.JPG


IMG_5582.JPG