I am sending some data by post request to my webserver using ethernet shield and uno. But ethernet shield is connecting to the server but not sending the post request. Can anyone help me to do that?
here is my code
#include <Ethernet.h>
#include <SPI.h>
//byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
String data;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
else{
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
h = 7;
t = 6;
data = "";
// if (client.connect("www.fibrous-health.000webhostapp.com",80)) { // REPLACE WITH YOUR SERVER ADDRESS
// Serial.print("connected to ");
// Serial.println(client.remoteIP());
// }
// else{
// Serial.print("Not Connected");
// }
}
char server[] = "www.fibrous-health.000webhostapp.com";
void loop(){
data = String("temp1=") + t + "&hum1=" + h;
if (client.connect("www.fibrous-health.000webhostapp.com",80)) {
Serial.print("connected \n");
// Make a HTTP request:
Serial.print("Making a HTTP request \n");
client.println("POST /add.php HTTP/1.1");
client.println("Host: www.fibrous-health.000webhostapp.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println("User-Agent: Arduino/1.0");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
client.println();
Serial.print(data+"\n");
Serial.print("Data sent... \n");
}
else{
Serial.print("Not Connected \n");
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
delay(3000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
and here is my add.php file code
<?php
include 'connect.php';
$link=Connection();
$temp1=$_POST["temp1"];
$hum1=$_POST["hum1"];
$sql = "INSERT INTO `templog` (`temperature`, `humidity`)
VALUES ('".$temp1."','".$hum1."')";
if (mysqli_query($link, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($link);
}
mysqli_close($link);
//header("Location: index.php");
?>
when i send the post request from a different source likehttps://www.codepunker.com/tools/http-requests it worked but from arduino it did not. any kind of help will appreciated