Use a ESPEasy to control the Bridge of the Philips HUE lights.

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
anarro
Normal user
Posts: 14
Joined: 01 Aug 2016, 08:32

Use a ESPEasy to control the Bridge of the Philips HUE lights.

#1 Post by anarro » 07 Aug 2016, 01:57

Hi,

I update a code with a new command: SendToHUE, I did it finally.. but I dont have too match knowledge of programming in Arduino... someone can I help me to get a better code, please?


1- Add in ESPEasy.ini this:

Code: Select all

#include <RestClient.h>
2- Add in Connand.ini file this:

Code: Select all

 
 .
 ..
  if (strcasecmp_P(Command, PSTR("SendToHUE")) == 0)
  {
    success = true;
    String strLine = Line;
    String host = parseString(strLine,2);
    String port = parseString(strLine,3);
    int pathinipos = getParamStartPos(strLine,4);
    int pathfinpos = getParamStartPos(strLine,5);
    String path = strLine.substring(pathinipos,(pathfinpos-1)); // With this line dont work because change it to a "lower" case.
    //const char *cpath = path.c_str();
    String msgs = parseString(strLine,5);
    //const char *cmsgs = msgs.c_str();

      const char HUE_IP[] = "192.168.1.201";
      const  int HUE_PORT = 80;

      RestClient hue(HUE_IP,HUE_PORT);

      String response;
      response = "";
      int statusCode = 0;
      
      char c_path[(path.length()+1)];
      path.toCharArray(c_path, (path.length()+1));

      char c_msgs[(msgs.length()+1)];
      msgs.toCharArray(c_msgs, (msgs.length()+1));

      statusCode = hue.put(c_path, c_msgs, &response);

      Serial.print("Status code from server: ");
      Serial.println(statusCode);
      Serial.print("Response body from server: ");
      Serial.println(response);

  }
  ..
  .
  
..and then in a Rules seccion I add some think like that:

Code: Select all

on sw1#Switch do
  if [sw1#Switch]=1
    SendToHUE <ip>,<port>,/api/<mykey>/lights/6/state,{"on":true}
  else
    SendToHUE <ip>,<port>,/api/<mykey>/lights/6/state,{"on":false}
  endif
endon

Thank you very match..
Antoni.
Last edited by anarro on 27 Aug 2016, 23:35, edited 3 times in total.

anarro
Normal user
Posts: 14
Joined: 01 Aug 2016, 08:32

Re: Use a ESPEasy to control the Bridge of the Philips HUE lights.

#2 Post by anarro » 27 Aug 2016, 23:30

Hi!

I update a code with only one change:

1- Add in Connand.ini file a new function SendToPUT:

Code: Select all

  if (strcasecmp_P(Command, PSTR("SendToPUT")) == 0)    //ANTONI
  {
    success = true;
    String strLine = Line;
    String host = parseString(strLine,2);
    String port = parseString(strLine,3);
    int pathinipos = getParamStartPos(strLine,4);
    int pathfinpos = getParamStartPos(strLine,5);
    String path = strLine.substring(pathinipos,(pathfinpos-1));
    String msgs = parseString(strLine,5);
    WiFiClient client;
    if (client.connect(host.c_str(), port.toInt()))
    {
        client.println("PUT " + path + " HTTP/1.1");
        client.println("Connection: keep-alive");
        client.println("Host: " + host);
        client.println("Content-Type: text/plain;charset=UTF-8");
        client.print("Content-Length: ");
        client.println(msgs.length());
        client.println();
        client.println(msgs);

        unsigned long timer = millis() + 200;
        while (!client.available() && millis() < timer)
          delay(1);
  
        while (client.available()) {
          String line = client.readStringUntil('\n');
          if (line.substring(0, 15) == "HTTP/1.1 200 OK")
            addLog(LOG_LEVEL_DEBUG, line);
          delay(1);
        }
        client.flush();
        client.stop();
    }
  }

2- To use... only need add a Rules with some to:

Code: Select all

on sw1#Switch do
  if [sw1#Switch]=1
    SendToPUT <ip>,<port>,/api/<mykey>/lights/6/state,{"on":true}
  else
    SendToPUT <ip>,<port>,/api/<mykey>/lights/6/state,{"on":false}
  endif
endon
All is that!

Antoni.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 31 guests