not get response from server using ethernet shield arduino

Hello, I use ethernet shield arduino to read data from mysql database.This is my sketch :

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

#include <SPI.h>

byte mac[] = {
  0xDE,0xAD,0xBE,0xEF,0xFE,0xED
};

IPAddress ip(192, 168, 1, 177);
IPAddress server(192, 168, 1, 10);
EthernetClient client;

char s[8];

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  Ethernet.begin(mac,ip);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (client.connect(server,80)) {  
    client.println("GET /GetData.php");
    client.println("HTTP/1.1");
    client.println("Host: 192.168.1.10");
    client.println("Connection: close");
    client.println();
    client.println();
    //client.stop();

    Serial.println("GET /GetData.php");
  Serial.println("HTTP/1.1");
  Serial.println("Host: 192.168.1.10");
  Serial.println();
  Serial.println();
  }
  else {
    Serial.println("Connection failed\n");
  }
  String data="";
    int a = 0;
    while (client.available()) {
      char c = client.read();
      data += c;
      //Serial.print(c);
      a++;
    }
    Serial.println(data);
delay(5000);
}

And this is my php code:

<?php 
require_once('koneksi.php');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

$sql = "SELECT * FROM tb_room"; 

$result = $con->query($sql);

while ($row=mysqli_fetch_row($result)) {
  if ($row[3]=='ON') {
     echo "0"; 
  }   
  else {
     echo "1";
  }
}

$con->close();

?>

And I get a response I want :

GET /GetData.php
HTTP/1.1
Host: 192.168.1.10



Connection failed

 
00010110

But, if I use this sketch :

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

#include <SPI.h>

byte mac[] = {
  0xDE,0xAD,0xBE,0xEF,0xFE,0xED
};

IPAddress ip(192, 168, 1, 177);
IPAddress server(192, 168, 1, 10);
EthernetClient client;

char s[8];

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  Ethernet.begin(mac,ip);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (client.connect(server,80)) {  
    client.println("GET /GetData.php");
    client.println("HTTP/1.1");
    client.println("Host: 192.168.1.10");
    client.println("Connection: close");
    client.println();
    client.println();
    //client.stop();

    Serial.println("GET /GetData.php");
  Serial.println("HTTP/1.1");
  Serial.println("Host: 192.168.1.10");
  Serial.println();
  Serial.println();
  }
  else {
    Serial.println("Connection failed\n");
  }
  String data="";
    int a = 0;
    while (client.available()) {
      char c = client.read();
      s[a] = c;
      //data += c;
      //Serial.print(c);
      a++;
    }
    Serial.println(s[0]);
delay(5000);
}

I don’t get a response I want.

GET /GetData.php
HTTP/1.1
Host: 192.168.1.10



Connection failed

why I can’t get a response if I’m using array to save the response? Please someone help me. Thank you.

And I get a response I want :

You want "Connection failed"? You are one strange dude.

You are not getting the response you want. Besides the “Connection failed”, once you get the client to connect, you will probably get a 400 error.

// change this
    client.println("GET /GetData.php");
    client.println("HTTP/1.1");

// to this
    client.print("GET /GetData.php");
    client.println(" HTTP/1.1");

Now I can get the response from server like this:

connected
GET /GetData.php HTTP/1.1
Host: 192.168.1.10


⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮HTTP/1.1 200 OK
Date: Sun, 02 Jul 2017 07:53:29 GMT
Server: Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.1
Content-Length: 13
Content-Type: text/html

 
<10110110>⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

disconnecting.

Now, I want to get just the value 10110110 and separate them one by one to control a relay. So, this value is to control 8 relay. If first value is 1, the first relay will off, if the second value is 0, the second relay will on. This is my arduino sketch:

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

#include <SPI.h>

byte mac[] = {
  0xDE,0xAD,0xBE,0xEF,0xFE,0xED
};

IPAddress ip(192, 168, 1, 177);
IPAddress server(192, 168, 1, 10);
EthernetClient client;

char s[8];

void setup() {
  // put your setup code here, to run once:
 
  Serial.begin(9600);
  while (Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  
  
  Ethernet.begin(mac,ip);
  //while (!client.connect(server,80)) {
  if (client.connect(server,80)) {  
    Serial.println("connected");
  break;
  }
  else {
    Serial.println("Connection failed\n");
  }
  delay(10000);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  client.connect(server,80);
  if (client.connected()) {
    client.print("GET /GetData.php");
    client.println(" HTTP/1.1");
    client.println("Host: 192.168.1.10");
    //client.println("Connection: close");
    client.println();
    client.println();
    //client.stop();

    Serial.print("GET /GetData.php");
  Serial.println(" HTTP/1.1");
  Serial.println("Host: 192.168.1.10");
  Serial.println();
  Serial.println();
  }
  else {
    Serial.println("Client disconnect");
  }
  boolean collectdata = false;
  
  int a = 0;
    while (client.connected()) {
      char c = client.read();

      if (collectdata = true) {
        s[a] = c;
        a++;
      }
      if (c == '<') {
        collectdata = true;
      }
      if (c == '>') {
        collectdata = false;
      }
      
    }
    
    for (int a=0;a<=7;a++) {
      Serial.println(s[a]);
    }
    if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    }
 delay(2000);  
}

And this is the ouput in serial monitor :

connecte⸮

what is the problem? Why I don’t get the value?

You must change the assignment to a comparison.

// change this
      if (collectdata = true) {

// to this
      if (collectdata == true) {

It’s work. Thank you very much.