Philips Hue

Babustino

Level-2
Beiträge
11
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo Zusammen,

ich bin bei mir Zuhause gerade Philips Hue am testen. Dieses System ist zwar etwas teuer, hat mich aber von den Möglichkeiten der Beleuchtungstechnik doch erst einmal überzeugt.

Ich möchte die Leuchten jetzt auch über meine Wago ansteuern.

Hier komme ich zu meinem Problem:

Die meisten Http Requests von Philips Hue sind vom Typ PUT, ich finde aber nur Bausteine von Wago für HTTP_GET und HTTP_POST, auch bei OSCAT habe ich nichts hilfreiches entdeckt bisher.

Hat jemand eine Idee wie ich HTTP_PUT Requests abschicken kann?

Meine bisherige Lösung ist über einen TCP Baustein (TCP_Client)
Ich habe die HTTP Requests mit Wireshark aufgezeichnet. Den Hex Code für Codesys umgewandelt und in einem entsprechendem Array angelegt.
Dazu habe ich 3 verschiedene Programme gebraucht um nicht 531 Bytes mit der Hand zu schreiben :smile:. Diese Variante ist relativ aufwendig wenn man verschiedene Befehle ausführen möchte.

Ich bin dankbar für jede Hilfe :smile:

Meine Bibliotheken:

WagoLibHttp_02.lib
WagoLibEthernet_01.lib

Grüsse Benni
 
Ja, hier der Code:

Code:
FUNCTION_BLOCK HTTP_PUT
VAR_INPUT
    sServerName     : STRING;                     (* Server IP in dotted normal form *)
    wServerPort     : WORD:=80;                 (* PortNumber *)
    sURL            : STRING(200);                (* URL of ressource to call *)
    sData            : STRING(200);                (* Pointer to HTTP-Request-data *)
    sAccept            : STRING;                    (* Request header field "Accept": *)
    sContentType    : STRING;                    (* Reaquest header field "Content-Type": *)
    tTimeOut        : TIME := t#30s;            (* WatchDogTime, time x for cancel operation*)
END_VAR
VAR_IN_OUT
    xSend : BOOL;
END_VAR
VAR_OUTPUT
    diError            : DINT; (* 0 == successful *)
    sStatus            : STRING(255);
    abContentData   : ARRAY [0..MAX_RECEIVE_TCP_CLIENT] OF BYTE; (* The HTTP response *)
    uiContentLength : UINT; (* Length of HTTP response in byte *)
END_VAR
VAR
    wState            : WORD := 0;
    oTON            : TON;        (* Watchdogtimer for statemashine *)


    xWatchDogOn        : BOOL;     (* enable disable Watchdog for statemashine *)
    wStateOld        : WORD;        (* persist the last state of statemashine *)


    oTcpClient        : TCP_Client2;
    xOpenConnection    : BOOL;        (* Open a TCP connection *)
    xTcpSend        : BOOL;      (* Send a TCP telegram *)
    xResult            : BOOL;        (* Return value of functions *)
    diTcpError        : DINT;     (* Return value of oTcpClient *)


    abTxBuffer        : ARRAY[0..MAX_SEND_TCP_CLIENT] OF BYTE;
    abRxBuffer        : ARRAY[0..MAX_RECEIVE_TCP_CLIENT] OF BYTE;
    diTxCount        : DINT; (* Number of bytes to send *)
    diRxCount        : DINT; (* Number of received bytes *)


    oDataRxWd        : TON;    (* Data receive wachdog *)
    diRxCountOld    : DINT; (* Number of received bytes register *)


    (* HTTP-Response *)
    uiResponseCode : UINT;  (* Numeric response code 200 --> OK *)


    (* Helpers *)
    i,k : INT;
    iHelp : INT;
    sHelp : STRING(500);
    psHelp : POINTER TO STRING(500);
    abHelp : ARRAY [0..180] OF BYTE;
    pabHelp : POINTER TO ARRAY [0..160] OF BYTE;
    uiPos : UINT;


END_VAR

