ESP8266 Request Page to Execute code on it

I have code hosted on a PHP web page that checks for changes in a database and executes a POST request accordingly. Rather than having to keep my computer on 24/7 to keep the code running on the php page, I figured that I’d use the ESP to continuously request the page so that the code can run.

My ESP8266 is hooked up to an Arduino Uno.
All AT commands work as intended and I was able to set up a basic web server.

I realize that I could have the ESP execute a GET from the database and then POST to the php page, but it seemed as though it would be a lot simpler if the php page handled both requests and the ESP simply ran the code on that page.

My code so far successfully executes a GET request from the ESP to the php page every second, but I’m not sure how to actually run the code on the php page.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "mySSID";
const char* password = "myPassword";

void setup () {


  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.print("Connecting..");

  }

}

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("http://mywebsite.com/code.php");  //Specify request destination
               int httpCode = http.GET();   //Send the request

    if (httpCode > 0) { //Check the returning code
      Serial.println("Retrieved...");                    
    }
  }
 // delay(1000);    //Send a request every second
  
  http.end();   //Close connection
}

Any help or advice would be appreciated.

If you

have code hosted on a PHP web page that checks for changes in a database and executes a POST request accordingly

then you have all you need- GETing the php page will lookup the database and do the POST.. It would be clearer if you posted the PHP code also.

If you want your ESP8266 to GET info from a database and POST to a PHP page then the next step for your code is to read the complete response and extract the info you actually need from the response.

rw950431:
Then you have all you need- GETing the php page will lookup the database and do the POST… It would be clearer if you posted the PHP code also.

The GET request is definitely working as intended and prints the page’s source to the serial if I add:

 String payload = http.getString();   //Get the request response payload
 Serial.println(payload);

I’m using an API to handle listening for changes in my database (Firebase, to be precise).

When changes are made, an ajax request executes a POST, and the php retrieves the changed message and prints it to to the page (see below).

<html>
<body>
<form action="" method="POST" id="myForm" onsubmit="return false;"></form>
<script type="text/javascript">

// listens for changes
firebase.on('child_changed', function(snapshot) {

    var obj = snapshot.val();
    var message = obj.message; // changed message retrieved from Firebase

    var $form = $('#myForm');
    
    // append message to form 
    $form.append($('<input type="hidden" name="message">').val(message)); 
 
    // submit form 
    $.ajax({
           type: 'POST',
           url: 'code.php', // current page
           data: $form.serialize(), 
           success: function (data) {
               payments.child(key).remove();
           }
     }); 

});
</script>
</body>
</html>
<?php 
if(isset($_POST['message'])) {

   $message = $_POST['message'];

   echo $message; 
}
?>

Again, simply keeping the page up on a tab works fine so I’m not sure why it does not work in this instance.

Your browser will be executing the clientside javascript function in the background- the ESP8266 will not.

But if you have a server running this PHP code cant you use it to run a scheduled task to poll the database instead of resorting to javascipt trickery? If javascript is your thing you can run that on the server using https://nodejs.org/en/ (but it seems like overkill).