1

我正在尝试通过 PUT 从 arduino 发送 JSON 字符串,以控制飞利浦 Hue 智能灯。我在谷歌上搜索了很多关于 POST 和 GET 的信息,但在 PUT 上却不多。我正在尝试将“{"on":false}" 放入我的本地 Hue 桥 (/api/[key]/lights/3/state),但现在不知道如何格式化它。任何人都可以帮忙吗?

这是我使用 Hue 的调试工具成功发送请求时的控制台信息:

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

这是我一直在尝试的,但没有成功:

     #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); 
}

我也尝试过:

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

而不是 UTF-8。

根据 API,打开/关闭灯应该只是向 Hue 桥发送一个带有 {"on":true/false} 的 PUT 请求。

4

1 回答 1

3

尝试发送格式如下的 json:

client.println("{\"on\":false}");
于 2013-12-28T09:08:57.567 回答