How to control Philips hue light from Blynk app?

both

I tried everything I could think off, without much success…
For now I think I will wait till someone chimes in who has more experience and happens to have a hue bridge and lights and the same desire to control all automation from 1 place…

I don’t think there are many people in the world that have more experience of using Blynk’s Webhook widget with 3rd party API’s (Thingspeak, EmonCMS, Wunderground, SunriseSunset, Google Sheets, Binance and Blynk’s own API etc). Yes someone with the your light might be able to assist further.

I have never been beaten by an API yet and don’t intend for Philips to be the first.

Studying the API at Login - Philips Hue Developer Program now.

Looks like they are sticking rigidly to PUT for control.

I have created an account with Philips. I didn’t see any reference to username in any of your urls.

A browser should GET the details with the following syntax /api/<username>/lights

Have you managed to do this first step yet?

Have you thought about trying IFTTT?
They have a Phillips Hue service and applets. If you’re not that familiar with web service APIs then you might find it easier to use that method. There are a few threads about using Blynk with IFTTT, so you should have plenty of info to play with.

Pete.

@Dmitriy if we want to send a body via PUT like {"bri":42} using "/pin/" in the Webhook does the following code extract look ok?

unsigned int lightBrightness = 42;

String msgBody = ("{""\"bri\":" + String(lightBrightness) + "}");

Blynk.virtualWrite(V0, msgBody); // Webhook allocated to V0

@jbowser the code above is the syntax we use for sending json to 3rd party API’s, only difference is we normally use GET rather than PUT.

I didnt mean to be offensive, I was referring to someone with hue api experience.

Yes that worked, everything in this tutorial Get Started - Philips Hue Developer Program worked! So I followed their instructions to make a user (which was a very long and cryptic set of characters and numbers), which I added to all my attempts.

Should I put this code on an esp8266 (I have a bunch of different boards) to try?

Good now we are getting somewhere.

Yes but I don’t know how much Blynking experience you have.
Do you have plug and play ESP’s like the Blynk board, WeMos or NodeMCU?
Have you flashed any working Blynk sketches to your ESP before?
Do you now know the syntax for the Webhook i.e ["/pin/"] in the PUT body is literally that, not V0, D0 etc?
Do you want a full sketch or are you good to go now?

A basic sketch to use the slider with the light would be:

/*************************************************************
 WebhookPUTtest.ino by Costas the code 13/3/18
 Control Philips Hue Light with Blynk Webhook widget
 V0 Webhook widget url syntax: http://<bridge ip address>/api/<username>/lights/2/state
    Method PUT
    Body ["/pin/"]   
 V1 Blynk slider with no decimal places set as send on release   
    range say 50 to 100
 *************************************************************/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define BLYNK_MAX_READBYTES 1024 //added based on webhook docs

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
unsigned int lightBrightness = 50;

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}


BLYNK_WRITE(V0)   // Webhook widget
{
  Serial.println(param.asStr());  // display response from hardware  
}

BLYNK_WRITE(V1)  // Slider
{
  lightBrightness = param.asInt();
  String msgBody = ("{""\"bri\":" + String(lightBrightness) + "}");
  Serial.println(msgBody); //extra feedback to ensure slider value is received
  Blynk.virtualWrite(V0, msgBody); // send to Webhook on V0  
}

void loop()
{
  Blynk.run();
}
1 Like

I have some blynking experience, made a cycle timer to investigate the ease of operation and integration of the cnc machines at my work using wemos d1 mini’s.

So with this sketch the wemos would act like a sort of bridge between the app and the Hue bridge?
I will try this sketch later this evening when I’m home again!

Excellent choice of MCU.

That’s correct.
The sketch compiles but I can’t easily test it beyond compilation.
Took me days to work out the escape sequence for json etc but if you follow it exactly you should be good to go.
You can then expand the msgBody String for additional control.

