Go Down

Topic: [SOLVED] Problem with sending POST data (Read 23231 times) previous topic - next topic

rufek

Sep 28, 2013, 03:16 pm Last Edit: Sep 28, 2013, 04:56 pm by rufek Reason: 1
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 :( - 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: [Select]
#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: [Select]

<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>




PaulS

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.

rufek

Thanks for reply. Networking will be good :)

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?

PaulS

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.

rufek

#4
Sep 28, 2013, 03:50 pm Last Edit: Sep 28, 2013, 03:56 pm by rufek Reason: 1
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: [Select]
[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

PaulS

Quote
I have posted all of my code

No, you haven't. Until you do, this is my last reply.


rufek

#6
Sep 28, 2013, 04:07 pm Last Edit: Sep 28, 2013, 04:13 pm by rufek Reason: 1
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

rufek

#7
Sep 28, 2013, 04:39 pm Last Edit: Sep 28, 2013, 04:48 pm by rufek Reason: 1
Solved!

Post message must contain 'Content-Type'

Working code for arduino:

Code: [Select]
#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();
 }
}

PaulS

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.

kimjessen

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.

Hi,

I have been struggling now for two day's to pass data to my website using Microsoft, I see you got it right I have tried everything, but no luck.  Could you help me?

Hi again,

If I go and type this in the browser www.manski.co.za/default.aspx?id=123 then it works and it inserts the 123.  If you see anything wrong with my code please give advise.

Thank you

#include <EtherCard.h>

// your variable

#define PATH    "default.aspx?"
#define VARIABLE    "testing123"

// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

char website[] PROGMEM = "www.manski.co.za";

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

void setup () {
  Serial.begin(9600);
  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");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip); 
  ether.printIp("DNS: ", ether.dnsip); 

  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("?id=");
    stash.print(VARIABLE);
    stash.print("&action=Submit");
    stash.save();

    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("POST http://www.manski.co.za/default.aspx" "\r\n"
                        "Host: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, PSTR(PATH), website, stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
  }
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy