سؤال

I have been struggling with this for hours. Was able to get the hue to change light color and turn off using curl (curl -v -X PUT -d '{"on":true, "bri":254}' http://192.168.x.x/api/newdeveloper/lights/3/state) and the browser interface, but when I try to make arduino do the same I get the error: invalid/missing parameters. Below is my code. I took a lot from looking at the headers from the browser interface which indeed worked(from browser):

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x83, 0x0B };

IPAddress server(74,125,232,128);  
IPAddress server(192,168,x,x);     

IPAddress ip(192,168,0,177);

EthernetClient client;

void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(2000);
Serial.println("connecting...");

if (client.connect(server, 80)) {
Serial.println("connected");
client.println("PUT /api/newdeveloper/lights/3/state HTTP/1.1");
client.println("Host: 192.168.x.x");
client.println("Connection: keep-alive");
client.println("Content-Length: 45");
client.println("Origin: http://192.168.x.x");
client.println("User-Agent: arduino-ethernet");
client.println("Content-Type: text/plain;charset=UTF-8");
//client.println("Accept: */*");
client.println("Referer: http://192.168.x.x/debug/clip.html");
client.println("Accept-Encoding: gzip,deflate,sdch");
client.println("Accept-Language: en-US,en;q=0.8");
client.println("{\"on\":true, \"sat\":20, \"bri\":255,\"hue\":10000}");
//client.println();
} 
else {
Serial.println("connection failed");
}
}

void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
while(true);
}
}
هل كانت مفيدة؟

المحلول

I think you're missing a newline between the request headers and the body. Try something like this:

client.println("Accept-Language: en-US,en;q=0.8");
client.println();
client.println("{\"on\":true, \"sat\":20, \"bri\":255,\"hue\":10000}");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top