【问题标题】:Sending a PUT request with an arduino使用 arduino 发送 PUT 请求
【发布时间】:2014-01-15 13:27:30
【问题描述】:

我正在尝试通过 PUT 从 arduino 发送 JSON 字符串,以控制 philips 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 请求。

【问题讨论】:

  • PUT 请求没有发送吗?响应内容是什么?
  • 我知道发送到Hue桥地址是成功的。不幸的是,我不知道如何在 arduino 上读取和解析响应 JSON。根据 API,打开/关闭灯应该只是向 Hue 桥发送一个带有 {"on":true/false} 的 PUT 请求。
  • @nonlinearmind 你能把这些信息添加到你的帖子中吗?
  • 尝试将数据包发送到您的 PC 并使用 wireshark 捕获内容。您有要比较的数据包吗?

标签: arduino put philips-hue


【解决方案1】:

尝试发送格式如下的 json:

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

【讨论】:

  • 谢谢!就是这样。不敢相信我错过了。
猜你喜欢
  • 1970-01-01
  • 2022-12-25
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多