Send data to MySQL (Arduino Uno, Eth shield W5100, IR obstacle sensor) [solved]

Hi. I need help with my project. I'm having trouble to send IR sensor data from serial monitor to MySQL database.

Here is my arduino code:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 192, 168, 0, 127 };
byte server[] = { 192, 168, 0, 121 };

//Initialize the Ethernet server library
EthernetClient client;

int LED = 13; // Use the onboard Uno LED
int isSensePin = 7;  // This is our input pin
int isSensePin2 = 8;
int isSensePin3 = 9;
int sensor1 = HIGH;  // HIGH MEANS NO OBSTACLE
int sensor2 = HIGH;
int sensor3 = HIGH;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(isSensePin, INPUT);
  pinMode(isSensePin2, INPUT);
  pinMode(isSensePin3, INPUT);

  Serial.begin(9600);
  while(!Serial)
  {
    ;
    }
   if(Ethernet.begin(mac) == 0)
   {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
    }

    delay(1000);
    Serial.println("connecting...");

    if(client.connect(server, 80))
    {
      Serial.println("---connected---");

      client.println("GET /arduino/getdata.php?");
      client.println("Host: 192.168.0.121");
      client.println("Connection: close");
      client.println();
      }
     else
     {
      Serial.println("---connection failed---");
      }
}


void loop()
{
  sensor1 = digitalRead(isSensePin);
  sensor2 = digitalRead(isSensePin2);
  sensor3 = digitalRead(isSensePin3);

  int rc = client.connect(server, 80);
  if (rc)
  {
    Serial.println("---connection ok---");
    
    client.print("GET /arduino/getdata.php?");
    client.print("sensor1 = ");
    client.print(sensor1);
    client.print("&sensor2 = \n");
    client.print(sensor2);
    client.print("&sensor3 = \n");
    client.print(sensor3);

    client.println("HTTP/1.1");
    client.println("Host: 192.168.0.121");
    client.println("Connection: close");
    client.println();

    Serial.print("sensor1 = ");
    Serial.println(sensor1);
    Serial.print("sensor2 = ");
    Serial.println(sensor2);
    Serial.print("sensor3 = ");
    Serial.println(sensor3);

    client.stop();
  }

  else
  {
    Serial.println("---connection failed---\n");
    Serial.println(rc);
    client.stop();
  }

  delay (500);

  sensor1 = digitalRead(isSensePin);
  if (sensor1 == LOW)
  {
    Serial.println("Occupied1");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available1");
    digitalWrite(LED, LOW);
  }
  delay(1000);

  sensor2 = digitalRead(isSensePin2);
  if (sensor2 == LOW)
  {
    Serial.println("Occupied2");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available2");
    digitalWrite(LED, LOW);
  }
  delay(1000);

  sensor3 = digitalRead(isSensePin3);
  if (sensor3 == LOW)
  {
    Serial.println("Occupied3");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available3");
    digitalWrite(LED, LOW);
  }
  delay(1000);
}

The result that I get when I run the Arduino code:

connecting...
---connected---
---connection failed---

0
Available1
Available2
Available3
---connection ok---
sensor1 = 1
sensor2 = 1
sensor3 = 1
Available1
Available2
Available3
---connection ok---
sensor1 = 0
sensor2 = 1
sensor3 = 0
Occupied1
Available2
Available3

getdata.php:

<?php

    $connect = mysqli_connect("localhost", "root", "", "arduino");

    $sensor1 = $_GET['sensor1'];
    $sensor2 = $_GET['sensor2'];
    $sensor3 = $_GET['sensor3'];

    $sql_insert = "insert into data (sensor1, sensor2, sensor3) values ('$sensor1', '$sensor2', '$sensor3')";

    mysqli_query($connect, $sql_insert);

    if($sql_insert)
    {
        echo "success";
    }
    else
    {
        echo "failed";
    }

?>

When I test getdata.php, it works well to store the data to the database, and for status.php, it also works fine in retrieve the data.

status.php is where I show the data from MySQL database:

<?php
    $url = $_SERVER['REQUEST_URI'];
    header("Refresh: 5; URL = $url");
?>

<!DOCTYPE html>
<html>
<title> ARDUINO Parking Status </title>
<head> 
	<link rel = "stylesheet" type = "text/css" href = "testing.css" />
    <meta charset="utf-8">
