ESP8266/ESP32 connection with personal domain

I am building a smart home project and want to establish communication between my personal domain and ESP (I don't want to use any 3rd party IOT solutions like AWS or IFTTT etc). I have a domain in hostinger and have also shared hosting. I am okay with HTTP protocol but am not able to get how to establish the communication since I know least in PHP. I am able to post the data to my website using this tutorial https://www.instructables.com/id/Arduino-Esp8266-Post-Data-to-Website/ but I also want to GET HTTP request from website to ESP

Is there anyone who have done similar project or can help me out in any ways. Thank you in Advance

basically on PHP side you can call

echo "this goes back to the ESP";

or

die("this goes back to the ESP");

and on the ESP side you will have something like

http.begin(completeURL);
int httpCode = http.GET();
String payload = http.getString();
Serial.println(payload);  // this will print  > this goes back to the ESP <

Thanks a lot @Kassim I shall try it right away, Arduino side I have no doubt now but in PHP I have few.

So my PHP code looks something like this.

<?php


$servername = "localhost";

$dbname = "dbname";
// Your Database user
$username = "usrname";
// Your Database user password
$password = "Pass";

// Keep this API Key value to be compatible with the ESP32 code provided in the project page. 
// If you change this value, the ESP32 sketch needs to match
$api_key_value = "tPmAT5Ab3j7F9";

$api_key= $sensor = $location = $value1 = $value2 = $value3 = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $api_key = test_input($_POST["api_key"]);
    if($api_key == $api_key_value) {
        $sensor = test_input($_POST["sensor"]);
        $location = test_input($_POST["location"]);
        $value1 = test_input($_POST["value1"]);
        $value2 = test_input($_POST["value2"]);
        $value3 = test_input($_POST["value3"]);
        
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
        
        $sql = "INSERT INTO SensorData (sensor, location, value1, value2, value3)
        VALUES ('" . $sensor . "', '" . $location . "', '" . $value1 . "', '" . $value2 . "', '" . $value3 . "')";
        
        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } 
        else {
            echo "Error: " . $sql . "
" . $conn->error;
        }
    
        $conn->close();
    }
    else {
        echo "Wrong API Key provided.";
    }

}
else {
    echo "No data posted with HTTP POST.";
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

Also another doubt, $servername = “localhost”; works fine within local system now(Though working in my domain’s hosting PHP only) if I have to connect it to my domain should change the servername ?

Hope my question makes sense. I am very new to PHP and server side (Started yesterday only).

mohits:
domain's hosting PHP only) if I have to connect it to my domain should change the servername ?

No, serverName remains 'localhost'

mohits:
I am very new to PHP and server side (Started yesterday only).

Then you definitely need a right forum for this

Besides, the link you posted shows tutorial on Arduino with ESP-01 Module ( talking over AT commands )

Any reason why you are using a combination of the two ( Arduino and ESP) ?

while ESP alone can suffice what you do, and you can program it straight from the Arduino IDE

However you really need board like Wemos or nodeMCU that comes with more input/output pins broken out

However you really need board like Wemos or nodeMCU that comes with more input/output pins broken out

Yes I am using ESP8266 NodeMCU-12E. I just used that tutorial and made necessary changes as required for nodeMCU and am now able to see the sensor data too in my website.

No, serverName remains 'localhost'

Oh ok, Thank you.

Things are more complicated when you send a GET/POST from the webserver back to your ESP. That is why so many people use 3rd party IOT solutions.

Your ISP probably allocates a dynamic IP address to you when your router connects, which could change each time. You need a DNS service to translate a host name into that currently allocated IP address, and a way to keep the DNS server updated with the correct IP address when it changes.

Your router will have a firewall which will block incoming requests. You need to make an exception in that firewall to allow requests from your webserver.

The IP addresses on your local network are not visible to the rest of the internet. You need to set your router to listen on an open port and forward and requests on to the ESP's local IP address.

Your ESP will probably be given a dynamic address on your local network each time it connects, by the DHCP function of your router. You may be able to give the ESP a fixed address by either restricting the range of addresses used by DHCP and hard coding an address in that range into the ESP, or by configuring the DHCP to allocate a fixed address based on the ESP's MAC address.