3
votes

I'm trying to send an HTTP POST to my Arduino with an Ethernet shield from my server with cURL, but it doesn't work. If I send POST values through an HTML form or by Mac OS X's terminal cURL, the Arduino receive the data, so I asume the Arduino code is OK.

This is my cURL function:

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";
    }
}

I get two responses:

cURL Error (28): connect() timed out!

and

cURL Error (7): couldn't connect to host

If CURLOPT_CONNECTTIMEOUT is not declared, it returns nothing.

What is wrong with my code?

1
trying pinging the destination server from your server and see if it is accessible.air4x
In urlencode($blue), what is $blue? I don't see it defined in your function.HellaMad
It's defined by extract($what) command.Salsoni
What is the sketch that you are running in the Arduino side?Sudar

1 Answers

0
votes

Do not add 'http://' before your host ip:

$url = $arduinoip;

Try to increase your time limit:

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