</head>

<body>
	<div class="wrapper">
	  

	  
	  <header> 
		<img src="test 2.png" class="img2"/> 
	  </header>
	  
	  <nav>
		<div id="rightcol">
            <div class="imgh"><img src="parking.png" class="img4"/></div>

		Welcome, Admin<!--?php echo $_SESSION['username']; ?-->.<p/>
		<a href="logout.php" class="basicbtt">Logout</a>
<p/>
		<hr>

        <center><a href="#" class="sidebtt" style="padding: 6px 33px;">PARKING STATUS</a></center>
<p/>
        Parking status:-
<p/>
        Total: 
<p/>
        Available: 
<p/>
        Occupied: 
<p/>
        
<center><a href="user.php" class="sidebtt">REGISTERED USER</a></center>


		<center><a href="report.php" class="sidebtt" style="padding: 6px 60px;">FEEDBACK</a></center>

            
        <p/><a href="testing.php">Main</a>
		</div>
	  </nav>
	  
	  <section>
		[b]<div id="tablecol">
		<table width="500" border="1" cellspacing="2" cellpadding="5">
            <tr>
                
                <th>NO.</th>
                <th>TIMESTAMP</th>
                <th>PARK 1</th>
                <th>PARK 2</th>
                <th>PARK 3</th>
                
            </tr>
            
        <?php
            
            $connect = mysqli_connect("localhost", "root", "", "arduino");
            
            $result = mysqli_query($connect, "select * from data");
            
            while($line = mysqli_fetch_array($result))
            {
                echo'<tr>';
                
                echo'<td>'.$line["id"].'</td>';
                echo'<td>'.date('d.m.Y - H:i:s', strtotime($line["timestamp"])).'</td>';
                echo'<td>'.$line["sensor1"].'</td>';
                echo'<td>'.$line["sensor2"].'</td>';
                echo'<td>'.$line["sensor3"].'</td>';
                
                echo'</tr>';
            }
        ?>[/b]
            
            </table>
		</div>
	  </section>
	</div>
</body>

</html>

I think there's something wrong with my arduino code. But I don't know what it is. I've always checking the IP address for my PC and the Ethernet shield if there's any changes, and if so, I would change it in my code. But the problem is still the same.

OK. The PHP code works when you type a URL manually in a web browser. Something like:

http://192.168.0.121/arduino/getdata.php?sensor1=91&sensor2=92&sensor3=93

If that is the case, this is odd:

    client.print("GET /arduino/getdata.php?");
    client.print("sensor1 = ");
    client.print(sensor1);
    client.print("&sensor2 = \n");
    client.print(sensor2);
    client.print("&sensor3 = \n");
    client.print(sensor3);

maybe this instead ?:

    client.print("GET /arduino/getdata.php?");
    client.print("sensor1=");
    client.print(sensor1);
    client.print("&sensor2=");
    client.print(sensor2);
    client.print("&sensor3=");
    client.print(sensor3);

You have also structured your loop() oddly, in that if this test fails:

if (rc)

You carry on at this statement instead of giving up:

delay (500);

  sensor1 = digitalRead(isSensePin);
  if (sensor1 == LOW)
  {
    Serial.println("Occupied1");
    digitalWrite(LED, HIGH);
  }
  . . .
  . . .

You are not using status.php in your Arduino code.

Thanks for your reply.

6v6gt:
OK. The PHP code works when you type a URL manually in a web browser. Something like:

http://192.168.0.121/arduino/getdata.php?sensor1=91&sensor2=92&sensor3=93

Yes. It works just fine when I'm doing that. I can see the value is store in the database.

I change my code like this, as what I understand from your reply:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 192, 168, 0, 127 };
byte server[] = { 192, 168, 0, 121 };

//Initialize the Ethernet server library
EthernetClient client;

int LED = 13; // Use the onboard Uno LED
int isSensePin = 7;  // This is our input pin
int isSensePin2 = 8;
int isSensePin3 = 9;
int sensor1 = HIGH;  // HIGH MEANS NO OBSTACLE
int sensor2 = HIGH;
int sensor3 = HIGH;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(isSensePin, INPUT);
  pinMode(isSensePin2, INPUT);
  pinMode(isSensePin3, INPUT);

  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }
  if (Ethernet.begin(mac) == 0)
  {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  delay(1000);
  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("---connected---");

    client.println("GET /arduino/getdata.php?");
    client.println("Host: 192.168.0.121");
    client.println("Connection: close");
    client.println();
  }
  else
  {
    Serial.println("---connection failed---");
  }
}


