Project

General

Profile

ENC28J60 chip - communicate with a server (http POST and GET)

Added by ruij9 over 5 years ago

Hello,

I’m a beginner in arduinos.

I’m doing some tests with the library EtherCard for ENC28J60 chip.

Need to communicate with a server through Posts and Gets.
I need to send some data in communication (json).

Saw the documentation and I’m not able to understand the is to use the Stash :: prepare. How and when should I use Stash :: prepare?

Anybody have some example code with http POST and another example with
http GET?

thanks, best regards,
Rui Jorge


Replies (8)

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by padvinder95 over 5 years ago

I think the webClient example (in the ethercard libary’s examples section) is a good GET example: https://github.com/jcw/ethercard/blob/master/examples/webClient/webClient.ino

[edit] And there is a PUT example, which is close to a POST, in the Pachube example: https://github.com/jcw/ethercard/blob/master/examples/pachube/pachube.ino

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by ruij9 over 5 years ago

Hello,

thank you padvinder95!

now i already connect to my server with GET.

but i cant do a post.. :(
i do something like this (bottom), and the request never run the page php.

byte sd = stash.create();
stash.print("apikey='abc'");
stash.print("&json='def'");
stash.save();
Stash::prepare(PSTR("POST http://$F/postValues.php HTTP/1.1" "\r\n"
  "Host: $F" "\r\n"
  "Content-Length: $D" "\r\n"
  "\r\n"
  "$H"),
  website, website, stash.size(), sd);
ether.tcpSend();

is something wrong?

best regards,
RUi Jorge

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by padvinder95 over 5 years ago

Hard to tell without the complete sketch, although this part looks OK to me.

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by ruij9 over 5 years ago

hello,

i really don’t have must more code then that.
the post http request is send, but on the server side (php) i can’t get the data :(

..but i change de POST to PUT ant it works just fine.

PUT:

Stash::prepare(PSTR("PUT http://$F/$F HTTP/1.0" "\r\n"
                        "Host: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, PSTR(FEED), website, stash.size(), sd);

SERVER SIDE:

$link=mysql_connect ...
mysql_select_db ...

if($_SERVER['REQUEST_METHOD'] == 'PUT') {
    parse_str(file_get_contents("php://input"),$post_vars);
    $sql = "INSERT INTO table (field) VALUES ('data: ".$post_vars['data']."')"; 
            $result = mysql_query($sql, $link);

}

thanks, best regards,
Rui Jorge

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by padvinder95 over 5 years ago

I take it that you also change your php script to check for $_SERVER['REQUEST_METHOD'] == 'POST' instead of 'PUT'? Also, I’m not sure whether POST data is available in the input stream—have you tried using POST and then simply accessing $_POST['data']?

Apart from that, your SQL is insecure: if I were to send a PUT request to your script containing something like '); DROP TABLE table; -- it could destroy your database. Better use mysql_real_escape_string on $post_vars['data'].

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by ruij9 over 5 years ago

hello,

“I take it that you also change your php script to check for \$_SERVER[‘REQUEST_METHOD’] == ‘POST’ instead of ‘PUT’? Also, I’m not sure whether POST data is available in the input stream—have you tried using POST and then simply accessing \$_POST[‘data’]?”

i don’t use “\$_SERVER[‘REQUEST_METHOD’]”, just simply access \$_POST[‘data’].

“Apart from that, your SQL is insecure: if I were to send a PUT request to your script containing something like ‘); DROP TABLE table; — it could destroy your database. Better use mysql_real_escape_string on \$post_vars[’data’].”

yes, you are right! but this is just a example code :)
but thanks for the advise!

Rui Jorge

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by padvinder95 over 5 years ago

ruij9 wrote:
> hello,
>
> i don’t use “\$_SERVER[‘REQUEST_METHOD’]”, just simply access \$_POST[‘data’].
>
>

So why are you posting code you don’t use? It’s hard to spot problems then..

RE: ENC28J60 chip - communicate with a server (http POST and GET) - Added by ruij9 over 5 years ago

hello,

“So why are you posting code you don’t use? It’s hard to spot problems then..”

all code that i post is in use.

i’m just telling that i don’t use “\$_SERVER[‘REQUEST_METHOD’]” when i’m trying to get data with post.
i just access \$_POST[‘data’].

SERVER SIDE (with POST):

$link=mysql_connect ...
mysql_select_db ...

$sql = "INSERT INTO table (field) VALUES ('data: ".$_POST['data']."')"; 
   $result = mysql_query($sql, $link);

..but when i’m doing PUT i’m using “\$_SERVER[‘REQUEST_METHOD’]” and “parse_str(file_get_contents(”php://input“),\$post_vars);”

best regards,
Rui Jorge

    (1-8/8)