Go Down

Topic: Sending Data to a Mysql WebServeur using POST method (Read 9 times) previous topic - next topic

royalldonkey

Hi everyone,

I would need so help. I am trying to send data to a php page using an arduino with Ethernet Shield.
I manage to send data using the GET method but it is not a safe way. So I am now trying the POST method but I have no idea why it won't work.

Here is the arduino code.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

long randNumber;
//Affectation d'une adresse MAC
byte mac[] = {  0xB8, 0x27, 0xEB, 0xFE, 0xB2, 0x49 };

// Serveur avec Mysql
IPAddress server(192,168,0,100);

bool connected = false;

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup()
{
Serial.begin(9600);

        randomSeed(analogRead(0));

if (Ethernet.begin(mac) == 0)
{
Serial.println("Impossible de configurer Ethernet en utilisant DHCP"); 
    // Attente perp├ętuelle
    for(;;);
        }
    Serial.println("Configuration Ethernet DHCP : OK \n@IP : ");
      for (byte thisByte = 0; thisByte < 4; thisByte++)
      {
// print the value of each byte of the IP address:
            Serial.print(Ethernet.localIP()[thisByte], DEC);
            Serial.println(".");
       }
    delay(1000);
}

int val=0;

void loop()
{
int val = random(300);
        String DatatoSend ="";
        DatatoSend += "Data=";
        DatatoSend += (val);
       
        Serial.println("DatatoSend :" + DatatoSend );
       
     
       
if (client.connect(server, 80))
{
    Serial.println("\nconnected...");
    Serial.println("ARDUINO: forming HTTP request message");
    client.println("POST /Test/PhpPost.php HTTP/1.1");
    client.println("From: Arduino1 ");
    client.println("User-Agent: HTTPTool/1.0");
    client.println("Content-Type: application/x-www-form-urlencoded");
                client.print("Content-Length:");
                client.println(DatatoSend.length());
                client.println("Connection: close");
                client.println(DatatoSend);
                client.println();

    Serial.println("ARDUINO: HTTP message sent");
    delay(3000);
    if(client.available())
    {
    Serial.println("ARDUINO: HTTP message received");
    Serial.println("ARDUINO: printing received headers and script response...\n");
   
    while(client.available())
    {
    char c = client.read();
    Serial.print(c);
    }
    }
    else
    {
    Serial.println("ARDUINO: no response received / no response received in time");
    }
   
    client.stop();
    }
    else
    {
    Serial.println("connection failure");
    }
    delay(2000);

}



and the Php Page :

Code: [Select]
<?php 
  $Variable 
$_POST['Data'];
  echo 
$Variable ;
?>


Thanks in advance for your Help  ;)

billroy

When you say it won't work, what do you mean exactly?

The place to start debugging is the access and error logs for the web server that is serving the PHP page.

The URL in the code uses mixed upper and lower case.  Not all file systems support this correctly, and for this reason URLs are usually not mixed case.  This is one possible cause of bugs like the one you are seeing. 

But the (presumably Apache) error log should be your next stop.

-br

royalldonkey

Thanks you for this 1st answers. I will try what you say tomorrow and I will post the log of the server.

TrailerTrash

Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

iDroid

$_POST is used to get values from a HTML form element with a name that corresponds to the value inside the square brackets.

For example, this:
Code: [Select]
<?php 
  $Variable 
$_POST['Data'];
  echo 
$Variable ;
?>


Is now looking for a HTML element that has the name "Data", which doesn't exist.
Try writing your data into a separate text file from the arduino (remember to chmod the file) and then do something like;

Code: [Select]
<? php
$var = include 'youfilenamehere.txt';
echo $var;
?>

Go Up