void loop()
{
  sensor1 = digitalRead(isSensePin);
  sensor2 = digitalRead(isSensePin2);
  sensor3 = digitalRead(isSensePin3);

  if (client.connect(server, 80))
  {
    Serial.println("---connection ok---");

    client.print("GET /arduino/getdata.php?");
    client.print("sensor1 = ");
    client.print(sensor1);
    client.print("&sensor2 = ");
    client.print(sensor2);
    client.print("&sensor3 = ");
    client.print(sensor3);

    client.println("HTTP/1.1");
    client.println("Host: 192.168.0.121");
    client.println("Connection: close");
    client.println();

    Serial.print("sensor1 = ");
    Serial.println(sensor1);
    Serial.print("sensor2 = ");
    Serial.println(sensor2);
    Serial.print("sensor3 = ");
    Serial.println(sensor3);

    client.stop();
  }

  else
  {
    Serial.println("---connection failed---\n");
    client.stop();
  }

  delay (500);

  sensor1 = digitalRead(isSensePin);
  if (sensor1 == LOW)
  {
    Serial.println("Occupied1");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available1");
    digitalWrite(LED, LOW);
  }
  delay(1000);

  sensor2 = digitalRead(isSensePin2);
  if (sensor2 == LOW)
  {
    Serial.println("Occupied2");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available2");
    digitalWrite(LED, LOW);
  }
  delay(1000);

  sensor3 = digitalRead(isSensePin3);
  if (sensor3 == LOW)
  {
    Serial.println("Occupied3");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available3");
    digitalWrite(LED, LOW);
  }
  delay(1000);
}

and the output is like this:

connecting...
---connection failed---
---connection ok---
sensor1 = 0
sensor2 = 1
sensor3 = 1
Occupied1
Available2
Available3
---connection ok---
sensor1 = 1
sensor2 = 1
sensor3 = 1
Occupied1
Available2
Available3
---connection ok---
sensor1 = 0
sensor2 = 1
sensor3 = 1

It seems like the connection is failed at void setup()

6v6gt:
You are not using status.php in your Arduino code.

As what I understand, getdata.php is to store data to database, and status.php is to display the data from database. Are you saying that I need to change -client.print("GET /arduino/getdata.php?");- to this -client.print("GET /arduino/status.php?");- ?

The connection at setup() doesn't seem to do anything so you can leave that at the moment.

I'm not sure why you still have embedded spaces here, for example:

client.print("&sensor2 = ");

Maybe it does not matter but if you have not put any spaces in the URL you type manually:

http://192.168.0.121/arduino/getdata.php?sensor1=91&sensor2=92&sensor3=93

Then it is best to leave these out.

No, I am not suggesting you attempt to include status.php in your code. I was just wondering why you posted it here since it is not (yet) used.

I can't see from the output if the values of sensor1, sensor2 or sensor3 got into the database. If they did, is your problem then solved ?

I'm sorry. I thought that the empty space doesn't affect anything. I already get rid of the spaces.

6v6gt:
No, I am not suggesting you attempt to include status.php in your code. I was just wondering why you posted it here since it is not (yet) used.

I can't see from the output if the values of sensor1, sensor2 or sensor3 got into the database. If they did, is your problem then solved ?

I posted it just to make sure there's nothing wrong in my code to retrieve the data, but actually, it can successfully retrieve the data in the database and display it in my website.

http://192.168.0.121/arduino/getdata.php?sensor1=91&sensor2=92&sensor3=93

When I do this, the output is successfully stored in MySQL database. The sensor data that is shown in the serial monitor is not yet successfully stored in my database. So, the problem is still remain the same. I wonder if there's a problem in any of my code.

OK. Look very carefully at the example at https://www.arduino.cc/en/Tutorial/WebClient

From what I can see, try changing:

client.println("HTTP/1.1");

to:

client.print(" HTTP/1.1");  // note first space  and print instead of println

Also remove the embedded spaces here and similar:

client.print("&sensor2 = ");   // should be "&sensor2="

If it still does not work, post your code here again.

6v6gt:
OK. Look very carefully at the example at https://www.arduino.cc/en/Tutorial/WebClient

