1

I am building an app that controls three philips hue RGB LED bulbs. I want to be able to change the brightness using the UISlider. Currently I have a UISlider that calls a method upon each change, however, this method far exceeds the philips hue bridge's 10 commands per second limitation. Here is the method I call upon a change in the UI slider.

- (void) changeBulbBrightness: (NSNumber *)currentBrightness
{
    NSTimeInterval timeInterval = [self.timeLastCommandSent timeIntervalSinceNow];
    NSLog(@"Time Since Last command: %f", timeInterval);
    if (timeInterval < -0.3)
    {
        NSLog(@"COMMAND SENT!!!!");
        PHBridgeResourcesCache *cache = [PHBridgeResourcesReader readBridgeResourcesCache];
        PHBridgeSendAPI *bridgeSendAPI = [[PHBridgeSendAPI alloc] init];
        for (PHLight *light in cache.lights.allValues)
        {
            PHLightState *lightState = light.lightState;
            //PHLightState *lightState = [[PHLightState alloc] init];
            if (lightState.on)
            {
                [lightState setBrightness:currentBrightness];
                // Send lightstate to light


     [bridgeSendAPI updateLightStateForId:light.identifier withLightState:lightState completionHandler:^(NSArray *errors) {
                /*if (errors != nil) {
                    NSString *message = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"Errors", @""), errors != nil ? errors : NSLocalizedString(@"none", @"")];
                    if (self.loggingOn)
                    {
                        NSLog(@"Brightness Change Response: %@",message);
                    }
                }
                 */
            }];
        }
        self.timeLastCommandSent = [[NSDate alloc]init];
    }
}
self.appDelegate.currentSetup.brightnessSetting = currentBrightness;
NSLog(@"Brightness Now = %@", currentBrightness);

I've tried making a timer to limit the amount of commands to 10 per second but the bridge still acts the same way it does when it is overwhelmed with commands (stops acceptance of all commands). Any help or direction would be greatly appreciated. Thanks in advance!

Daniel I
  • 127
  • 8
  • The logic looks correct and your check should only send up to 4 commands per second. Have you verified that you are in fact not sending more than 4 commands per second? Add a log statement just before the call to `updateLightState` to verify the frequency. – rmaddy May 08 '15 at 14:25
  • I have an NSLog at the UISlider level which displays the current NSNumber being sent. It sends logs at least 30 times a second (even when the value hasn't changed). – Daniel I May 08 '15 at 14:40
  • That's expected. But you need to log when you actually call `updateLightStateForId` so you can verify your logic. – rmaddy May 08 '15 at 14:41

1 Answers1

0

One reason might be your multiple lights. You are sending an update command for each light. So if you have 3 bulbs connected as in the Hue starter kit, you might still send 10 or a little more if there is some unfortunate caching involved packing the updates from 2 seconds into 1 second of sending. Thus I suggest you further decrease the number of updates you are sending (try 0.5 or even 1.0) as an interval and see if it gets better.

Also note that the SDK is quite vague about the rate limit. It says:

If you stay roughly around 10 commands per second

Since the Philips Hue SDK is generally not that well supported (look at the open GitHub issues), take this with a grain of salt and do your own experiments. Once I have the time to check it myself, I will post an update here.

Update 1: I just discovered this remark by one of the contributors to the Hue SDK github repo (maybe Philips employee) advising to only send 2 commands per second:

As mentioned earlier, be careful when doing lot's of requests in a loop as the calls to PHBridgeSendAPI are not blocking and requests are retained until they get a response or timeout. Two calls per second seems like a safe rate, if you want to have a higher rate, it's advised to chain requests with a queuing mechanism of your own (to ensure memory will be released before new requests are getting called).

Raphael
  • 2,691
  • 1
  • 16
  • 21
  • Sorry i could not comment sooner, I have been very busy. Two commands per second works and is safe, but it is not quick enough to match the sliding rate of the UISlider. It is doable however. They are able todo this in the free philips hue application. When you slide your finger the brightness changes instantly for all three bulbs and updates very rapidly. What could they be doing that i am not? – Daniel I May 18 '15 at 17:51