Sending HTTP POST request from arduino yun

I am doing a project where i have to upload sensor data to Mysql from arduino yun. I tried using python and php but have been unsuccessful in both attempts. In the php method i tried to use a POST request:

#include <Bridge.h>

#include <YunClient.h>

#include <SPI.h>

IPAddress server(192,168,43,254);

YunClient client;

int sensor1 = 64;
int sensor2 = 65;

void setup() {

// Serial.begin starts the serial connection between computer and Arduino
Serial.begin(9600);
Bridge.begin();
client.connect(server,80);

if (client.connect(server,80))
{
Serial.println("connected");
client.println("POST /insert.php HTTP/1.1");
client.println( "Connection: close" );
client.println( "Host:192.168.43.254" );
client.println( "Content-Type: application/x-www-form-urlencoded" );

}
else
{
Serial.println("connection failed");
}

}

void loop() { if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;:wink:
;
}
}

but they have me an error of request timeout :

HTTP/1.1 408 Request Timeout
Date: Mon, 24 Apr 2017 07:59:16 GMT
Server: Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1
Content-Length: 320
Connection: close
Content-Type: text/html; charset=iso-8859-1

408 Request Timeout

Request Timeout

Server timeout waiting for the HTTP request from the client.


Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1 Server at localhost Port 80

disconnecting.

any suggestions on how to send the GET request and upload the data to mysql database ? Preferably using python and php, and also i dont want to use an ethernet cable or wifi shields only bridge. Thank you :slight_smile:

Curiously, you have two connections to the server - while I don't think it's causing your problem, you should get rid of the first one. It's wasted effort to open the connection, only to close it and reopen it again in the next statement.

The timeout response from the server makes sense, it's waiting for the rest of the POST request. A properly formatted request starts with the verb/URL/HTTP version, which you have, along with several request header lines. But after the request headers, you need to send a blank line and then the request body, neither of which are there. The server is waiting for the rest of your request, and finally giving up after waiting too long.

What you have can be made to work, but it's a rather cumbersome way to do it. The Yun includes a powerful Linux system with many helpful utilities. Rather than implement all of the details of the HTTP protocol yourself, consider using a Process object to call the curl command on the Linux side. It will make things easier and handle a lot of the details for you.

ShapeShifter:
Curiously, you have two connections to the server - while I don't think it's causing your problem, you should get rid of the first one. It's wasted effort to open the connection, only to close it and reopen it again in the next statement.

The timeout response from the server makes sense, it's waiting for the rest of the POST request. A properly formatted request starts with the verb/URL/HTTP version, which you have, along with several request header lines. But after the request headers, you need to send a blank line and then the request body, neither of which are there. The server is waiting for the rest of your request, and finally giving up after waiting too long.

What you have can be made to work, but it's a rather cumbersome way to do it. The Yun includes a powerful Linux system with many helpful utilities. Rather than implement all of the details of the HTTP protocol yourself, consider using a Process object to call the curl command on the Linux side. It will make things easier and handle a lot of the details for you.

Could you please give an example of this? would want to see how a HTTP POST request looks like. Also i tried process class many times but nothing happened at all and i went through all the possible errors and found none

For an example of making a POST request with curl, follow the curl link above and scroll about halfway down the page.

As far as Process goes, please post the code that's giving you trouble, along with details of what's going wrong. There isn't much help we can give with such a vague description.

When posting code, please use code tags: either click the </> button above the post edit box and paste the code between the tags, or in the Arduino IDE, select your code and the use the File | Copy for Forum command to copy your code with the code tags automatically added, and paste that into the post.

SOLVED: i managed to get the POST request up and working, my advice is to be extremely careful with order and formatting. In case anyone here wants the code:

#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>

#include <SPI.h>
//char server[] = "192,168,43,254"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
IPAddress server(192,168,43,254);
//IPAddress ip(192,168,43,254);
YunClient client;

int sensor1 = 64;
int sensor2 = 65;
String sensorvalue1 = "";
String sensorvalue2 = "";
void setup() {

// Serial.begin starts the serial connection between computer and Arduino
Serial.begin(9600);
Bridge.begin();
String sensorvalue1 = "";
String sensorvalue2 = "";
client.connect(server,80);

}

void loop() {
if (client.connect(server,80))
{
Serial.println("connected");
sensorvalue1 ="sensor1="+int(sensor1);
sensorvalue2 ="sensor2="+int(sensor2);
client.println("POST /insert.php? HTTP/1.1");
// client.print("Content-length:");
// client.println(sensorvalue1.length());
Serial.println(sensorvalue1.length());
Serial.println(sensorvalue1);
// client.println(sensorvalue2.length());
// Serial.println(sensorvalue2.length());
// Serial.println(sensorvalue2);

client.println("Connection: close" );
client.println("Host:127.0.0.1" );//ur web server
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();
client.println(sensorvalue1);
// client.get("localhost/insert.php?sensor1=20&&sensor2=21");
}
else
{
Serial.println("connection failed");
}

if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;:wink:
;
delay(10000);
}
}

I tried to send the string to the web service, but my database did not receive the message,Where is the error?

#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>

#include <SPI.h>
//char server[] = "192,168,43,254"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
IPAddress server(120,101,8,52);
//IPAddress ip(192,168,43,254);
YunClient client;

String str="ABCDE";
void setup() {

// Serial.begin starts the serial connection between computer and Arduino
Serial.begin(9600);
Bridge.begin();

client.connect(server,80);

}

void loop() {
if (client.connect(server,80))
{
Serial.println("connected");
client.println("POST /2017farmer/WebService1.asmx/Z_Insert_Name HTTP/1.1");
client.print("Content-length: ");

client.println("Connection: close" );
client.println("Host:120.101.8.52");//ur web server
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();
client.println(str);

// client.get("localhost/insert.php?sensor1=20&&sensor2=21");
}
else
{
Serial.println("connection failed");
}

if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;:wink:
;

delay(10000);
}
}