I'm trying to send arduino data to my online web server using a Arduino Uno and a Ethernet shield. I used POST method for sending data and tried sending data to my sever (add.php) using a online POST request generating site (https://www.codepunker.com/tools/http-requests) . That works perfect and it updated my database with sent data. But my database doesn't get any data when I try to send data with Arduino and Ethernet shield. Any suggestions to make this work? What changes should I do to my arduino code?

Database enter image description here

Serial Out

Initialize Ethernet with DHCP:
DHCP assigned IP
connected to
still working here
Client Stopped

Arduino Code

  #include <SPI.h>
  #include <Ethernet.h>

  byte mac[] = { 0x00, 0xAB, 0xBA, 0xBC, 0xDD, 0x02 };
  EthernetClient client;
  String data;
  int t;
  int h;
  IPAddress ip(192, 168, 1, 177);
  IPAddress myDns(192, 168, 1, 1);
  char server[] = "lattalottaroom2.000webhostapp.com";

  void setup() {
    Serial.println("Initialize Ethernet with DHCP:");
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // Check for Ethernet hardware present
      if (Ethernet.hardwareStatus() == EthernetNoHardware) {
        Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
        while (true) {
          delay(1); // do nothing, no point running without Ethernet hardware
      if (Ethernet.linkStatus() == LinkOFF) {
        Serial.println("Ethernet cable is not connected.");
      // try to congifure using IP address instead of DHCP:
      Ethernet.begin(mac, ip, myDns);
    } else {
      Serial.print("  DHCP assigned IP ");

    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      Serial.print("connected to ");
    } else {
      // if you didn't get a connection to the server:
      Serial.println("connection failed");

    Serial.println("still working here");


  void loop() {
  t = 10;
  h = 24;

  data = "temp1=";




    if (client.connect("lattalottaroom2.000webhostapp.com",80)) {
      client.print("POST /add.php HTTP/1.1");
      client.print("Host: lattalottaroom2.000webhostapp.com");
      client.print("Connection: close");
      client.print("Content-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: ");


    if (client.connected()) {
      Serial.println("Client Stopped");





        function Connection(){

            $connection = mysqli_connect($server,$user,$pass,$db);

            if (!$connection) {
                die('MySQL ERROR: ' . mysqli_connect_error());

            return $connection;




      $query = "INSERT INTO `tempLog` (temp, hum) VALUES ('".$_POST["temp1"]."','"$_POST["hum1"]."')"; 

              if (mysqli_query($link, $query)) {
                 echo "New record created successfully";
              } else {
                 echo "Error: " . $query . "" . mysqli_error($link);


      header("Location: index.php");
  • All your headers are on one long line. You need to split them. Use println instead of print when your header is a complete string. – Majenko Aug 29 '19 at 11:53
  • Thanks for your quick response Majenko . I changed those to println and still doesn't get any data to database. – Chathuranga Lakmal Aug 29 '19 at 12:03
  • What do you see when you echo the $query variable? – VE7JRO Aug 29 '19 at 16:49
  • If I directly feed the data using the url like https://lattalottaroom2.000webhostapp.com/add.php?temp1=40&hum1=50 it echo INSERT INTO tempLog (temp, hum) VALUES ('40', '50') and updates the DB. However after I changed the arduino code as follows, I was able to upload data to local database with add.php using my arduino and ethernet shield. client.print("GET /arduino/add.php?"); client.print("temp1="); client.print(t); client.print("&hum1="); client.println(h); – Chathuranga Lakmal Aug 30 '19 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.