I tried to run the webclient code and it successfully connected.

In my code, I've changed the code, and remove some code in void setup(), but still, my database cannot store the data from the sensor.

This is my code:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 192, 168, 0, 127 };
byte server[] = { 192, 168, 0, 121 };

//Initialize the Ethernet server library
EthernetClient client;

int LED = 13; // Use the onboard Uno LED
int isSensePin = 7;  // This is our input pin
int isSensePin2 = 8;
int isSensePin3 = 9;
int sensor1 = HIGH;  // HIGH MEANS NO OBSTACLE
int sensor2 = HIGH;
int sensor3 = HIGH;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(isSensePin, INPUT);
  pinMode(isSensePin2, INPUT);
  pinMode(isSensePin3, INPUT);

  Serial.begin(9600);
  Ethernet.begin(mac, ip);
}


void loop()
{
  sensor1 = digitalRead(isSensePin);
  sensor2 = digitalRead(isSensePin2);
  sensor3 = digitalRead(isSensePin3);

  if (client.connect(server, 80))
  {
    Serial.println("---connection ok---");

    client.print("GET /arduino/getdata.php?");
    client.print("sensor1=");
    client.print(sensor1);
    client.print("&sensor2=");
    client.print(sensor2);
    client.print("&sensor3=");
    client.print(sensor3);

    client.print(" HTTP/1.1");
    client.println("Host: 192.168.0.121");
    client.println("Connection: close");
    client.println();

    Serial.print("sensor1=");
    Serial.println(sensor1);
    Serial.print("sensor2=");
    Serial.println(sensor2);
    Serial.print("sensor3=");
    Serial.println(sensor3);

    client.stop();
  }

  else
  {
    Serial.println("---connection failed---\n");
    client.stop();
  }

  delay (500);

  
  if (sensor1 == LOW)
  {
    Serial.println("Occupied1");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available1");
    digitalWrite(LED, LOW);
  }
  delay(1000);

 
  if (sensor2 == LOW)
  {
    Serial.println("Occupied2");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available2");
    digitalWrite(LED, LOW);
  }
  delay(1000);

  
  if (sensor3 == LOW)
  {
    Serial.println("Occupied3");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available3");
    digitalWrite(LED, LOW);
  }
  delay(1000);
}

I've made changes in two places following the sample Ethernet Client code:

  1. obtaining an IP address from dhcp if available
  2. printing out any return information from the web/php server.
    I can't test it, however.
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 192, 168, 0, 127 };
byte server[] = { 192, 168, 0, 121 };

//Initialize the Ethernet server library
EthernetClient client;

int LED = 13; // Use the onboard Uno LED
int isSensePin = 7;  // This is our input pin
int isSensePin2 = 8;
int isSensePin3 = 9;
int sensor1 = HIGH;  // HIGH MEANS NO OBSTACLE
int sensor2 = HIGH;
int sensor3 = HIGH;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(isSensePin, INPUT);
  pinMode(isSensePin2, INPUT);
  pinMode(isSensePin3, INPUT);

  Serial.begin(9600);
  
  // =====================
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // =====================
}


void loop()
{
  sensor1 = digitalRead(isSensePin);
  sensor2 = digitalRead(isSensePin2);
  sensor3 = digitalRead(isSensePin3);

  if (client.connect(server, 80))
  {
    Serial.println("---connection ok---");

    client.print("GET /arduino/getdata.php?");
    client.print("sensor1=");
    client.print(sensor1);
    client.print("&sensor2=");
    client.print(sensor2);
    client.print("&sensor3=");
    client.print(sensor3);

    client.print(" HTTP/1.1");
    client.println("Host: 192.168.0.121");
    client.println("Connection: close");
    client.println();

    Serial.print("sensor1=");
    Serial.println(sensor1);
    Serial.print("sensor2=");
    Serial.println(sensor2);
    Serial.print("sensor3=");
    Serial.println(sensor3);


    // ============

    // if there are incoming bytes available
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }

    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
    }

    // ============

  }

  else
  {
    Serial.println("---connection failed---\n");
    client.stop();
  }

  delay (500);


  if (sensor1 == LOW)
  {
    Serial.println("Occupied1");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available1");
    digitalWrite(LED, LOW);
  }
  delay(1000);


  if (sensor2 == LOW)
  {
    Serial.println("Occupied2");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available2");
    digitalWrite(LED, LOW);
  }
  delay(1000);


  if (sensor3 == LOW)
  {
    Serial.println("Occupied3");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available3");
    digitalWrite(LED, LOW);
  }
  delay(1000);
}

