Go Down

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

rockwallaby

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

This is partly true, in that what you say is a method often used, but it is not the definition of the POST method.

Using the POST method, you simply place the data, not on the url, but within the body of the http request, whether that be a html form, or for example, json encoded data from an external server/client system like an Arduino. And as such it is possible to send what ever data you have from the Arduino to a server using this method. Using the POST method also allows you to send more data than the GET method.

Quote
royalldonkey wrote:
I manage to send data using the GET method but it is not a safe way

How is using the GET method an issue with safety when using an Arduino?
I can understand the safety issues using the GET method from a html request from a browser on a computer, but I am not sure that you have this issue when sending data using the GET method from the Arduino?
How could the data be intercepted and viewed if it is sent from the Arduino?
If there is any chance of data snooping, the POST method I believe offers no extra protection, as the data is simply shifted to a different location in the http request.

Why do you have the following?
Code: [Select]
randomSeed(analogRead(0));

Do you know that the Arduino is setting up the IP configuration correctly using DHCP according to your program?
The Arduino will display this on the serial console at startup.

royalldonkey, have you had any success yet, let us know?

Santé
Paul

rockwallaby

#6
Feb 02, 2013, 07:31 am Last Edit: Feb 02, 2013, 07:55 am by rockwallaby Reason: 1
Bonjour royalldonkey,

I have taken a section of my Arduino code out that deals with the transfer of data from my Arduino to my SQL database on my hosted site.
I use the Eclipse IDE, and make use of headers files, so file.cpp and header.h
The code below is part of an include file I use.

File sqlData.cpp
Code: [Select]

#include    "sqlData.h"                                 // include my own header file oui!
#include    "Ethernet.h"
#include    "Local_files/sensor.h"                      // header file to link to my data in other areas of Arduino:
#include    "Local_files/pidControl.h"                  // same as above:

       uint16_t    okCountPOST         = 0;            // number of successful POST connects:
       uint16_t    fltCountPOST        = 0;            // number of unsuccessful POST connects:
       boolean     fatcowConnected     = false;

       EthernetClient    clientFatCow;

//-----------------------------------------
//    Prepare POST data to SQL table live
//
//
void postLive() {
   char postData[128];

//    reference vars and scaling of integer data below
//    vB_pv        = vdcBattery.pv / 100
//    vT_pv        = vacHydro.pv / 10
//    iT_pv        = idcHydro.pv / 50
//    iS_pv        = idcSolar.pv / 50
//    fT_pv        = frqHydro_pv / 100
//    vT_cvPID     = hydroPID.cvPID / 10.23
//    vS_cvPID     = solarPID.cvPID / 10.23

   sprintf(postData, "{\"id\":\"live\",\"vB_pv\":%1i,\"vT_pv\":%2i,\"iT_pv\":%3i,\"iS_pv\":%4i,\"fT_pv\":%5i,\"vT_cvPID\":%6i,\"vS_CVPID\":%7i}",
           vdcBattery.pv, vacHydro.pv, idcHydro.pv, idcSolar.pv, frqHydro_pv ,hydroPID.cvPID, solarPID.cvPID);

   if(!postPage(postData)) {
       Serial.println("Error Post live");
       ++fltCountPOST;
   }
   else {
//        Serial.println("Post live");
   }
   ++okCountPOST;
}



