Sending a PUT request with an arduino

6k views Asked by At

I'm trying to send a JSON string via PUT from an arduino in order to control a philips Hue smart light. I've googled and found a lot about POST and GET, but not much on PUT. I'm attempting to PUT "{"on":false}" to my local Hue bridge (/api/[key]/lights/3/state), but don't now how to format it. Can anyone help?

Here's the console info when I successfully send a request using the Hue's debugging tool:

Accept   text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    
Accept-Encoding   gzip, deflate    
Accept-Language   en-us,en;q=0.5    
Connection   keep-alive    
Content-Length   12    
Content-Type   text/plain; charset=UTF-8    
Host   192.168.1.8    
Referer   http://192.168.1.8/debug/clip.html    
User-Agent   Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:12.0) Gecko/20100101 Firefox/12.0

Here's what I been trying, unsuccessfully:

     #include <SPI.h>
    #include <Ethernet.h>

byte mac[]     = { 0x90, 0xA2, 0xDA, 0x0D, 0x83, 0x9D };
byte ip[]      = { 192, 168,   1,  199 }; 
byte gateway[] = { 192, 168,   1,  1 };   
byte subnet[]  = { 255, 255, 255,   0 };   

void setup()
{
  Ethernet.begin(mac, ip); 
 Serial.begin(9600); 
 delay(1000);
}
void loop()
{ 

 EthernetClient client;

IPAddress server(192,168,1,8);

if (client.connect(server,80))
{
   client.println("PUT /api/[key]/lights/3/state HTTP/1.1");
    client.println("Connection: keep-alive");
    client.println("Content-Type:   text/plain; charset=UTF-8");
    client.println("Content-Length: 12");
    client.println("\"on\":false");
    }
  else
  {
    Serial.println("Connection Failed.");  
   Serial.println();
  }
 delay(5000); 
}

I've also tried it with:

    "Content-Type: application/x-www-form-urlencoded"

rather than UTF-8.

According to the API, turning a light on/off is supposed to just be a matter of sending a PUT request with {"on":true/false} to the Hue bridge.

1

There are 1 answers

1
Christiaan On BEST ANSWER

Try sending the json formatted like this:

client.println("{\"on\":false}");