Help with the simplest parsing of a PHP request

I’ve spent the last 4 hours looking at every example here and S.O. on how to read what a webserver is saying after you visit it with an arduino. In this case, I’m using a wemos d1 mini.

I wrote a basic php file that plays a sound when it is hit with the $_GET request, so I know the arduino is hitting it, but I’m usually not seeing any output in the serial monitor from the server response. When I get that, I would love to be able to parse that on the arduino. The gist of what I’m trying to accomplish is that I want the php server to give me single words like yes, no, pass, fail and I want to be able to look for those words in the arduino and do stuff based on that.

The problem with the other posts is that everyone is always trying to do so much with their code, that I can’t really figure out what is “necessary”. Here’s my simple php.

$msg = $_GET["msg"];

//sets the mp3 player based on win vs linux
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
   $player = 'cmdmp3.exe';
} else {
    $player = 'mpg123';
}

if($msg == 'cheer'){
  shell_exec($player.' sounds/good/cheer.mp3');
  echo('happy');
}

My Arduino code is…

#include <ESP8266WiFi.h>

byte mac[] = { 0xDE, 0xED, 0xE7, 0xF6, 0xDE, 0x96 };
IPAddress arduino_ip ( 192,  168,   95,  156);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,  168,   95,   1);
IPAddress subnet_mask(255, 255, 255,   0);
IPAddress server(192, 168, 95, 249);

const char* ssid = "Hoover2018";
const char* password = "@bcdef123";

WiFiClient client;


void setup()
{
  Serial.begin(9600);

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, password);


  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  // Allow the hardware to sort itself out
  delay(1500);

}


void loop()
{
  //if you're not connected to the server, connect!
  if (!client.connect(server, 80)){
    Serial.println("connecing");
    client.connect(server, 80);
  }

  //the address of my php file relative to the root
    client.println("GET /hda/pick/get.php?msg=cheer HTTP/1.1");
    client.println("Host: 192.168.95.249");
    client.println("Connection: close");
    client.println();

  while(client.connected()) {

    while(client.available())
    {
      char c = client.read();
      Serial.write(c);
     }
  }

  delay(20000);
}

Basically the mp3 file plays every 20 seconds, but I don’t get anything on my serial.write. I’m sure I need something to wait for the response a certain number of ms, but I haven’t gotten that to work consistently.

Once I get that, I’d like to know how to look for my “happy” on the arduino. My gut says that using some sort of special character as a key would make sense, but my c-fu is not awesome.

Thanks in advance!

Some things to try are:

  1. Issue 192.168.95.249/hda/pick/get.php?msg=cheer from a web browser on a client in the same network as your apache/php server. It may not look pretty because you have not wrapped your message 'happy' in a nice HTML page but you should see something.

  2. Comment out "shell_exec($player.' sounds/good/cheer.mp3');" in your PHP code now you know that part works. It may interfere with the communication back to the client.

Thanks so much for your help.

Visiting from the browser triggers the sound and shows the word.

Commenting out the sound doesn't change anything either.

Any help you can give would be really appreciated.

Try this to wait a few seconds until the client is available after sending the HTTP GET:

#include <ESP8266WiFi.h>

byte mac[] = { 0xDE, 0xED, 0xE7, 0xF6, 0xDE, 0x96 };
IPAddress arduino_ip ( 192,  168,   95,  156);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,  168,   95,   1);
IPAddress subnet_mask(255, 255, 255,   0);
IPAddress server(192, 168, 95, 249);

const char* ssid = "Hoover2018";
const char* password = "@bcdef123";

WiFiClient client;


void setup()
{
  Serial.begin(9600);

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, password);


  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  // Allow the hardware to sort itself out
  delay(1500);

}


void loop()
{
  //if you're not connected to the server, connect!
  if (!client.connect(server, 80)) {
    Serial.println("connecing");
    client.connect(server, 80);
  }

  //the address of my php file relative to the root
  client.println("GET /hda/pick/get.php?msg=cheer HTTP/1.1");
  client.println("Host: 192.168.95.249");
  client.println("Connection: close");
  client.println();

  while (client.connected()) {
    unsigned long timeout = millis();
    // stay here until client available or timeout
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(" Client Timeout !");
        client.stop();
        return;
      }
    }
    while (client.available())
    {
      char c = client.read();
      Serial.write(c);
    }
  }

  delay(20000);
}

That seems to be working! Thank you so much!!!!!

Yeah. I wasn’t sure enough about what was happening with the client stop etc to make that happen. I don’t think I would have ever figured that out on my own. Thanks so much!