REST api call to Philips hue bridge fails

#1

Hello,

I’m trying to connect the cactus micro rev2 to my hue light system. That can be configured via some simple put requests like this curl request here:

curl -H "Accept: application/json" -X PUT --data '{"on":false}' http://192.168.1.123/api/newdeveloper/lights/4/state

This simple turns off bulb number 4. It tried now for several hours to achieve this with the rest functionality of the cactus. Unfortunately nothing happens. Maybe someone has a hint?

Here is my code:

#include <espduino.h>
#include <rest.h>

#define PIN_ENABLE_ESP 13
#define SSID  "SSID"
#define PASS  "PASSWORD"

ESP esp(&Serial1, &Serial, PIN_ENABLE_ESP);

REST rest(&esp);

boolean wifiConnected = false;

void wifiCb(void* response)
{
  uint32_t status;
  RESPONSE res(response);

  if(res.getArgc() == 1) {
    res.popArgs((uint8_t*)&status, 4);
    if(status == STATION_GOT_IP) {
      Serial.println("WIFI CONNECTED");
     
      wifiConnected = true;
    } else {
      wifiConnected = false;
    }
    
  }
}

void setup() {
 
  
  Serial1.begin(19200);
  Serial.begin(19200);
  
  esp.enable();
  delay(500);
  esp.reset();
  delay(500);
  while(!esp.ready());

  Serial.println("ARDUINO: setup rest client");
  if(!rest.begin("philips-hue/api/newdeveloper")) {
    Serial.println("ARDUINO: failed to setup rest client");
    while(1);
  }

  /*setup wifi*/
  Serial.println("ARDUINO: setup wifi");
  esp.wifiCb.attach(&wifiCb);
  esp.wifiConnect(SSID, PASS);
  Serial.println("ARDUINO: system started");

 
}


void loop() {
  char data_buf[256];

 
  char response[266];
  esp.process();
  if(wifiConnected) {

      sprintf(data_buf, "{\"on\":false}");      
            
      rest.put("/lights/4/state",(const char*)data_buf); 

      rest.getResponse(response, 255);
      Serial.println(response); 
     
      delay(1000);
  }
}

The response coming back from the hue bridge just seems to be rubbish:

kmw��F,}[?�1a���;j�������'
���1��-��9ͧ�gZi<�ظǩ�|�sɆd(g��r[5��=*��+[�O�.��7!l���U�Ļ����l��>���f�
>��۽{�J��؀����d}�V!�<܎��s,��'�qE�qmӼ��ϝ����
#2

You should change the line to correct IP not URI.

  if(!rest.begin("yourapihere-com-r2pgihowjx7x.runscope.net")) {
#3

Thanks for the fast reply. Your suggestion works. Can’t believe it is that easy! Thank you very much!

But I still cannot get back a proper response from the bridge. When I open the URL http://192.168.1.123/api/newdeveloper/lights/4/ in the browser I get back a JSON result:

{"state": {"on":false,"bri":104,"hue":6131,"sat":139,"effect":"none","xy":[0.5068,0.3725],"ct":455,"alert":"none","colormode":"hs","reachable":true}, "type": "Extended color light", "name": "room", "modelid": "LCT007", "manufacturername": "Philips","uniqueid":"xx-xx-xx", "swversion": "123"}

I tried to achieve that with that lines of code to save the result:

rest.get("/api/newdeveloper/lights/4");
rest.getResponse(response, 255);
Serial.println(response); 

The result is simply some crazy non readable characters again. I also tried to convert the response via the JsonObject library but still no result. Do you maybe have another hint how to get back a readable JSON object?

#4

The buffer size is 255

rest.getResponse(response, 255);

But the JSON result length is 291.

{"state": {"on":false,"bri":104,"hue":6131,"sat":139,"effect":"none","xy":[0.5068,0.3725],"ct":455,"alert":"none","colormode":"hs","reachable":true}, "type": "Extended color light", "name": "room", "modelid": "LCT007", "manufacturername": "Philips","uniqueid":"xx-xx-xx", "swversion": "123"}

I think this is a problem.