//-----------------------------------------
//    POST data to server host
//
//
bool postPage(char* thisData) {
   int inChar;
   char outBuf[32];

//    Serial.print("postData: ");
//    Serial.print(thisData);
//    Serial.print("Len: ");
//    Serial.println(strlen(thisData));

   if (!fatcowConnected) {                       // if not connected then connect:
       fatcowConnect();
   }

   if (fatcowConnected) {
//    if(clientFatCow.connect("www.paulalting.com", 80)) {               // www.mydomain.com:
//        sprintf(outBuf,"POST %s HTTP/1.1",page);                       // DEBUG print:
       clientFatCow.println("POST /yourphp.php HTTP/1.1");              // send the header:
//        sprintf(outBuf,"Host: %s",domainBuffer);                       // DEBUG print:
       clientFatCow.println("Host: www.paulalting.com");
       clientFatCow.println("Connection: close\r\nContent-Type: application/json");
       sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));       // DEBUG print:
       clientFatCow.print(thisData);                                    // send the data:
   }
   else {
       Serial.println("Heck, were're not connected!");                  // what gives here:
       return false;
   }

   int connectLoop = 0;
   while(clientFatCow.connected()) {
       while(clientFatCow.available()) {
           inChar = clientFatCow.read();
           Serial.write(inChar);                         // serial print reply from host:
           connectLoop = 0;
       }

       delay(1);
       connectLoop++;
       if(connectLoop > 2500) {                          // wait up to 2500mSec for reply:
//            Serial.println();
           Serial.println("Timeout");
           clientFatCow.stop();
           fatcowConnected = false;
       }
   }

   Serial.print("Post Count : ");
   Serial.print(okCountPOST);
   Serial.print("  Fails : ");
   Serial.println(fltCountPOST);
   Serial.println();
//    Serial.println("disconnecting");
   clientFatCow.stop();
   fatcowConnected = false;
   return true;
}

//-----------------------------------------
//    Connect to Server as Client
//
//
void fatcowConnect() {
    uint16_t    retry = 3;                                // retry count, normally 3:
//    Serial.println("Connecting..");
    while (retry) {
        if (clientFatCow.connect("www.paulalting.com", 80)) {
            Serial.print("Connect to host ok, retry: ");
            Serial.println(retry);
            fatcowConnected = true;
            break;
        }
        else {
            Serial.print("Connect to host failed, retry: ");
            Serial.println(retry);
            fatcowConnected = false;
        }
//        delay(10);                                      // delay before retry:
        --retry;
    }
}

//-----------------------------------------
//    Disconnect from Server as Client
//
//
void fatcowClose() {
    if (clientFatCow) {
        clientFatCow.stop();
        fatcowConnected = false;
    }
}



What the routines do;

void postLive(), simply pre-formats some real-time data into json formatted style. You can do this a different way as you please. You just need the php script on your server to understand your format. It then calls the main posting routine below, if successful, it increments a success counter else increments a un-successful counter which I use for debug stats.
I have other functions for other data.

bool postPage(char* thisData), does the hard work of actually using the POST method to send data to my host, where a php script pulls out the data and puts it into a SQL database.

void fatcowConnect(), makes a connection avec retries.
void fatcowClose(), closes the connection.

Be sure not to mix http_1.0 and http_1.1 requests together, as I see you do in your code section.

Then, from your main routine, all you need to do is set up a time based trigger that will call postLive(); and it all happens after that.
If you want some php with SQL code, let me know.

Hope this helps you out.

A bientôt,
Paul

H2SO4

Hi Rockwallaby,

It would be great if you could also post some parts of your PHP listener.

Thanks

iDroid


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

This is partly true, in that what you say is a method often used, but it is not the definition of the POST method.

Using the POST method, you simply place the data, not on the url, but within the body of the http request, whether that be a html form, or for example, json encoded data from an external server/client system like an Arduino. And as such it is possible to send what ever data you have from the Arduino to a server using this method. Using the POST method also allows you to send more data than the GET method.



Oh, never knew that. Thanks.
If that isn't the problem... Then what is?

rockwallaby

Quote
iDroid wrote:
Oh, never knew that. Thanks.
If that isn't the problem... Then what is?


No problems, even I learn new things each day, much from other users here on the forum and from way too much googling  :smiley-eek-blue:

The other parts of the code royalldonkey has put together will be the problem.
His 'Type Content', mixing of http versions, and also I wonder about the following;
Code: [Select]
client.println(DatatoSend.length());
I am unsure if that will work, it looks like nice oop style, but I think you need to use the length function.
Also, the use of the 'String' data type is discouraged due to some issues, use a char array instead.

The code I have placed above is in my working Arduino and POST's to my sql database each 60 seconds.
The code I used is based on code developed by SurferTim.

@H2SO4
I will post a section of php with sql code up here shortly, just having first morning coffee  :smiley-sleep:

Regards,
Paul

Go Up