在Arduino ESP8266中重现成功的卷曲请求

时间:2017-07-14 04:07:14

标签: curl arduino esp8266

我是ESP8266和Arduino IDE /代码的菜鸟。

我有一个红外传感器连接到ESP-07,并且具有在检测到运动时成功打开LED的代码。

我正在尝试向运行Webiopi的树莓派发送POST请求,以打开连接到RPi的中继。

我已成功在RPi上运行curl请求,该请求打开GPIO引脚#11并激活与该引脚相连的继电器: curl -v -X POST -u username:password http://192.168.2.10:8000/GPIO/11/value/0

服务器响应是:

    * About to connect() to 192.168.2.10 port 8000 (#0)
    *   Trying 192.168.2.10...
    * connected
    * Connected to 192.168.2.10 (192.168.2.10) port 8000 (#0)
    * Server auth using Basic with user 'webiopi'
    > POST /GPIO/11/value/1 HTTP/1.1
    > Authorization: Basic **a key was provided by the server**
    > User-Agent: curl/7.26.0
    > Host: 192.168.2.201:8000
    > Accept: */
    > 
    * additional stuff not fine transfer.c:1037: 0 0
    * HTTP 1.0, assume close after body
    < HTTP/1.0 200 OK
    < Server: WebIOPi/0.7.1/Python3.2
    < Date: Fri, 14 Jul 2017 01:27:05 GMT
    < Cache-Control: no-cache
    < Content-Type: text/plain
    < Content-Length: 1
    < 
    * Closing connection #0

我需要使用Arduino代码在我的ESP-07上复制这个卷曲请求。

我已经尝试过这段代码,但是当提交POST时,继电器没有打开,串行监视器窗口没有输出。

char server[] = "192.168.2.10"
if (client.connect(server, 8000)) {
  Serial.println("Connected to server");
  // Make a HTTP request
  client.println("Authorization: Basic **I put the key generated from the curl call here**");
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Content-Length: 35");
  client.println("username=username&password=password");
  client.println("User-Agent: curl/7.26.0");
  client.println("Host: 192.168.2.10:8000");
  client.println("POST /GPIO/11/value/0 HTTP/1.1");
  client.println("Accept: */*");
  client.println();
  delay(1000);
  Serial.println();
  Serial.println("disconnecting");
  Serial.println("==============");
  Serial.println();
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
    }
  client.stop(); //stop client
  }

如果这个请求过于神秘,我提前道歉,但这是我第一次在这样的论坛上发帖。

非常感谢提前......: - )

@Cagdas,@ Defozo,thx的回复...... 这是尝试过,但代码没有打开LED。

HTTPClient http;
http.begin("http://192.168.2.10:8000/");
http.addHeader("User-Agent", "curl/7.26.0"); 
http.setAuthorization("username", "password");
auto httpCode = http.POST("/GPIO/11/value/0");
http.end();

@cagdas所以我接下来试了一下,仍然没有LED开启

HTTPClient http;
http.begin("http://192.168.2.10:8000/");
http.addHeader("User-Agent", "curl/7.26.0"); 
http.POST("username=username&password=password");
http.POST("/GPIO/11/value/0");
http.end();

并且重申一下,这是我试图复制的卷曲电话:

curl -v -X POST -u username:password http://192.168.2.10:8000/GPIO/11/value/0

1 个答案:

答案 0 :(得分:1)

为了精益,您最好使用Esp8266的HTTP客户端。

HTTPClient http; 
http.begin("192.168.2.10:8000/");
http.addHeader("User-Agent: curl/7.26.0"); http.POST("username=username&password=password");
http.end();