If that doesn't work, but prints something, then post the results.

Is the web browser that you manually entered the following command on
http://192.168.0.121/arduino/getdata.php?sensor1=91&sensor2=92&sensor3=93
running on an normal PC in your network or is it running on the web server itself ?

Can you access the web server log files ?

This is the code that I have change according to your reply:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte ip[] = { 192, 168, 0, 127 };
byte server[] = { 192, 168, 0, 121 };

//Initialize the Ethernet server library
EthernetClient client;

int LED = 13; // Use the onboard Uno LED
int isSensePin = 7;  // This is our input pin
int isSensePin2 = 8;
int isSensePin3 = 9;
int sensor1 = HIGH;  // HIGH MEANS NO OBSTACLE
int sensor2 = HIGH;
int sensor3 = HIGH;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(isSensePin, INPUT);
  pinMode(isSensePin2, INPUT);
  pinMode(isSensePin3, INPUT);

  Serial.begin(9600);
  
  // =====================
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // =====================
}

void loop()
{
  sensor1 = digitalRead(isSensePin);
  sensor2 = digitalRead(isSensePin2);
  sensor3 = digitalRead(isSensePin3);

  if (client.connect(server, 80))
  {
    Serial.println("---connection ok---");

    client.print("GET /arduino/getdata.php?");
    client.print("sensor1=");
    client.print(sensor1);
    client.print("&sensor2=");
    client.print(sensor2);
    client.print("&sensor3=");
    client.print(sensor3);

    client.print(" HTTP/1.1");
    client.println("Host: 192.168.0.121");
    client.println("Connection: close");
    client.println();

    Serial.print("sensor1=");
    Serial.println(sensor1);
    Serial.print("sensor2=");
    Serial.println(sensor2);
    Serial.print("sensor3=");
    Serial.println(sensor3);

     // ============

    // if there are incoming bytes available
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }

    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
    }
    // ============
  }

  else
  {
    Serial.println("---connection failed---\n");
    client.stop();
  }

  delay (500);

  
  if (sensor1 == LOW)
  {
    Serial.println("Occupied1");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available1");
    digitalWrite(LED, LOW);
  }
  delay(1000);

 
  if (sensor2 == LOW)
  {
    Serial.println("Occupied2");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available2");
    digitalWrite(LED, LOW);
  }
  delay(1000);

  
  if (sensor3 == LOW)
  {
    Serial.println("Occupied3");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("Available3");
    digitalWrite(LED, LOW);
  }
  delay(1000);
}

The result shows that one time the connection is ok and another time it is failed:

---connection ok---
sensor1=1
sensor2=1
sensor3=1
Available1
Available2
Available3
---connection failed---

Occupied1
Available2
Occupied3
---connection ok---
sensor1=0
sensor2=1
sensor3=0
Occupied1
Available2
Occupied3
---connection failed---

Available1
Occupied2
Occupied3
. . .

There is this one part, it print this in the serial monitor:

---connection ok---
sensor1=1
sensor2=1
sensor3=1
HTTP/1.1 400 Bad Request
Date: Fri, 04 May 2018 15:42:23 GMT
Server: Apache/2.4.33 (Win64) PHP/5.6.35
Content-Length: 311
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.

</p>
<hr>
<address>Apache/2.4.33 (Win64) PHP/5.6.35 Server at localhost Port 80</address>
</body></html>

disconnecting.
Available1
Available2
Available3
---connection ok---
sensor1=1
sensor2=1
sensor3=1
HTTP/1.1 400 Bad Request
Date: Fri, 04 May 2018 15:42:27 GMT
Server: Apache/2.4.33 (Win64) PHP/5.6.35
Content-Length: 311
Connection: close
Content-Type: text/html; charset=iso-8859-1
. . .

6v6gt:
Is the web browser that you manually entered the following command on
http://192.168.0.121/arduino/getdata.php?sensor1=91&sensor2=92&sensor3=93
running on an normal PC in your network or is it running on the web server itself ?

It's running on WAMP server.

6v6gt:
Can you access the web server log files ?

