0
votes

Seeking advice on getting the arduino and the ESP8266 wifi module to read a PHP file on a web page (not LAN; i am using a domain name and hosting service for the webpage) that echoes '1' or '0'. If it is a '1', i am looking at turning an LED on, and if '0', turning it off.

for example the PHP file looks like this to turn LED on: <?php echo 1; ?>

i need to be able to read off the php file to turn the LED on. What would be the best possible approach in this scenario? Is it better to send a HTTP GET request to the IP address of the ESP8266 wifi module or is there a way to program the module to read the echoed data off the php file? Is there another wifi module that would make this easier ?

if i have not made myself clear or you need further information to advise me please let me know.

thanks in advance !

1
PHP file looks like this <?php echo 1; ?> - KDEE Projects
Welcome to SO. To be clear, you have an Arduino that is calling a PHP Page. Based on the result of the PHP page, you want the LED to turn on or off. What information are you passing to the PHP Page? What is the condition? Please also post an example of your Arduino code and the PHP Code. - Twisty

1 Answers

0
votes

I would advise using an HTTP GET request from the Arduino. Depending on your stack code, it may not be able to resolve the domain name if DNS is not setup. So I would advise using the IP unless you know it can resolve your domain to the proper IP. You can see more on the WebClient examples: http://www.arduino.cc/en/Tutorial/WebClient

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /arduino.php?led=1 HTTP/1.1");
    client.println("Host: www.yourwebsite.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }

Then in your Loop, you look for the correct response (assuming LEDPIN has been defined in setup):

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    if(c == 1){
      digitalWrite(LEDPIN, HIGH);
    } else {
      digitalWrite(LEDPIN, LOW);
    }
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

The PHP can then do something like:

<?php

if(isset($_GET['led']) && $_GET['led']){
  // LED is on, send 0 to turn it off
  echo "0";
} else {
  // Turn LED on
  echo "1";
}

?>

So the page will always show a 0 unless you pass an led is passed and the conditions are met.

If you need more info or a more clear response, please update your question with more details. Post your code.