[SOLVED] Problem with sending POST data

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

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

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

phpInsert.php (418 Bytes)

EmptyDB.jpg

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.

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?

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.

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

[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

I have posted all of my code

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

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

Solved!

Post message must contain 'Content-Type’

Working code for arduino:

#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();
  }
}

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.

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();
}
}[/table]

Hi All, this code works fine to me but is using DHCP

#include <EtherCard.h>

#define REQUEST_RATE 5000 // milliseconds

// remote name
const char website PROGMEM = “80.253.138.54”;

//ether.dnsLookup(HOST); // okay

byte Ethernet::buffer[700]; // a very small tcp/ip buffer is enough here
static long timer;

// called when the client request is complete
/static void my_result_cb (byte status, word off, word len) {
Serial.print("<<< reply “);
Serial.print(millis() - timer);
Serial.println(” ms");
Serial.println((const char
) Ethernet::buffer + off);
}*/

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

// Change ‘SS’ to your Slave Select pin, if you arn’t using the default pin
if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
Serial.println(F(“Failed to access Ethernet controller”));
if (!ether.dhcpSetup())
Serial.println(F(“DHCP failed”));

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

#if 1
// use DNS to locate the IP address we want to ping
//if (!ether.dnsLookup(PSTR(“www.google.com”)))
if (!ether.dnsLookup(PSTR(“80.253.138.54”)))
Serial.println(“DNS failed”);
else
Serial.println(“DNS OK.”);
#else
ether.parseIp(ether.hisip, “74.125.77.99”);
#endif
ether.printIp("SRV: ", ether.hisip);

// call this to report others pinging us
// ether.registerPingCallback(gotPinged);

timer = - REQUEST_RATE;
Serial.println();

ether.printIp("–>SRV: ", ether.hisip);

}

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

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

ether.hisport = 3003; //to access the server port
Serial.println("\n>>> GET");
char gets[50]; // string to be sent to the server, you can access it i.e. $_GET in PHP …
sprintf(gets,“h=%s&t=%s&c=%d”,“10”,“20”,30); // prepare the GET, all variables in one string
Serial.println(gets);
//Serial.println(website);
ether.browseUrl(PSTR("/data?"), gets, website, my_result_cb); // send it to the server

}
}

// called when the client request is complete
static void my_result_cb (byte status, word off, word len)
{
// JSON Body of my post request
Serial.println((const char*) Ethernet::buffer + off);
}