Code:
(* Call the TcpClient-FB each cycle *)
            oTcpClient(    xOpenConnection    := xOpenConnection,
            sIPaddress        := sServerName,
            wPortNumber        := wServerPort,
            ptSendData        := ADR(abTxBuffer),
            diSendCount        := diTxCount,
            tConnectWatchdogTime:= t#20s,
            xStartSend        := xTcpSend,
            aReceiveBuffer    := abRxBuffer,
            diReceiveCount    := diRxCount,
            diError            => diTcpError );


CASE  wState OF (* The state maschine *********************************************)


0:    (* Idle - wait for something to do -------------------------------------------*)
    IF xSend THEN
        diError := 0;
        sStatus := 'Idle...';
        uiContentLength := 0;
        wState := 10;
    END_IF


10: (* Open a TCP connection ------------------------------------------------------*)
    xOpenConnection:=TRUE;
    sStatus :=CONCAT('Try to connect to ', sServerName);
    wState:=20;


20: (* Wait until connection is established ---------------------------------------*)
    IF oTcpClient.xConnected THEN
        sStatus :=CONCAT('Connection established with ', sServerName);
        wState:=30;
    END_IF


30:    (*Assemble HTTP-PUT-Request -------------------------------------------------- *)
    sStatus :='Prepare HTTP-Request';
    psHelp := ADR(abTxBuffer);
    (*Prepare PUT*)
    psHelp^:= CONCAT('PUT ', sURL);
    psHelp^:= CONCAT(psHelp^, ' HTTP/1.0$r$n');
    (*Prepare Host*)
    psHelp^:= CONCAT(psHelp^, 'Host: ');
    psHelp^:= CONCAT(psHelp^, sServerName);
    psHelp^:= CONCAT(psHelp^, '$r$n');
    (*Prepare User-Agent*)
    psHelp^:= CONCAT(psHelp^, 'User-Agent: WAGO PLC$r$n');
    (*Prepare Accept*)
    psHelp^:= CONCAT(psHelp^, 'Accept: ');
    psHelp^:= CONCAT(psHelp^, sAccept);
    psHelp^:= CONCAT(psHelp^, '$r$n');
    (*Prepare Content-type*)
    psHelp^:= CONCAT(psHelp^, 'Content-type: ');
    psHelp^:= CONCAT(psHelp^, sContentType);
    psHelp^:= CONCAT(psHelp^, '$r$n');
    (*Prepare Content-length*)
    psHelp^:= CONCAT(psHelp^, 'Content-length: ');
    psHelp^:= CONCAT(psHelp^, INT_TO_STRING(LEN(sData)));
    psHelp^:= CONCAT(psHelp^, '$r$n');
    (*Prepare Put Data*)
    psHelp^:= CONCAT(psHelp^, '$r$n');
    psHelp^:= CONCAT(psHelp^, sData);


    uiPos := LEN(psHelp^);
    psHelp := ADR(abTxBuffer[uiPos]);


    diTxCount := uiPos;
    diRxCount:=0;
    xTcpSend := TRUE;
    sStatus := 'Send Request!';
    wState := 999;


999: IF xTcpSend = FALSE THEN (* Clean up and close connection ------------------------*)
    xOpenConnection := FALSE;
    xSend:=FALSE;
    diRxCount:=0;
    wState:=0;
    sStatus := '';
    END_IF


END_CASE (************************************************************************************)


(* Watchdog for statemachine  *)
IF oTON.Q THEN
    xWatchDogOn := FALSE;
    diError := 16#80004001;     (* Watchdog time elapsed, see sStatus for details *)
    sStatus := CONCAT('TIMEOUT on:', sStatus);
    wState:=999;
END_IF

IF (wState > 0) THEN
    oTON(IN:=xWatchDogOn , PT:=tTimeOut);
    IF (wStateOld <> wState) THEN
        xWatchDogOn := FALSE; (* Step has changed *)
    ELSE
        xWatchDogOn := TRUE; (* Step still active *)
    END_IF
END_IF

wStateOld:=wState;
 
Zurück
Oben