Philips also have a complex remote control system that works similar to the authentication used by Google for Sheet access i.e. oauth2 and tokens etc. We have previously hacked Google systems to use this authentication but it’s not straightforward. With the mighty WeMos and Blynk you should be able to bypass all this complex stuff and control your lights from anywhere in the world.

Well I’d like to try this, but after updating my local server to 0.33.1 and setting the static IP address of the pi and the hue bridge my wemos d1 mini and feather esp32 won’t connect to the server.

Blynk library version 0.5.1 and app version 2.19.2

ps. I’m getting connected to my wifi, just not the server and my app CAN connect to the server

Use Blynk’s cloud server.

Switched over to Blynk’s cloud, now my wemos is connected.
I set the webhook as follows:

  • Output: V0
  • URL: http://<hue bridge ip>/api/<verified hue user>/lights/2/state (I know light 2 is next to me)
  • Method: PUT
  • Content type: application/json
  • Body: “/pin/”
  • Target: the connected wemos

I loaded the wemos with the following sketch (2 extra additions) and I also tried it using {} around the msgBody, but I get no response back from the lamp (the lamp is already on and the slider value is coming in correctly).

/*************************************************************
 WebhookPUTtest.ino by Costas the code 13/3/18
 Control Philips Hue Light with Blynk Webhook widget
 V0 Webhook widget url syntax: http://<bridge ip address>/api/<username>/lights/2/state
    Method PUT
    Body "/pin/" 
 V1 Blynk slider with no decimal places set as send on release   
    range say 50 to 100
 *************************************************************/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define BLYNK_MAX_READBYTES 1024 //added based on webhook docs

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "TP-LINK_2.4GHz_BE9EF1";
char pass[] = "xxxxxxxxxxxxxx";
unsigned int lightBrightness = 50;

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}

BLYNK_WRITE(V0)   // Webhook widget
{
  Serial.println(param.asStr());  // display response from hardware  
}

BLYNK_WRITE(V1)  // Slider
{
  lightBrightness = param.asInt();
  String msgBody = ("{""\"bri\":" + String(lightBrightness) + "}");
  Serial.println(msgBody); //extra feedback to ensure slider value is received
  Blynk.virtualWrite(V0, msgBody); // send to Webhook on V0  
}

void loop()
{
  Blynk.run();
}

Yes you will need these.
What does msgBody display as in Serial Monitor from the slider movement?

Set up another Webhook with the working GET command and code up a button in PUSH mode to trigger it and display result to Serial Monitor.

With the button code set it to trigger just with ==1.

Don’t over test the system. 10 failures and you are blocked.

This is my serial output:

⸮[5103] Connecting to TP-LINK_2.4GHz_BE9EF1
[6105] Connected to WiFi
[6105] IP: 192.168.0.102
[6105]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.5.1 on Arduino

[6111] Connecting to blynk-cloud.com:80
[6290] Ready (ping: 2ms).
Starting program…
1
0
{“results”:{“sunrise”:“3:34:07 AM”,“sunset”:“3:23:09 PM”,“solar_noon”:“9:28:38 AM”,“day_length”:“11:49:02”,“civil_twilight_begin”:“3:09:26 AM”,“civil_twilight_end”:“3:47:50 PM”,“nautical_twilight_begin”:“2:40:40 AM”,“nautical_twilight_end”:“4:16:36 PM”,“astronomical_twilight_begin”:“2:11:47 AM”,“astronomical_twilight_end”:“4:45:29 PM”},“status”:“OK”}
{“bri”:184}

The last line is from the slider

Correction not [/pin/] in the webhook just /pin/ and when you enter this the text will change from white to green to confirm it’s the correct syntax.

I have edited all the earlier posts to show /pin/ and include {}
I can see your basic sunrise sunset api call is working with GET.
Test the Philips GET with webhook (not for changing the hue).

Could it be that V0 and therefore the webhook attached to it isn’t being triggered by Blynk.virtualWrite(V0, msgBody); ?

Have you changed the webhook from[/pin/] to /pin/ ?

You could code up a button for testing but the slider should be fine.

I would make sure GET works before moving to PUT.