Pages: [1]   Go Down
Author Topic: [SOLVED] Problem with sending POST data  (Read 5665 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,
I don't know is 'storage' the best section for my topic.

I have simple problem:
I had wrote simple php page which inserts data from POST to database.

I know, that my script is working, because I tested it on http://www.requestmaker.com/  I can send data to save into database. Program to send data on arduino does not work properly. I think, i forgot something...
I know, that POST request fires, because in database i have new records, but they are empty smiley-sad - instead of POST messages sended by http://www.requestmaker.com/  

I'm using ENC 28J60 and arduino uno (but i think, that is does not matter)

Can anyone help?


Arduino code:


Code:
#include <EtherCard.h>
#define PATH    "temp.php"

byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char website[] PROGMEM = "www.iskrzycki.hekko.pl";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");

  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    timer = millis() + 10000;

    byte sd = stash.create();
    stash.print("t0=22");
    stash.save();

            
            Stash::prepare(PSTR("POST http://www.iskrzycki.hekko.pl/temp.php HTTP/1.0" "\r\n"
                        "Host: www.iskrzycki.hekko.pl \r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            stash.size(), sd);
    ether.tcpSend();
  }
}





PHP CODE:
Code:
<html>
<body>
<?php
$link 
mysql_connect("HOST""LOGIN""PASS") or die("Keine Verbindung möglich: " mysql_error());
mysql_select_db("DATABASE") or die("Auswahl der Datenbank fehlgeschlagen");
$query "INSERT INTO table1(temp, data) VALUES ('" $_POST['t0'] . "', now())";$result mysql_query($query) or die("Anfrage fehlgeschlagen: " mysql_error());
mysql_close($link);
?>

</body>
</html>



* post.ino (1.08 KB - downloaded 13 times.)
* phpInsert.php (0.41 KB - downloaded 10 times.)

* EmptyDB.jpg (56.94 KB, 205x363 - viewed 26 times.)
« Last Edit: September 28, 2013, 09:56:52 am by rufek » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't know is 'storage' the best section for my topic.
Networking would have been the right place. I'll ask a moderator to move it.

You need to post ALL of your code (and describe your hardware; it is clear you are not using a WizNet based ethernet shield). Nothing in that snippet actually sends any data after the POST request.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for reply. Networking will be good smiley

This is all of my code.

POST is sended by line:      ether.tcpSend();
And I know, that POSTS are sended, because in my database appears blank records - but why empty?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but why empty?
Without seeing all of your code, I don't know. I would expect that somehow ether.tcpSend() needs to know what to send.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have posted all of my code (only without HTML tags in php function).

As you see, I'm using EtherCard library. In EtherCard.h I have found:   static uint8_t tcpSend ();    - so i don't pass any parameter to this function.

I think, you are thinking about TCPSend function in wizznet, but I have enc28j60.

If I am wrong, please correct me.

Edit: I found this snippet and it is correct - so ether.tcpSend(); is a good call

Code:
[setup works fine]
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (millis() > timer) {

Stash::prepare(PSTR("MessageB" "\r\n"));
ether.tcpSend();
}}

So I think, that problem is in my POST message - maybe are some quotation marks missing or something like that... But no idea, where
« Last Edit: September 28, 2013, 08:56:07 am by rufek » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have posted all of my code
No, you haven't. Until you do, this is my last reply.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I modified my php code - HTML tags added. Also I attached arduino project and php file to first post. I can't show more, because this is all of my code - If any instruction is missing, you can tell me about it.

Edit: Database 'select' screenshot also added to first post
« Last Edit: September 28, 2013, 09:13:05 am by rufek » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Solved!

Post message must contain 'Content-Type'

Working code for arduino:

Code:
#include <EtherCard.h>
#define PATH    "temp.php"

byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char website[] PROGMEM = "www.iskrzycki.hekko.pl";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");

  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    timer = millis() + 10000;

    byte sd = stash.create();
    stash.print("t0=22");
    stash.save();

              Stash::prepare(PSTR("POST http://www.iskrzycki.hekko.pl/temp.php HTTP/1.0" "\r\n"
                        "Host: www.iskrzycki.hekko.pl \r\n"
                        "Content-Length: $D" "\r\n"
                        "Content-Type: application/x-www-form-urlencoded \r\n"
                        "\r\n"
                        "$H"),
            stash.size(), sd);
            
    ether.tcpSend();
  }
}
« Last Edit: September 28, 2013, 09:48:58 am by rufek » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Resolved !
Excellent. I've never used POST. What you are doing could be done with a simple GET, and would, in my opinion, be simpler. Yes, GET requests are easier to sniff, but how the temperature somewhere could be considered sensitive, I do not know.

You could add [SOLVED] to the original thread title.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey sorry I start this up again.
but a little question.
if you need to send a sprcifik port, such as port 8004 => xxxxxxx.dd: 8004
how would you do this.
Logged

Pages: [1]   Go Up
Jump to:  

Powered by SMF 1.1.19 | SMF © 2013, Simple Machines