Yes, I can access the log files.

Is there another PC in your network (apart from the one running the wamp server) where you could try the test:

http://192.168.0.121/arduino/getdata.php?sensor1=1&sensor2=0&sensor3=1

It just could be an access rights issue which prevents the arduino code working although, from the response you got back, it looks more like a malformed HTTP GET problem.

As for the alternate fail/success issue

  1. comment out the two client.stop() statements in your latest code.
  2. add a new client.stop() statement as the first statement at the beginning of the loop()

To see if that clears the issue.

The following is a better template to use where you are making repeated http get calls:

https://www.arduino.cc/en/Tutorial/WebClientRepeating . Maybe use the structure there.

You've said you can see the web server log files. What do you see for the last time you ran your script ? Do you see the full URL your script sent ? If so, post it ?

Unfortunately, for the time being, there's no PC that I can test on right now.

6v6gt:
As for the alternate fail/success issue

  1. comment out the two client.stop() statements in your latest code.
  2. add a new client.stop() statement as the first statement at the beginning of the loop()

To see if that clears the issue.

I tried this, and it works. The connection is all ok. But I still get 400 Bad Request. I google-ed this issue, and found that I need to clear the cache and cookies in my browser. So I did, and it didn't print 400 Bad Request anymore. But, that is just for a while (like first and second test), and then 400 Bad Request appear again.

6v6gt:
The following is a better template to use where you are making repeated http get calls:

https://www.arduino.cc/en/Tutorial/WebClientRepeating . Maybe use the structure there.

I run this WebClientRepeating code. It gives me this:

My IP address: 192.168.0.127
connecting...
HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Date: Sat, 05 May 2018 07:56:10 GMT
Location: https://www.arduino.cc/latest.txt
Server: nginx/1.4.2
Content-Length: 184
Connection: Close

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.2</center>
</body>
</html>

6v6gt:
You've said you can see the web server log files. What do you see for the last time you ran your script ? Do you see the full URL your script sent ? If so, post it ?

So this is my access.log file:

