Go Down

Topic: Ethernet shield not sending the POST request to webserver  (Read 1 time) previous topic - next topic

hussy

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

Code: [Select]
#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
Code: [Select]
<?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 "<br>" 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

wildbill

Look at the ethernet client example - it ends with two println statements which, IIRC, is important.

Also, what do your server logs say?

notrino

You have to use this library;
"#include <Ethernet2.h>"

AshwiniGunjal

Had you test your API through Rest Client? if it give proper response to your request then there is no error on your server side code.

you should review your arduino code.
I think problem in your json formate.



AshwiniGunjal

you data variable is :

data = String("temp1=") + t + "&hum1=" + h;

here t & h have int datatype.

you should change it into

data = "temp1=" + (String)t + "&hum1=" + (String)h;

now this is proper string. you can try this.

Go Up