Problems in connecting Arduino to an Apache server

Hi guys,

recently, i’m in a project that i need to connect my Arduino Uno to a DB. I’m doing it step by step (seeing that i’m rather new in programming world.), then i created an sever and an “webpage” to connect my arduino with.

But, when i send it to my Arduino and open my Serial Monitor, the connection fail, it doesn’t connect and i don’t know why.

My computer Ip adress: 192.168.0.20 (where my server is located.)
Arduino Ipadress: 192.168.0.17

I can ping the arduino.

Web Page:

// Just a simple page to connect my Arduino with.
// htdocs/arduino/webclient1.php

<?php 

 echo "Just a page.";

?>

Sketch:

// THIS IS THE DEFAULT EXEMPLE! 
// Changed only the ip adresses

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "192.168.0.20";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 17); 
IPAddress myDns(192, 168, 0, 1);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement

void setup() {
  
  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  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 ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    Serial.println(client.remoteIP());
    // Make a HTTP request:
    client.println("GET /arduino/webclient1.php"); //where my web page is located.
    client.println("Host: Localhost");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  beginMicros = micros();
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  int len = client.available();
  if (len > 0) {
    byte buffer[80];
    if (len > 80) len = 80;
    client.read(buffer, len);
    if (printWebData) {
      Serial.write(buffer, len); // show in the serial monitor (slows some boards)
    }
    byteCount = byteCount + len;
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    endMicros = micros();
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    Serial.print("Received ");
    Serial.print(byteCount);
    Serial.print(" bytes in ");
    float seconds = (float)(endMicros - beginMicros) / 1000000.0;
    Serial.print(seconds, 4);
    float rate = (float)byteCount / seconds / 1000.0;
    Serial.print(", rate = ");
    Serial.print(rate);
    Serial.print(" kbytes/second");
    Serial.println();

    // do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
}

This is the error that appears to me on Serial Monitor:

Initialize Ethernet with DHCP:
  DHCP assigned IP 192.168.0.17
connecting to 192.168.0.20...
connection failed

disconnecting.
Received 0 bytes in 0.0000, rate = 0.00 kbytes/second

You can also help me in Github if you find it easier.

Please, if i need to do something else let me know, and remember i’m not into this programming world as you may are :wink:

try IPAddress for the server too. for string it will use dns to resolve. is 192.168.0.1 your DNS server address?

Juraj: try IPAddress for the server too. for string it will use dns to resolve. is 192.168.0.1 your DNS server address?

The IPAdress of the server you mean "localhost | 127.0.0.1" ? If yes then, i already tried this..

and yes, my DNS server is 192.168.0.1.

IPAddress ip(192, 168, 0, 20); not char server[] = "192.168.0.20";

why would you try the loopback address?

Juraj: IPAddress ip(192, 168, 0, 20); not char server[] = "192.168.0.20";

why would you try the loopback address?

It is an variable created so i can call it later on line 70:

  if (client.connect(**server**, 80)) {

Juraj: IPAddress ip(192, 168, 0, 20); not char server[] = "192.168.0.20";

why would you try the loopback address?

I actually changed it to "IPAddress server (192,168,0,20);" and, still not connecting :(