Client.connect() Error

I'm trying to send my data to PHP and upload it to mysql database. I tried using it on a XAMPP and it is working, now i'm trying to upload it on a website but I have no idea if im doing it right.

I tried doing this line of code Serial.println(client.connect(server,80)); and it returns 0.

PS: I also posted this on this site: ethernet shield - Sending Arduino data to PHP and upload it to MYSQL DATABASE - Arduino Stack Exchange

ARDUINO CODE:

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


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server [] = "mywebsite.000webhostapp.com"; // Please assume that this is my website.
IPAddress ip(192, 168, 1, 177);


#define trigPin 2
#define echoPin 3
long duration;
float distance;
int pinSensor =8;
int pinLed =9;
int pinBuzzer =7;
int pirSensor =0;
int pirState = LOW;
String desc = "";

EthernetClient client;

void setup() {

Serial.begin(9600);
delay(1000);
Ethernet.begin(mac, ip);
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());  

pinMode(pinSensor, INPUT);
pinMode(pinLed, OUTPUT);
pinMode(pinBuzzer, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);


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

if (client.connect(server,80)){
Serial.println("OK");
}
else {
Serial.println("NO");
}

}

void loop()
{

 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

 distance = (duration*0.034/2)/100;
 Serial.println(distance);
 delay(500);

 pirSensor = digitalRead(pinSensor);
 if (pirSensor == HIGH)
 {  
  digitalWrite(pinLed, HIGH);
   if (pirState == LOW)
   {
      desc = "DISTANCE:" +String(distance) +"m";
      pirState = HIGH;
      
    
        if (distance < 3)
        {
          tone(pinBuzzer, 1000,3000);
          
          desc = "DISTANCE:" +String(distance)+ "m";
                
        }
     
        delay(3000); 
        
    }
    else
    {
     if(pirState=HIGH)
     {
      pirState=LOW;    
      noTone(pinBuzzer);
     }
    }

   if (client.connect(server,80)){
    Serial.println(desc);
    client.println("GET /getdata1.php?desc=" +desc+ " HTTP/1.1");
    client.println("Host: mywebsite.000webhostapp.com");
    client.println("Connection: close");
    client.println();
    }
    else 
    {
     Serial.println("--- Connection Failed");
    } 
           
    }
    }```

PHP CODE:

include.php
<?php
$servername="localhost";
$username="mywebsiteusername";
$password= "P^axdR%D=vS>@bj2";
$dbase= "mywebsitedatabase";

$con= new mysqli($servername,$username,$password,$dbase);

if($con->connect_error)
{
echo("<h2>Connection Error</h2>" .$con->connect_error);
}
?>


getdata1.php
<?php
include "include.php";

$cdate = date("y-m-d");
date_default_timezone_set("Asia/Hong_Kong");
$ctime = date("h:i:sa");

$sql_insert = "INSERT INTO tbl_sensor (DATE, TIME, DESCRIPTION) VALUES ('$cdate', '$ctime', 
'".$_GET['desc']."')";

if(mysqli_query($con,$sql_insert))
{
echo "Done";
mysqli_close($con);
}
else
{
echo "error is ".mysqli_error($con );
}
?>

does the site accept insecure connection?

Hi Mr. Juraj. That is also my concern. I feel that is something todo with my website but I am not sure and literally have no idea. Thank you for patiently replying.

You are trying to connect twice. Which one indicates an error?

Hello Mr. Johnwasser the firsrt one is like a test if it will connect then I dont have to worry. But the output is always NO

this is the output that I always get:
image

Using the site logs helps in figuring if the application is reaching the site. Also, if the application is reaching the site the logs will show why the connection is being refused.

Perhaps the issue is with the assumed return values of the Adruino Ethernet library?

"

Returns

Returns an int (1,-1,-2,-3,-4) indicating connection status :

  • SUCCESS 1
  • TIMED_OUT -1
  • INVALID_SERVER -2
  • TRUNCATED -3
  • INVALID_RESPONSE -4
    "

Hello I tried the code in the link, Is it normal if it disconnects? I have noticed it is similar to web client in the example in Arduino. I also tried running the web client example but similarly it disconnects.

Thank you for responding.

I checked my site logs. There is none.

that is only wrong documentation. it only returns 1 or 0

Hello guys, I have noticed that whenever I am using XAMPP it will only work if my char server[] = "192.168.1.4" otherwise it will not connect. I dont know if it has something to do with my problem :frowning:

Then the site is not being reached by your application.

Thank you for your help. Do you happen to know on what should I do?

That's not a solution?

Yeah. But I feel that I need to mention it. Still working on it and still no progress.

I tried running my Arduino WebClient example.
And this is the output I recieved, I wonder if it has something to do with it:
image

If you can't connect to the network you can't do the MQTT thing.

If you can connect to an IP-address, you issue might be a failure in DNS lookups.

EDIT: You should also mind that if your ethernet shield has a MAC address printed on it (may be a label), you must use this MAC address, otherwise your router may deny access. Also read this.

EDIT2: Try to check if your ethernet (DHCP) is established with:

if (Ethernet.begin(mac) == 0)
{
  Serial.println("Ethernet failed to initialize..");
  while (1) ; //Lockup
}

@Danois90 @Idahowalker see the screenshot two comments back

Yes. That's what i'm trying to achieve.