【问题标题】:Send an HTTP POST to Arduino with cURL使用 cURL 向 Arduino 发送 HTTP POST
【发布时间】:2012-09-28 14:00:18
【问题描述】:

我正在尝试使用cURL 从我的服务器发送一个带有以太网屏蔽的 HTTP POST 我的 Arduino,但它不起作用。如果我通过 HTML 表单或通过 Mac OS X 的终端 cURL 发送 POST 值,Arduino 会收到数据,所以我认为 Arduino 代码是可以的。

这是我的 cURL 函数:

function curler($what,$arduinoip){
    $url = "http://".$arduinoip;
    extract($what);
    $fields = array(
         'blue' => urlencode($blue)
    );
    foreach($fields as $key=>$value){$fields_string .= $key.'='.$value.'&';}
    $fields_strings = rtrim($fields_string, '&');
    // hasta aca todo bien
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_strings);
    //execute post
    $result = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    curl_close($ch);
    if ($curl_errno > 0) {
        echo "cURL Error ($curl_errno): $curl_error\n";
    } else {
        echo "Data received: $result\n";
    }
}

我得到两个回应:

cURL 错误 (28):connect() 超时!

cURL 错误 (7):无法连接到主机

如果未声明 CURLOPT_CONNECTTIMEOUT,则不返回任何内容。

我的代码有什么问题?

【问题讨论】:

  • 尝试从your server ping destination server 看看它是否可以访问。
  • urlencode($blue) 中,$blue 是什么?我没有在你的函数中看到它。
  • 由 extract($what) 命令定义。
  • 你在 Arduino 端运行的草图是什么?

标签: php http curl arduino


【解决方案1】:

不要在主机 ip 前添加“http://”:

$url = $arduinoip;

尝试增加你的时间限制:

//200+ maybe?    
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 250);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2017-01-23
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    相关资源
    最近更新 更多