Sending data to arduino from a PHP script

Hi,

Here is my code which is pretty standard copied almost as is from somewhere. I am using an ENC28J60 module to give my arduino nano network access.

The code hosts a simple webpage with 2 buttons, on and off. It works fine to turn on and off my LED. I have a few issues though:

  1. The web page works fine from a PC, but not from an iOS device. The page won’t load at all.
  2. If I click on any button repeatedly before the previous operation finishes, the arduino gets stuck and has to be reset/rebooted.

It would be nice to know how to fix those issues, but even after I would prefer to have my raspberry pi host a php page where I would have several arduinos listed in different parts of the house to control different things.

So I know how to send data from arduino to a website like thingspeak, so I can send the data to my raspberry pi 3 the same way, but i want to do the opposite. Rather than having the raspberry pi overloaded with web requests from multiple devices, I want it to contact the arduino only when i ask it to. So on my php page when I click button X, it calls the appropriate arduino to turn device X on or off.

How can I do that? I need a starting point.

Thanks

#include <UIPEthernet.h> // Used for Ethernet

// **** ETHERNET SETTING ****
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };                                      
IPAddress ip(192, 168, 1, 15);                        
EthernetServer server(80);
String readString = "";
String javascriptCode = "";
int readIndex = 0;

boolean isBlink = false;
const int LED = 7;

void setup() {
  Serial.begin(9600);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());  

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED, OUTPUT);
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();

  if (client)
  {  
    Serial.println("-> New Connection");

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();

        //store characters to string
        readString = readString + c;

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n')
        {
          client.print(F("\
            <!DOCTYPE html>\
            <html>\
            <body>\
            <h2>Arduino Control 2</h2>\
            <button onclick=\"window.location = 'http://192.168.1.15/?1'\">Switch ON</button>\
            <button onclick=\"window.location = 'http://192.168.1.15/?0'\">Switch OFF</button>\
            </body>\
            </html>\
          "));

          Serial.println("readString " + readString);
          readIndex = readString.indexOf("?");
          Serial.println(readIndex);
          Serial.println("readStringVal >" + readString.substring(readIndex + 1, readIndex + 2)+"<");
          if(readString.indexOf("?") > 0) {
            if(readString.substring(readIndex + 1, readIndex + 2) == "1") {
              isBlink = true;          
            } else {
              isBlink = false;
            }
          }          
          Serial.println(isBlink);
          readString = "";          
          break;
        }
      }
    }

    // give the web browser time to receive the data
    delay(100);

    // close the connection:
    client.stop();
    Serial.println("Disconnected\n");
  }

  if(isBlink == true) {
    digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
    //delay(1000);                       // wait for a second
    //digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
    //delay(1000); 
  } else {
    digitalWrite(LED, LOW);
  }
}