192.168.0.127 - - [05/May/2018:15:41:55 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1Host:" 400 311
192.168.0.127 - - [05/May/2018:15:41:59 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1Host:" 400 311
192.168.0.127 - - [05/May/2018:15:42:03 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1Host:" 400 311
192.168.0.127 - - [05/May/2018:15:42:07 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1Host:" 400 311
192.168.0.127 - - [05/May/2018:15:55:11 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1Host:" 400 311
192.168.0.127 - - [05/May/2018:15:55:15 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1Host:" 400 311

What does 400 311 behind it mean?

But I still get 400 Bad Request. I google-ed this issue, and found that I need to clear the cache and cookies in my browser. So I did, and it didn't print 400 Bad Request anymore.

Which browser did you clean the cache from? In this case your Arduino is acting as the browser (as a web client).

The web repeating example has to be adapted for your environment. That means it would have to have your server configured and your format URL with athe 3 sensors. It would take some work. I gave it as an example for the structure. But it at least show that, from your network, you can get out to an external network.
Anyway, it looks like the change to the client.stop() statements may have solved the failed connection problem, so it may not be necessary to use the repeating example.

The 311 in the log entry ending with 400 311 is simply the number of bytes sent from the server to the client (Arduino).

Can you try entering:

http://192.168.0.121/arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1

in a web browser again and see exactly what you get in the Apache access log to compare with what you get from when the same statement is issued from the Arduino. And check the mySQL database to see that the new entry was actually written.
When you enter the URL in the browser, does the URL change at all when you hit enter ? If so, this might indicate a redirect.

If there is no other PC maybe, if it is a WLAN network, connect your smartphone (if you have one) to that local network and try the browser there as well.

6v6gt:
Which browser did you clean the cache from? In this case your Arduino is acting as the browser (as a web client).

I clean the cache and cookies from Google Chrome. Oh, if that the case, is there a way to clean cache from Arduino (if there's any)?

This is what I get in apache.log file. I've tried run the test in several smartphones and it did update in the database, also same for the web browser in my PC. When I hit enter, the URL didn't change. It stays the same. But still, I tried again with Arduino, and it still don't update the value in the database.

192.168.0.121 - - [05/May/2018:17:14:35 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1" 200 7
192.168.0.121 - - [05/May/2018:17:14:58 +0800] "GET /phpmyadmin/sql.php?server=1&db=arduino&table=dataaa&pos=0&token=c6a1554d934ae7d8c0c1a31c5c12cc8f HTTP/1.1" 200 12771
192.168.0.121 - - [05/May/2018:17:15:00 +0800] "POST /phpmyadmin/navigation.php?ajax_request=1&token=c6a1554d934ae7d8c0c1a31c5c12cc8f HTTP/1.1" 200 2497
192.168.0.121 - - [05/May/2018:17:15:00 +0800] "GET /phpmyadmin/index.php?ajax_request=1&recent_table=1&token=c6a1554d934ae7d8c0c1a31c5c12cc8f&no_debug=true&_nocache=1525511700438682684 HTTP/1.1" 200 1475
192.168.0.121 - - [05/May/2018:17:15:00 +0800] "GET /phpmyadmin/favicon.ico HTTP/1.1" 200 22486
192.168.0.142 - - [05/May/2018:17:15:23 +0800] "GET /arduino/getdata.php?sensor1=10&sensor2=20&sensor3=30 HTTP/1.1" 200 7
192.168.0.121 - - [05/May/2018:17:15:27 +0800] "GET /phpmyadmin/sql.php?server=1&db=arduino&table=dataaa&pos=0&token=c6a1554d934ae7d8c0c1a31c5c12cc8f HTTP/1.1" 200 12858
192.168.0.121 - - [05/May/2018:17:15:28 +0800] "POST /phpmyadmin/navigation.php?ajax_request=1&token=c6a1554d934ae7d8c0c1a31c5c12cc8f HTTP/1.1" 200 2497
192.168.0.121 - - [05/May/2018:17:15:29 +0800] "GET /phpmyadmin/favicon.ico HTTP/1.1" 200 22486
192.168.0.121 - - [05/May/2018:17:15:28 +0800] "GET /phpmyadmin/index.php?ajax_request=1&recent_table=1&token=c6a1554d934ae7d8c0c1a31c5c12cc8f&no_debug=true&_nocache=1525511728931850382 HTTP/1.1" 200 1475

OK. I think the problem is now clear from comparing the log files. The first one fails. The other 2 work.

192.168.0.127 - - [05/May/2018:15:41:55 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1Host:" 400 311

192.168.0.121 - - [05/May/2018:17:14:35 +0800] "GET /arduino/getdata.php?sensor1=1&sensor2=1&sensor3=1 HTTP/1.1" 200 7
192.168.0.142 - - [05/May/2018:17:15:23 +0800] "GET /arduino/getdata.php?sensor1=10&sensor2=20&sensor3=30 HTTP/1.1" 200 7

Change:

client.print(" HTTP/1.1");
client.println("Host: 192.168.0.121");

to by adding a new println():

client.print(" HTTP/1.1");
client.println();   // new
client.println("Host: 192.168.0.121");

It works! :smiley: Thank you so much for your help!

I have another question. Is it possible to display the picture in the website instead of the number? Do I need to change my arduino code in order to put in the picture?

OK. I'm happy that problem has been solved.

Do I understand your next question correctly ? When a user types http://192.168.0.121/arduino/status.php in a web browser, you want to display some images and the images displayed should be dependent on the value of the sensor readings ? For example, if sensor1 returns a value of 0, you want to display a specific image and if sensor 1 returns a value of 1 you want to display another ?

If so, that is a pure PHP/HTML matter and really nothing more to do with an Arduino.

You would progress as follows:

  1. Identify some images you want to use and store these in such a way that these are accessible from you web server using, for example the link http://192.168.0.121/arduino/image1.jpg , http://192.168.0.121/arduino/image2.jpg etc.

  2. Write some test html which formats the page you want to display to the user, which includes the images.

  3. once you are happy with the display format, embed the essential elements of the html in the PHP script and make the display of the images conditional on the sensor results retrieved from the database.

6v6gt:
Do I understand your next question correctly ? When a user types http://192.168.0.121/arduino/status.php in a web browser, you want to display some images and the images displayed should be dependent on the value of the sensor readings ? For example, if sensor1 returns a value of 0, you want to display a specific image and if sensor 1 returns a value of 1 you want to display another ?

Yes, its correct and I successfully display image instead of the value in my website last night. Thanks for your time! Appreciate it! :drooling_face: