Philips Hue

This part of the sample includes a PUT request to a Philips Hue Bridge (Required TwinCAT version: 3.1.4024.10). The REST API of the bridge enables the TwinCAT user to control Philips Hue devices out of the PLC. The user is required to have a Philips Hue Bridge available through his network and at least one device connected to it.

Basically, the HTTP client, in this case TwinCAT, sends different values as a JSON document to the bridge (e.g. saturation, brightness, state and color value for a bulb). The bridge answers with another JSON document which shows if the commands have been successful.

Philips Hue 1:
Scheme PhilipsHue

Within the setup process, the Philips Hue Bridge randomly generates a username for a device that acts as HTTP client. This username can then be used for the communication with the bridge.

Philips delivers a getting started tutorial which shows how to find the bridge in a network and how to give a client application here: https://developers.meethue.com/develop/get-started-2/
The API description can be found on the same website, a registration is required for that.

The following sample implements on the one hand a single PUT request and on the other hand a toggeling PUT request that is used for a so-called “Blinking mode”. This mode triggers a Philips Hue Go lamp to change the color (between 0 and 65000) every time the timer output is set.

PROGRAM MAIN
VAR
    // trigger command execution for Philips Hue samples
    bPutPhilipsHue : BOOL;

    fbHttpClientPhilips Hue        : FB_IotHttpClient :=(sHostName:='172.17.x.x',
                                     bKeepAlive:=TRUE, tConnectionTimeout:=T#10S);

    fbHttpPutPhilipsHue            : FB_TestHTTP_Put_PhilipsHue;

    bBlinkingMode                  : BOOL;
    fbTimer                        : TON:=(PT:=T#500MS);
    nColor                         : UINT;
END_VAR
//init client parameters at startup
IF NOT fbHttpClientPhilipsHue.bConfigured THEN
    fbHttpClientPhilipsHue.nHostPort:= 80;
    fbHttpClientPhilipsHue.stTLS.bNoServerCertCheck:= FALSE;
END_IF

IF fbHttpClientPhilipsHue.bConfigured THEN
    fbHttpPutPhilipsHue(bSend:=bPutPhilipsHue, fbClient:=fbHttpClientPhilipsHue, nColor:=nColor);
END_IF

IF bBlinkingMode THEN
    fbTimer(IN:=NOT fbTimer.Q);
    IF fbTimer.Q THEN
        nColor:=nColor+5000;
        IF nColor=65000 THEN
            nColor:=0;
        END_IF
        IF bPutPhilipsHue THEN
            bPutPhilipsHue:=FALSE;
        ELSE
            bPutPhilipsHue:=TRUE;
        END_IF
    END_IF
END_IF

fbHttpClientPhilipsHue.Execute();